There are no items in your cart
Add More
Add More
Item Details | Price |
---|
Lets Understand How kernel Synchronization works
Mon Jul 25, 2022
Understanding Kernel Synchronization
If a resource is being shared between multiple process at same time then we need to provide protection from concurrent access because if two or more thread will access and manipulate the data at same time then thread may overwrite each other's changes or access the data while it's in an inconsistent state. The process of maintaining multiple access to shared data at same in safe manner is called synchronization .In this chapter we will discuss what is synchronization , why it's needed, what is critical region and race conditions ,what is deadlock. In next chapter we will discuss various synchronization techniques available in Linux Kernel.
Critical Region and race conditionsPiece of code which access the shared data is called critical Region or critical section . If multiple process execute this code at same time then there might be chances of inconsistency of data. Hence System programmer need to ensure that two or more process should not execute critical section at same time. When two or more threads executing same critical section at same time is called Race condition. So to avoid race condition we need to use synchronization . Now let's understand with example that why we need protection Consider a global integer variable i and a simple critical section which tries to increment the variable. Let's assume two thread A and B tries to enter in critical section at same time and initial value of i is 5. below is the expected operation
Thread 1 | Thread 2 |
Get value of i(5) | |
Increment value of i(5>>6) | |
Write back value of i in memory. | |
Get value of i (6) | |
Increment value of i(6>>7) | |
Write back value of i in memory. | |
Thread 1 | Thread 2 |
Get value of i(5) | Get value of i (5) |
Increment value of i(5>>6) | - - - - - - - |
- - - - - - - | Increment value of i(5>>6) |
Write back value of i in memory (6). | - - - - - - - - |
- - - - - - - - | Write back value of i in memory(6). |
Thread 1 | Thread 2 |
Increment and store value of i(5>>6) | - - - - - - - - - - |
- - - - - - - - - - | Increment and store value of i (6>>7) |
Cause of concurrency In linux kernel there are various reason for concurrency to occur
1)Kernel Preemption As Kernel got preemptive after 2.6 version . It means one thread can preempt another task. The task which is newly scheduled might be executing in same critical region.
2) InterruptsAs interrupt occurs asynchronously any time , which interrupt the execution of current task.
3)softirqs and taskletsThe can raise or schedule a softirq or tasklet any time, interrupting the currently executing process.
4) Symmetrical multiprocessingTwo or more processor can execute same kernel code at same time which can cause race condition.
5)SleepingA task in kernel can sleep any time and ask kernel schedule to schedule new process. Before writing code developer should indentify all the possible cause of concurrency . If all the reason of concurrency well known then its very easy for Kernel developer to avoid the race conditions.
Knowing Which data to need protection
Developer has to identify which part of data need protection . Generally its very easy to identify the data which needs protection . All the local variable which are specific to some particular thread they don't need any protection. But all the global variable which can be accessed by other processes also needs to be protected . Below are the other possible data which needs to be protected 1) Data shared between process context and interrupt context2) Data shared between currently executing process and new scheduled process who preempted the currently executing task3) Function or code which can be executed by two or more processor at same time.
Deadlocks
Deadlock refers to a situation in which two or more threads are waiting for each process to release a resource . Because of which neither of process get chance to proceed further as the resource for which all the process are waiting will never be available .For example if a process tries to acquire a lock which its already hold will result a deadlock. As process will never release the previously held lock as its waiting for other lock to acquire.There are various techniques of avoiding the deadlock. We will discuss deadlocks and various method of avoiding deadlock in other tutorial.
Admin
We love to share knowledge on all Embedded System Concepts. Please reach out to us for any Embedded Systems related Training