Process Synchronization in Operating System

FREE Online Courses: Your Passport to Excellence - Start Now

Process Synchronization means coordinating the execution of processes such that no two processes access the same shared resources and data. It is required in a multi-process system where multiple processes run together, and more than one process tries to gain access to the same shared resource or data at the same time.

Changes made in one process aren’t reflected when another process accesses the same shared data. It is necessary that processes are synchronized with each other as it helps avoid the inconsistency of shared data.

For example: A process P1 tries changing data in a particular memory location. At the same time another process P2 tries reading data from the same memory location. Thus, there is a high probability that the data being read by the second process is incorrect.

Sections of a Program in OS

Following are the four essential sections of a program:

1. Entry Section: This decides the entry of any process.

2. Critical Section: This allows a process to enter and modify the shared variable.

3. Exit Section: This allows the process waiting in the Entry Section, to enter into the Critical Sections and makes sure that the process is removed through this section once it’s done executing.

4. Remainder Section: Parts of the Code, not present in the above three sections are collectively called Remainder Section.

Types of process in Operating System

On the basis of synchronization, the following are the two types of processes:

1. Independent Processes: The execution of one process doesn’t affect the execution of another.

2. Cooperative Processes: Execution of one process affects the execution of the other. Thus, it is necessary that these processes are synchronized in order to guarantee the order of execution.

Critical Section Problem in OS

A segment of code that a signal process can access at a particular point of time is known as the critical section. It contains the shared data resources that can be accessed by other processes.

Wait() function (represented as P()) handles the entry of a process to the critical section. Whereas signal() function (represented as V()) handles the exit of a process from the critical section. A single process executes in a critical section at a time. Other processes execute after the current process is done executing.

Race Condition in OS

When more than one processes execute the same code or access the same memory/shared variable, it is possible that the output or value of the shared variable is wrong. In this condition, all processes race ahead in order to prove that their output is correct. This situation is known as race condition.

When multiple processes access and manipulate the same data concurrently the outcome depends on the order in which these processes accessed the shared data. When the output of multiple thread execution differs according to the order in which the threads execute, a race condition occurs.

We can avoid it if we treat the critical section as an atomic instruction and maintain proper thread synchronization using locks or atomic variables.

Rules for Critical Section

There are three rules that need to be enforced in the critical section. They are:

1. Mutual Exclusion: A special type of binary semaphore used to control access to shared resources. It has a priority inheritance mechanism that helps avoid extended priority inversion problems. Only one process can execute at a time in the critical section.

2. Progress: We use it when the critical section is empty, and a process wants to enter it. The processes that are not present in their reminder section decide who should go in, within a finite time.

3. Bound Waiting: Only a specific number of processes are allowed into their critical section. Thus, a process needs to make a request when it wants to enter the critical section and when the critical section reaches its limit, the system allows the process’ request and allows it into its critical section.

Solutions To The Critical Section

Following are some common solutions to the critical section problem:

1. Peterson Solution: If a process executes in a critical state, the other process can only execute the rest of the code and vise-versa.This method developed by Peterson is widely used and helps make sure that only one process runs at a specific time in the critical section

Example: Let there be N processes (P1, P2, … PN) such that at some point of time every process needs to enter the Critical Section. There is a FLAG[] array of size N that is false by default. Therefore, the flag of a process needs to be set true, whenever it wants to enter the critical section.

A variable TURN tells the process number waiting to enter the Critical Section. When a process enters the critical section it changes the TURN to another number from the list of ready processes when it is exiting.

Program:

PROCESS Pi
FLAG[i] = true
while( (turn != i) AND (CS is !free) ){ wait;
}
CRITICAL SECTION FLAG[i] = false
turn = j; //choose another process

Process Synchronization in OS

2. Synchronization Hardware: Hardware can also help resolve the problems of critical sections sometimes. Some OS offer lock functionality. This gives a process a lock when it enters the critical section and releases the lock after it leaves a critical section. Due to this other processes can’t enter a critical section when a process is already inside.

3. Mutex Locks: Mutex Locks is a strict software method in which a LOCK over critical resources is given to a process in the entry section of code. The process can use this LOCK inside the critical section and can get rid of it in the exit section.

4. Semaphore Solution: Semaphore is a non-negative variable that is shared between threads. It is a signaling mechanism that uses a thread waiting on a semaphore, to signal another thread. It makes use of wait and signal for process synchronization.

Example:

WAIT ( S );
while ( S <= 0 );
S = S - 1;
SIGNAL ( S ):
S = S + 1;

Summary

Process Synchronization means coordinating the execution of processes such that no two processes access the same shared resources and data. There are four sections of a program: Entry Section, Critical Section, Exit Section, and Remainder Section. A segment of code that a signal process can access at a particular point of time is known as the critical section.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

2 Responses

  1. brittany says:

    i love this this is so helpfull

  2. brittany says:

    i love this this is so helpfull

Leave a Reply

Your email address will not be published. Required fields are marked *