Critical Section(CS)


  • Critical Section is a segment of a code that contains shared variables or resources which are needed to be synchronized among multiple processes or threads.
  • If allowed to access simultaneously by more than one process or thread, it leads to data loss or inconsistencies.
  • In simple terms, a critical section contains instruction or a group of instructions that must be executed atomically.
  • Thus to avoid such inconsistencies or loss of data, processes or threads must be synchronized with each other and only one process or a thread can enter the critical section at a time.

Thus, process synchronization is nothing but controlling the execution of one process or thread by another process or thread using an implicit Operating system mechanism or an explicit mechanism.

Diag-1: Critical Section

Elements of Critical section:

There are four elements of a critical section:

  • Entry Section:
    It is basically a condition that decides whether a process or a thread can enter the critical section or not.
  • Critical section:
    If the condition happens to be true, the process enters this section of code and modifies the shared variables. It acquires the resources needed by the process to execute.
  • Exit Section:
    It handles the exit from the critical section and allows the other process waiting in the entry section to enter the CS.
  • Remainder Section:
    All other parts of code, which are not part of CS are known as a remainder section.

Diag-2: Elements of a Critical Section(CS)

Rules for Critical Section:

Critical Section problem states that only one process can enter the critical section at a time and hence it needs a synchronization among processes. The solution to the critical Section must satisfy the below conditions:

  • Mutual Exclusion(ME):
    This implies that only one process can enter the critical section at a time, another process must wait at the entry section until the entered process exists.
  • Progress:
    This implies that if the critical section is free, any process can enter the CS, any process cannot stop other processes from doing so.
  • Bounded waiting:
    This implies that each process must have a limited waiting time. It should not wait endlessly to access the critical section. A bound must exist on the number of times other processes are allowed to enter the CS after a process has made to request to enter its CS and before that request is granted.


Consider a scenario where more than one process is allowed to enter the critical section that is allowed to access the shared variables or resources.

  • If one process reads a shared variable and modifies its value(in register-R1) but before the value is written, the other process reads the same variable.
  • Hence later process does not read the updated data and modifies the same and also writes it to memory.
  • Now the first process again reads the value from its register(R1) and writes its to memory and hence the value of the shared variable updated by the second process is lost. Thus data is either lost or the value is inconsistent if the synchronization is not maintained among processes.
P1                                            p2
{                                             {
    x++;                                           x--;
}                                             }
p1                                     p2
{                                      {
  a) Move x to R1                        a) Move x to R2
  b) Increment the R1                    b) Decrement the R2
  c) Copy R1 to x                        c) Copy R2 to x
}                                      }

Now as per to above example, consider the value of x to be 5

case 1:
Order : p1(a) -> p1(b)->p2(a)->p2(b)->p2(c)->p1(c)
Value : 6

case 2:
Order : p1(a)->p1(b)->p1(c)->p2(a)->p2(b)->p2(c)
Value : 5

Hence we can see that the value of x is inconsistent and it depends upon the order of execution. To overcome this both the processes must be synchronized.

Solution to Critical Section: The solution to Critical section can broadly be classified as:

  • Mutex Lock and
  • Semaphore

Relevant Posts:

Categories: Operating system (OS)

8 replies


  1. Cancel a thread:pthread_cancel() API - Tech Access
  2. Thread synchronization - Tech Access
  3. Race Condition - Tech Access
  4. Mutual Exclusion(Mutex) - Tech Access
  5. Create a thread: pthread_create() API - Tech Access
  6. Comparison- Mutex and Semaphore - Tech Access
  7. Condition Variables - Tech Access
  8. Index of Operating System - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: