Critical Section Problem in Operating System

A contention situation arises if two independent programs or processes want to access common data, and this can lead to a collision. Code segments that access common data areas, are prone to collisions known as critical sections. A critical section is understood as program parts that must not be interrupted by other processes or threads while executed on the CPU. 

It provided the processes or threads involved access shared resources. An atomic action is needed in a critical section i.e., a single process can run at a time in its critical section. All the entire processes have to wait to run in their critical sections. 

Critical Section Brief Explanation

We can also say that a critical section is a code segment in a complete program where processes access shared resources. Shared resources can be a common variable or file. Suppose our code is performing some write operations on common variable.

As multiple processes are executing concurrently, any process can be interrupted in the mid of its execution. Due to partial execution on shared resources by processes I mean here we can say shared variables can lead to data inconsistencies. This situation is known as a race condition.

Race conditions lead to inconsistent states of data. Therefore, we need a synchronization protocol that allows processes to cooperate while manipulating shared resources, which essentially is the critical section problem.

Requirements for critical section problems are the following

Primary

  • Mutual exclusion: If a process P1 is executing in a critical section then other processes can’t be executed in their critical section.
  • Progress: If a process doesn’t require to run in a critical section, then it should not let wait indefinitely for other processes to take into the critical section. 

Secondary

  • Bounded waiting: Capable to forecast the waiting time for the entire process to take into the critical section. 
  • Architectural neutrality: When our solution is functioning perfectly on architecture, then it should execute on the other ones also. Hence, our technique must be architectural natural.