0% found this document useful (0 votes)
72 views

Process Synchronization

Process synchronization is required when multiple processes need to access shared resources. There are two types of processes: independent processes whose execution does not affect others, and cooperative processes whose execution impacts others. Cooperative processes need synchronization to preserve execution order. The critical section is the part of code accessing shared resources, and only one process can execute it at a time to avoid race conditions. Common synchronization problems include the bounded buffer, sleeping barber, dining philosophers, and readers-writers problems. Solutions include Peterson's algorithm, semaphores, and mutex locks.

Uploaded by

nanekaraditya06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Process Synchronization

Process synchronization is required when multiple processes need to access shared resources. There are two types of processes: independent processes whose execution does not affect others, and cooperative processes whose execution impacts others. Cooperative processes need synchronization to preserve execution order. The critical section is the part of code accessing shared resources, and only one process can execute it at a time to avoid race conditions. Common synchronization problems include the bounded buffer, sleeping barber, dining philosophers, and readers-writers problems. Solutions include Peterson's algorithm, semaphores, and mutex locks.

Uploaded by

nanekaraditya06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Process Synchronization

Introduction of Process Synchronization

On the basis of synchronization, processes are categorized as one of


the following two types:
 Independent Process : Execution of one process does not affects
the execution of other processes.
 Cooperative Process : Execution of one process affects the
execution of other processes.
Process synchronization problem arises in the case of Cooperative
process also because resources are shared in Cooperative processes.
Introduction
When two or more process cooperates with each other, their order of
execution must be preserved otherwise there can be conflicts in their
execution and inappropriate outputs can be produced.

A cooperative process is the one which can affect the execution of


other process or can be affected by the execution of other process.
Such processes need to be synchronized so that their order of
execution can be guaranteed.

The procedure involved in preserving the appropriate order of


execution of cooperative processes is known as Process
Synchronization. There are various synchronization mechanisms that
are used to synchronize the processes.
Race Condition
When more than one processes are executing the same code
or accessing the same memory or any shared variable in that
condition there is a possibility that the output or the value
of the shared variable is wrong so for that all the processes
doing the race to say that my output is correct this condition
known as a race condition. Several processes access and
process the manipulations over the same data concurrently,
then the outcome depends on the particular order in which
the access takes place.
Critical Section

The regions of a program that try to access shared resources


and may cause race conditions are called critical section. To
avoid race condition among the processes, we need to assure
that only one process at a time can execute within the
critical section.
The Critical Section Problem
 Critical Section is the part of a program which tries to access shared
resources. That resource may be any resource in a computer like a
memory location, Data structure, CPU or any IO device.
 The critical section cannot be executed by more than one process at the
same time; operating system faces the difficulties in allowing and
disallowing the processes from entering the critical section.
 The critical section problem is used to design a set of protocols which can
ensure that the Race condition among the processes will never arise.
 In order to synchronize the cooperative processes, our main task is to
solve the critical section problem. We need to provide a solution in such a
way that the following conditions can be satisfied.
Requirements of Synchronization mechanisms

Solution to critical section problem must satisfy the following requirement:-

Primary
Mutual Exclusion
Our solution must provide mutual exclusion. By Mutual Exclusion, we mean that if one
process is executing inside critical section then the other process must not enter in the
critical section.
Progress

Progress means that if one process doesn't need to execute into critical section
then it should not stop other processes to get into the critical section.
Secondary

Bounded Waiting
We should be able to predict the waiting time for every process to get into the
critical section. The process must not be endlessly waiting for getting into the
critical section.
There exists a bound, or limit, on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to
enter critical section and before that request is gurnated.
Solutions To The Critical Section
In Process Synchronization, critical section plays the main role so that the
problem must be solved.

Here are some widely used methods to solve the critical section problem.
Peterson Solution:-
Peterson’s solution is widely used solution to critical section problems. This
algorithm was developed by a computer scientist Peterson that’s why it is named
as a Peterson’s solution.
In this solution, when a process is executing in a critical state, then the other
process only executes the rest of the code, and the opposite can happen. This
method also helps to make sure that only a single process runs in the critical
section at a specific time.
PROCESS Pi
FLAG[i] = true
while( (turn != i) AND (CS is !free) )
{
wait;
}
CRITICAL SECTION FLAG[i] = false
turn = j; //choose another process to go
to CS
• Assume there are N processes (P1, P2, … PN) and every process at some point
of time requires to enter the Critical Section
• A FLAG[] array of size N is maintained which is by default false. So, whenever a
process requires to enter the critical section, it has to set its flag as true. For
example, If Pi wants to enter it will set FLAG[i]=TRUE.
• Another variable called TURN indicates the process number which is currently
waiting to enter into the CS.
• The process which enters into the critical section while exiting would change
the TURN to another number from the list of ready processes.
• Example: turn is 2 then P2 enters the Critical section and while exiting turn=3
and therefore P3 breaks out of wait loop.
Synchronization Hardware

Some times the problems of the Critical Section are also resolved by hardware.
Some operating system offers a lock functionality where a Process acquires a lock
when entering the Critical section and releases the lock after leaving it.

So when another process is trying to enter the critical section, it will not be able
to enter as it is locked. It can only do so if it is free by acquiring the lock itself.
 Mutex Locks
Synchronization hardware not simple method to implement for
everyone, so strict software method known as Mutex Locks was also
introduced.
In this approach, in the entry section of code, a LOCK is obtained over
the critical resources used inside the critical section. In the exit section
that lock is released.
 Semaphore Solution
Semaphore is simply a variable that is non-negative and shared
between threads. It is another algorithm or solution to the critical
section problem. It is a signaling mechanism and a thread that is waiting
on a semaphore, which can be signaled by another thread.
It uses two atomic operations, 1)wait, and 2) signal for the process
synchronization.
Classical Problems of Synchronization

 The classical problems of synchronization are as follows:


1. Bound-Buffer problem
2. Sleeping barber problem
3. Dining Philosophers problem
4. Readers and writers problem
Bound-Buffer problem

Also known as the Producer-Consumer problem. In this problem, there is a buffer of n


slots, and each buffer is capable of storing one unit of data. There are two processes
that are operating on the buffer – Producer and Consumer. The producer tries to insert
data and the consumer tries to remove data.

If the processes are run simultaneously they will not yield the expected output.

The solution to this problem is creating two semaphores, one full and the other empty
to keep a track of the concurrent processes.
Sleeping Barber Problem

This problem is based on a hypothetical barbershop with one barber.

When there are no customers the barber sleeps in his chair. If any customer
enters he will wake up the barber and sit in the customer chair. If there are no
chairs empty they wait in the waiting queue.
Dining Philosopher’s problem

This problem states that there are K number of philosophers sitting around a
circular table with one chopstick placed between each pair of philosophers. The
philosopher will be able to eat if he can pick up two chopsticks that are adjacent
to the philosopher.

This problem deals with the allocation of limited resources.


Readers and Writers Problem

This problem occurs when many threads of execution try to access the same
shared resources at a time. Some threads may read, and some may write. In this
scenario, we may get faulty outputs.

You might also like