Tag: Critical Section Problem

Critical Section | Critical Section Problem

Critical Section-

 

Before you go through this article, make sure that you have gone through the previous article on Process Synchronization.

 

We have discussed-

  • Process Synchronization controls the execution of processes running concurrently so as to produce the consistent results.
  • Critical section is a part of the program where shared resources are accessed by the process.

 

Critical Section Problem-

 

  • If multiple processes access the critical section concurrently, then results produced might be inconsistent.
  • This problem is called as critical section problem.

 

Synchronization Mechanisms-

 

Synchronization mechanisms allow the processes to access critical section in a synchronized manner to avoid the inconsistent results.

 

For every critical section in the program, a synchronization mechanism adds-

  • An entry section before the critical section
  • An exit section after the critical section

 

 

Entry Section-

 

  • It acts as a gateway for a process to enter inside the critical section.
  • It ensures that only one process is present inside the critical section at any time.
  • It does not allow any other process to enter inside the critical section if one process is already present inside it.

 

Exit Section-

 

  • It acts as an exit gate for a process to leave the critical section.
  • When a process takes exit from the critical section, some changes are made so that other processes can enter inside the critical section.

 

Criteria For Synchronization Mechanisms-

 

Any synchronization mechanism proposed to handle the critical section problem should meet the following criteria-

 

 

  1. Mutual Exclusion
  2. Progress
  3. Bounded Wait
  4. Architectural Neutral

 

1. Mutual Exclusion-

 

The mechanism must ensure-

  • The processes access the critical section in a mutual exclusive manner.
  • Only one process is present inside the critical section at any time.
  • No other process can enter the critical section until the process already present inside it completes.

 

2. Progress-

 

The mechanism must ensure-

  • An entry of a process inside the critical section is not dependent on the entry of another process inside the critical section.
  • A process can freely enter inside the critical section if there is no other process present inside it.
  • A process enters the critical section only if it wants to enter.
  • A process is not forced to enter inside the critical section if it does not want to enter.

 

3. Bounded Wait-

 

The mechanism should ensure-

  • The wait of a process to enter the critical section is bounded.
  • A process gets to enter the critical section before its wait gets over.

 

4. Architectural Neutral-

 

The mechanism should ensure-

  • It can run on any architecture without any problem.
  • There is no dependency on the architecture.

 

Important Notes-

 

Note-01:

 

  • Mutual Exclusion and Progress are the mandatory criteria.
  • They must be fulfilled by all the synchronization mechanisms.

 

Note-02:

 

  • Bounded waiting and Architectural neutrality are the optional criteria.
  • However, it is recommended to meet these criteria if possible.

 

To gain better understanding of Critical Section in OS,

Watch this Video Lecture

 

Next Article- Lock Variable | Synchronization Mechanism

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Process Synchronization | Race Condition in OS

Process Synchronization-

 

When multiple processes execute concurrently sharing system resources, then inconsistent results might be produced.

  • Process Synchronization is a mechanism that deals with the synchronization of processes.
  • It controls the execution of processes running concurrently to ensure that consistent results are produced.

 

Need of Synchronization-

 

Process synchronization is needed-

  • When multiple processes execute concurrently sharing some system resources.
  • To avoid the inconsistent results.

 

Critical Section-

 

Critical section is a section of the program where a process access the shared resources during its execution.

 

Example-

 

The following illustration shows how inconsistent results may be produced if multiple processes execute concurrently without any synchronization.

 

Consider-

  • Two processes P1 and P2 are executing concurrently.
  • Both the processes share a common variable named “count” having initial value = 5.
  • Process P1 tries to increment the value of count.
  • Process P2 tries to decrement the value of count.

 

 

In assembly language, the instructions of the processes may be written as-

 

 

Now, when these processes execute concurrently without synchronization, different results may be produced.

 

Case-01:

 

The execution order of the instructions may be-

P1(1), P1(2), P1(3), P2(1), P2(2), P2(3)

In this case,

Final value of count = 5

 

Case-02:

 

The execution order of the instructions may be-

P2(1), P2(2), P2(3), P1(1), P1(2), P1(3)

In this case,

Final value of count = 5

 

Case-03:

 

The execution order of the instructions may be-

P1(1), P2(1), P2(2), P2(3), P1(2), P1(3)

In this case,

Final value of count = 6

 

Case-04:

 

The execution order of the instructions may be-

P2(1), P1(1), P1(2), P1(3), P2(2), P2(3)

In this case,

Final value of count = 4

 

Case-05:

 

The execution order of the instructions may be-

P1(1), P1(2), P2(1), P2(2), P1(3), P2(3)

In this case,

Final value of count = 4

 

It is clear from here that inconsistent results may be produced if multiple processes execute concurrently without any synchronization.

 

Race Condition-

 

Race condition is a situation where-

  • The final output produced depends on the execution order of instructions of different processes.
  • Several processes compete with each other.

The above example is a good illustration of race condition.

 

PRACTICE PROBLEM BASED ON PROCESS SYNCHRONIZATION-

 

Problem-

 

The following two functions P1 and P2 that share a variable B with an initial value of 2 execute concurrently-

 

 

The number of distinct values that B can possibly take after the execution is-

  1. 3
  2. 2
  3. 5
  4. 4

 

Solution-

 

Different execution order of the instructions of P1 and P2 produce different results.

 

Case-01:

 

The execution order of the instructions may be-

P1(1), P1(2), P2(1), P2(2)

In this case,

Final value of B = 3

 

Case-02:

 

The execution order of the instructions may be-

P2(1), P2(2), P1(1), P1(2)

In this case,

Final value of B = 4

 

Case-03:

 

The execution order of the instructions may be-

P1(1), P2(1), P2(2), P1(2)

In this case,

Final value of B = 2

 

Case-04:

 

The execution order of the instructions may be-

P2(1), P1(1), P1(2), P2(2)

In this case,

Final value of B = 3

 

Case-05:

 

The execution order of the instructions may be-

P1(1), P2(1), P1(2), P2(2)

In this case,

Final value of B = 3

 

Case-06:

 

The execution order of the instructions may be-

P2(1), P1(1), P2(2), P1(2)

In this case,

Final value of B = 2

 

From here,

  • Distinct values that may be produced are 2, 3 and 4.
  • Number of distinct values that may be produced = 3

Thus, Option (A) is correct.

 

To gain better understanding of Process Synchronization,

Watch this Video Lecture

 

Next Article- Synchronization Mechanisms

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.