Critical Section Problem in OS

Critical Section Problem in OS

An operating system is a big set of codes to perform useful tasks on the hardware. So, we can say that a big operating system consists of many segments or sections. A critical section is one of the sections among different segments of the operating system.

Every process can have its own critical section. The Critical section can be accessed by only one process at a time. In the Critical section, there are many variables and functions that are shareable among different processes.  Now, the question is: Which process will access the Critical Section first. It depends upon the selected scheduling algorithm that which process will get into the critical section first and will enjoy the different variables and functions in the critical section.

Critical Section Problem in OS
Figure: Critical Section Problem in OS

Example of Critical Section Problem

Suppose P1 is a process and the Critical Section is assigned to the P1. Now, if P2  is requesting to enter into the critical section to perform some task, then P2 needs to be a wait because P1 is already using the critical section. When P1 leaves the Critical Section, then the Operating System can assign the Critical Section to the process P2.

Pseudocode of Critical Section Problem

do

{

Entry in the Critical Section

          Critical Section

Exit from the Critical Section

          Remainder Section

}

while(True);

How to Solve Critical Section Problem

The solution to the Critical Section Problem must satisfy the following three conditions;

Mutual Exclusion

If a process is executing in its critical section, then  no other process can  execute in its critical section

Example of Mutual Exclusion

Suppose a process P1 is executing in its critical section, then if the P2, P3 or some else process try to enter into the critical section of the P1, then all these processes need to wait until the P1 leaves the Critical Section.

Progress

If a process is not executing its own critical section, then it should not stop any other process to access the Critical Section.  We can say that any process can enter into the critical section of any other process if it observes that the Critical Section is free.

Example of  Progress

If a process P1 is not executing its own critical section, then P2 or some other process arrives to enter into the Critical Section of the P1, then P1 it should not stop any other process to access the Critical Section.

Bounded Waiting

Suppose, a process allows any of the processes to enter and to executes in its critical section. An unknown process arrives and leaves the Critical Section and repeats this process again and again in a spammy manner. Then what Operating System can do?

Abound can be maintained on the number of times that how many times the other processes are allowed to enter into their critical sections.

Example of  Bounded Waiting

Suppose a bound can be maintained for Process P1 that if P2 or any other process wants to enter into the critical section of the P1, they can enter and leave the Critical Section of P1 only 1 time or any time, etc.

Types of Critical Section Problem Solutions

Operating System solutions of Critical Section Problem

Provide some data structures and functions to the Operating System designers with the help of system calls.

Hardware solutions of Critical Section Problem

Hardware solutions to the critical section problem rely on machine instructions.

Programming Language solutions of  Critical Section Problem

Linguistic constructs provided as a part of a language.

Software solutions of Critical Section Problem

The algorithms whose correctness does not rely on any other assumptions.

Some of the software-based solutions of Critical Section Problem are mentioned below;

Peterson’s Algorithm

Peterson’s Algorithm is based on busy waiting.

Semaphores

Semaphores are used for signaling among the processes. For more details – Click Here

Monitors

Monitors are the abstract data type. Monitors used for synchronizing the processes. For more details – Click Here

C++ Code for Critical Section Problem

Let’s see the C language code for the demo of the Critical Section Problem.

This code is a demo that how a process can enter into the critical section.
The lock variable in the program is initially set with 0.
When a process tries to enter into its critical region, then it first tests the value of the Setlock variable. if the SetLock value is set as 0,  the process will change it to 1 and enters into the critical section.
Now, let’s see the another case, if the value of SetLock variable is already set as 1, then the process will wait for critical section until it becomes 0, thus the SetLock value 0 means that no process is in its critical section and there is an opportunity for any process to enter into its critical section.

 

Why we need to Synchronize the Processes

To work better for sharable resources. There are many examples of shareable resources. For example, some of the shareable resources are mentioned below;

  • Variables
  • Devices
  • Buffers and many more.

There are some situations when cooperating processes does not need to  synchronize the shareable resources:

  1. When only one process writes (atomically) the data or instructions, and all other processes read the data or instructions.
  2. When all processes are write-only.
  3. When all processes are read-only.

Video Lecture