Tuesday, 16 May 2017

Kernel

Atomic Variables in Linux kernel


But in a multiprocessor system, the memory bus might be released after the read and while one processor updates the value another processor can make use of the bus.
If two processors, processor A and processor B reach this instruction at the same time. Assume the value of val is 3 initially.


But in a multiprocessor system, the memory bus might be released after the read and while one processor updates the value another processor can make use of the bus.
If two processors, processor A and processor B reach this instruction at the same time. Assume the value of val is 3 initially.

Processor A reads the value 3 and lets go of the bus
While processor A updates the val processor B also reads val as 3 and lets the go of the bus
Now processor A updates the new value 4 in the val
Then processor B also updates the value of val as 4.


This simultaneous access can be prevented using semaphores and spinlocks but implementing a semaphore or spinlock for one instruction might be an overkill.


There is another solution in linux kernel called atomic variables


In the above example if val was an atomic variable then processor B would have been allowed access to the variable only after processor A has finished the updating the visible, until then the processor A would have held on to the memory bus.


To make use of the atomic variables the variable needs to be declared as of type atomic_t.


atomic_t *val   Declaration

atomic_read(val): Returns the value of *val
atomic_set(val,i) :  Sets *val to i


atomic_add(i,val):    adds i to *val
atomic_sub(i,val):  Subtracts i from *val


   

Normal Linux kernel vs RTOS 
Normal Linux Kernel is a preemptive kernel but not real time, of course. In most multithreading environments (also called multitasking), a preemptive kernel allows the thread that has higher priority to receive longer time on the processor. And, conversely a lower priority thread will have less time with the processor. 


 




 


       

No comments:

Post a Comment