0% found this document useful (0 votes)
7 views12 pages

Operating System L 4

Uploaded by

Ajit Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Operating System L 4

Uploaded by

Ajit Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Operating Systems

Dr.Sohail Saif
Assistant Professor
Department of Computer Applications
Maulana Abul Kalam Azad University of Technology, West Bengal
Process Synchronization
Process Synchronization is the coordination of execution of multiple
processes in a multi-process system to ensure that they access shared
resources in a controlled and predictable manner. It aims to resolve the
problem of race conditions and other synchronization issues in a
concurrent system.

The main objective of process synchronization is to ensure that multiple


processes access shared resources without interfering with each other.
Producer-Consumer Problem
The Producer-Consumer problem is a classical multi-process
synchronization problem, that is we are trying to achieve synchronization
between more than one process.

There is one Producer in the producer-consumer problem, Producer is


producing some items, whereas there is one Consumer that is consuming
the items produced by the Producer. The same memory buffer is shared
by both producers and consumers which is of fixed-size.

The task of the Producer is to produce the item, put it into the memory
buffer, and again start producing items. Whereas the task of the
Consumer is to consume the item from the memory buffer.
Let's understand what is the problem?
Below are a few points that considered as the problems occur in Producer-
Consumer:

The producer should produce data only when the buffer is not full. In
case it is found that the buffer is full, the producer is not allowed to store
any data into the memory buffer.

Data can only be consumed by the consumer if and only if the memory
buffer is not empty. In case it is found that the buffer is empty, the
consumer is not allowed to use any data from the memory buffer.

Accessing memory buffer should not be allowed to producer and


consumer at the same time.
Race Condition
When more than one process is 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.
p() x=10; P1 P2 P1 P2 P1
{ P1=11 P1=10
Read(x) P2=12 P2=11
Critical Section x++; P1=11
Write(x)
} Case: No Preemption Case: Preemption
Critical Section Problem
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
Process may be changing common variables, updating table, writing file, etc
When one process in critical section, no other may be in its critical section
 No two processes should execute in their critical sections at the same time.
Critical section problem is to design protocol to design a protocol that the
processes can use to cooperate
Each process must ask permission to enter critical section in entry section,
may follow critical section with exit section, then remainder section
Critical Section Problem
//Each process must request permission to enter its critical section

//The section of code implementing this request is the entry section

//The critical section may be followed by an exit section

//The remaining code is the remainder section


Solution to Critical-Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical section, then
no other processes can be executing in their critical sections

2. Progress - If no process is executing in its critical section and there exist


some processes that wish to enter their critical section, then the selection of
the processes that will enter the critical section next cannot be postponed
indefinitely

3. Bounded Waiting - A bound must exist on the number of times that


other processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is granted
Semaphores
Semaphores are just normal variables used to coordinate the activities of
multiple processes in a computer system. They are used to enforce mutual
exclusion, avoid race conditions and implement synchronization between
processes.

The process of using Semaphores provides two operations: wait (P) and signal
(V).

The wait operation decrements the value of the semaphore, and the signal
operation increments the value of the semaphore

Semaphores are used to implement critical sections, which are regions of code
that must be executed by only one process at a time
Binary Semaphore
This is also known as a mutex lock. It can have only two values – 0 and 1. Its
value is initialized to 1. It is used to implement the solution of critical section
problems with multiple processes.

wait (s) signal (s)


{ {
while (s<=0); s=s+1
s=s-1 }
}
Advantages of Semaphores
A simple and effective mechanism for process synchronization
Supports coordination between multiple processes
Provides a flexible and robust way to manage shared resources.
It can be used to implement critical sections in a program.
It can be used to avoid race conditions.
Disadvantages of Semaphores
It Can lead to performance degradation due to overhead associated with wait
and signal operations.
Can result in starvation and deadlock if used incorrectly.

P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
. .
. .
. .
signal(S); signal(Q);
signal(Q); signal(s);

You might also like