When more than one processes access a same code segment that segment is known as critical section. Critical section contains shared variables or resources which are needed to be synchronized to maintain consistency of data variable.
In simple terms a critical section is group of instructions/statements or region of code that need to be executed atomically (read this post for atomicity), such as accessing a resource (file, input or output port, global data, etc.).
In concurrent programming, if one thread tries to change the value of shared data at the same time as another thread tries to read the value (i.e. data race across threads), the result is unpredictable.
The access to such shared variable (shared memory, shared files, shared port, etc…) to be synchronized. Few programming languages have built-in support for synchronization.
It is critical to understand the importance of race condition while writing kernel mode programming (a device driver, kernel thread, etc.). since the programmer can directly access and modifying kernel data structures.
A simple solution to the critical section can be thought as shown below,
acquireLock(); Process Critical Section releaseLock();
A thread must acquire a lock prior to executing a critical section. The lock can be acquired by only one thread. There are various ways to implement locks in the above pseudo code. Let us discuss them in future articles.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Python | How to lock Critical Sections
- Process Synchronization | Set 2
- Top 5 Topics for Each Section of GATE CS Syllabus
- Semaphores in Process Synchronization
- Monitors in Process Synchronization
- Synchronization in Distributed Systems
- Mutual Exclusion in Synchronization
- Introduction of Process Synchronization
- Dekker's algorithm in Process Synchronization
- Priority Inheritance Protocol (PIP) in Synchronization
- Peterson's Algorithm in Process Synchronization
- Lock Variable Synchronization Mechanism
- Bakery Algorithm in Process Synchronization
- Mutex lock for Linux Thread Synchronization
- Classical problems of Synchronization with Semaphore Solution
- Sleeping Barber problem in Process Synchronization