Skip to content
Related Articles
Open in App
Not now

Related Articles

Critical Section in Synchronization

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 22 Jan, 2023
Improve Article
Save Article

Critical Section: When more than one processes access the same code segment that segment is known as the critical section. The critical section contains shared variables or resources which are needed to be synchronized to maintain the consistency of data variables. In simple terms, a critical section is a 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 variables (shared memory, shared files, shared port, etc…) is to be synchronized. Few programming languages have built-in support for synchronization. It is critical to understand the importance of race conditions while writing kernel-mode programming (a device driver, kernel thread, etc.). since the programmer can directly access and modify kernel data structures.


The use of critical sections in a program can cause a number of issues, including:

  • Deadlock: When two or more threads or processes wait for each other to release a critical section, it can result in a deadlock situation in which none of the threads or processes can move. Deadlocks can be difficult to detect and resolve, and they can have a significant impact on a program’s performance and reliability.
  • Starvation: When a thread or process is repeatedly prevented from entering a critical section, it can result in starvation, in which the thread or process is unable to progress. This can happen if the critical section is held for an unusually long period of time, or if a high-priority thread or process is always given priority when entering the critical section.
  • Overhead: When using critical sections, threads or processes must acquire and release locks or semaphores, which can take time and resources. This may reduce the program’s overall performance.

It could be visualized using the pseudo-code below:-

    while(flag); // (entry section)
        // critical section
    if (!flag)
        // remainder section
} while(true);

A simple solution to the critical section can be thought of as shown below,

Process Critical Section

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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!