×

Operating System Tutorial

Operating System Practice

Critical Section in Operating System

In this tutorial, we will learn about the race condition, solution to race condition, critical section problem, and the solution to satisfy the critical section problem in operating system. By Prerana Jain Last updated : May 07, 2023

Race Condition

When different process access same data and shared the same resources and their process of the outcome depends on the particular order then this phenomenon is called race condition (i.e. it depends on the speed of exclusion).

P()
{   
    R(i)
    i = i+1;
    w(1)
}

The race condition is a scenario whereby changing the order of execution of a process we can change the output here the final value is 11 but if a process executes one after another then it will be 12.

Solution

We need to ensure that only process at a time should be manipulating shared data i.e. the processes by synchronized in some way.

Critical Section Problem

The portion of the program where the shared data variables or shared resources or shared data will be placed is called a critical section. Each process has a critical section where it changes common variables, update tables, write a table, and so on.

When one process is allowed to execute in its critical section no other process is allowed to execute in its critical section that is no two process be in its critical section at the same time.

P1()
{  
    while()
    {    
        initial section
        Entry section
        Critical section
        Exit section
        Remainder section
    }
}

The execution before the critical section is called an initial section and after is called a remainder section. It depends totally on the process whether it will never go into a critical section or will go more than 1 time.

Entry section: Each process must repeated process to enter its critical section. Entry section implements this code.

Exit section: Code followed by critical section.

Remainder section: Remainder code (i.e. other than shared variables/data).

    do{
        entry section
        critical section
        exit section
        remainder section
    }  while(true);

Solutions to Critical Section Problem

Solutions to critical section problem: There are some solutions which satisfy the critical section problem.

1. Mutual exclusion

When one process pi is executing in its critical section so at that time no other process can be executing in the critical section problem.

2. Progress

When no process is executing in its critical section and there exists some process that wishes to enter in their critical section then only those processes can participate in the decision of which will enter its critical section which is not executing in their remainder section next and this selection cannot be postponed indefinitely.

3. Bounded Waiting

A bounding function exists on the number of processes and at that time that other processes are allowed to enter their critical section after a process has made a request to enter its critical section and before that request is granted.

Two process solution

P0 P1
while(1)
{ 
    initial section
    while(turn !=0) 
    Critical section 
    Turn = 1
    Remainder section 
}
while(1)
{ 
    initial section
    while(turn !=1) 
    Critical section 
    Turn = 0
    Remainder section 
}

It is a two process solution which uses a Boolean variables turn. It satisfies mutual exclusion condition but it fails on progress as it required strict alternation the solution is valid.

Example

P0 P1
while(1)
{ 
    initial section
    flag[0] = T
    while(flag[1])
    Critical section;
    Flag[0] = F;
    Remainder section 
}
while(1)
{ 
    initial section
    flag[1] = T
    while(flag[0])
    Critical section;
    Flag[1] = F;
    Remainder section 
}

In this solution, we used a boolean array called flag where each process has one call where false means not interested and true means interested. This solution satisfies mutual exclusion but fails on progress because it suffers from Deadlock.

This is a two process solution known as Peterson solution or Deskerls algorithm.

Here we have two resources:

  1. Array flag which is used to tell interested or not.
  2. Turn which is used to decide when both processes are interested.

It is a valid solution which satisfies both mutual exclusion and progress (bounded wait or well).




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.