Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

Process Synchronisation

Background
Critical–Section Problem
Semaphores
Classical Problems of Synchronization
Monitors

Prof. D.S.R. Murthy OS-7 Process Synchronisation 1


Background
 
Cooperating Process
Affects or affected by other processes
executing in the system.

Allowed to share data


through files.

Concurrent access to shared data


may result in data inconsistency.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 2


Background
 
Race Condition
Situation where several processes
access and manipulate shared data concurrently.

Final value of the shared data


depends upon which process finishes last.

Synchronising concurrent processes


prevents race conditions.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 3


Critical–Section Problem

Critical Section
Code segment of each process.

Accesses shared data.

When one process is executing in its critical section,


no other process is allowed to execute in its critical section.

Execution of critical sections by the processes


is mutually exclusive in time.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 4


Critical–Section Problem
Protocol design that the processes use to cooperate.
 
Requirements for 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 no. 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.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 5


Critical–Section Problem
General Structure of a typical process Pi

do {
entry section
critical section
exit section
reminder section
} while (1);

Prof. D.S.R. Murthy OS-7 Process Synchronisation 6


Critical–Section Problem
Two–Process Solutions
One Process Pi
Other Process Pj
 Algorithm 1
Shared variables:
int turn = 0;

Structure of Process Pi
do {
while (turn != i);
critical section
turn = j;
reminder section
} while (1);

Satisfies mutual exclusion, but not progress.


Prof. D.S.R. Murthy OS-7 Process Synchronisation 7
Critical–Section Problem
Algorithm 2
Shared variables
boolean flag[2] = false;

Structure of Process Pi
do {
flag [i] := true;
while (flag [j]);
critical section
flag [i] = false;
remainder section
} while (1);

Satisfies mutual exclusion, but not progress requirement.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 8


Critical–Section Problem
Algorithm 3
Combines the key ideas of algorithms 1 and 2.
Shared variables
boolean flag[2] = false;
int turn = 0;
Structure of Process Pi
do {
flag [i]:= true;
turn = j;
while (flag [j] && turn == j);
critical section
flag [i] = false;
remainder section
} while (1);
Meets all the requirements
and solves the Critical–Section problem for two processes.
Prof. D.S.R. Murthy OS-7 Process Synchronisation 9
Critical–Section Problem
Multiple–Process Solutions – Bakery Algorithm
Critical–Section problem for n processes.
Processes enter their critical section on a first-come, first-served basis.
Before entering its critical section, process receives a no.
Holder of the smallest no. enters the critical section.
If processes Pi and Pj receive the same no., if i < j, then
Pi is served first; else
Pj is served first.

The numbering scheme always generates


nos. in increasing order of enumeration.
i.e., 1,2,3,3,3,3,4,5, .…

Prof. D.S.R. Murthy OS-7 Process Synchronisation 10


Semaphores
Busy waiting / Spinlock
While a process is in its critical section,
any other process that tries to enter its critical section
must loop continuously in the entry code.

Semaphore
Synchronization tool that does not require busy waiting.
 

Semaphore S
Integer variable can only be accessed via
two indivisible (atomic) operations:
P (for wait; from the Dutch Proberen, to test)
V (for signal; from the Dutch Verhogen, to increment)

Prof. D.S.R. Murthy OS-7 Process Synchronisation 11


Semaphores
Classical definition of wait
wait (S) {
while (S  0)
; //no-op
S--;
}
 
Classical definition of signal
signal (S) {
S++;
}

Prof. D.S.R. Murthy OS-7 Process Synchronisation 12


Semaphores
Deals with Critical Section of n Processes.

Shared data:
semaphore mutex = 1
 
Structure of Process Pi:
do {
wait (mutex);
critical section
signal (mutex);
remainder section
} while (1);

Prof. D.S.R. Murthy OS-7 Process Synchronisation 13


Semaphores
Operations
block
suspends the process that invokes it.
 
wakeup(P)
resumes the execution of a blocked process P.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 14


Semaphores
Deadlocks and Starvation
Deadlock
Two or more processes waiting indefinitely for an event
that can be caused by only one of the waiting processes.
 
Let S and Q be two semaphores initialized to 1.
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
… …
signal (S); signal (Q);
signal (Q) signal (S);
 

Starvation
Indefinite blocking.
A process may never be removed from the semaphore queue
in which it is suspended.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 15


Semaphores
Types of Semaphores
Counting Semaphore
Integer value
range over an unrestricted domain.

 
Binary Semaphore
Integer value
range only between 0 and 1.

Simple to implement.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 16


Semaphores
Implementing a Counting semaphore S as a Binary semaphore
Data structures:
binary-semaphore S1, S2;
int C;

Initialization:
S1 = 1
S2 = 0
C = initial value of semaphore S

Prof. D.S.R. Murthy OS-7 Process Synchronisation 17


Semaphores
Implementing a Counting semaphore S as a Binary semaphore

wait operation on the counting semaphore S

wait (S1);
C--;
if (C < 0) {
signal (S1);
wait (S2);
}
signal (S1);

Prof. D.S.R. Murthy OS-7 Process Synchronisation 18


Semaphores
Implementing a Counting semaphore S as a Binary semaphore

Signal operation on the counting semaphore S

wait (S1);
C++;
if (C <= 0)
signal (S2);
else
signal (S1);

Prof. D.S.R. Murthy OS-7 Process Synchronisation 19


Classical Problems of Synchronization

Bounded–Buffer Problem

Readers and Writers Problem

Dining–Philosophers Problem

Prof. D.S.R. Murthy OS-7 Process Synchronisation 20


Classical Problems of Synchronization

Bounded–Buffer Problem
Used to illustrate the power of synchronisation primitives.

Consists of n buffers, each capable of holding one item.


 
Shared data
semaphore full, empty, mutex;

Initialisation:
full = 0, empty = n, mutex = 1

Prof. D.S.R. Murthy OS-7 Process Synchronisation 21


Classical Problems of Synchronization
Bounded–Buffer Problem
Structure of Producer Process

Producer produce full buffers for the Consumer.


do {

produce an item in nextp

wait (empty);
wait (mutex);

add nextp to buffer

signal (mutex);
signal (full);
} while (1);
Prof. D.S.R. Murthy OS-7 Process Synchronisation 22
Classical Problems of Synchronization
Bounded–Buffer Problem
Structure of Consumer Process

Consumer produce empty buffers for the Producer.


do {
wait (full)
wait (mutex);

remove an item from buffer to nextc

signal (mutex);
signal (empty);

consume the item in nextc

} while (1);
Prof. D.S.R. Murthy OS-7 Process Synchronisation 23
Classical Problems of Synchronization

Readers–Writers Problem
A data object (file or record) shared among several concurrent processes.

Reader processes
Interested only reading
the content of the shared object.
No adverse effects, if no. of readers access
the shared data simultaneously.

Writer processes
Interested only updating (reading and writing)
the content of the shared object.
Each process has exclusive access to
the shared data.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 24


Classical Problems of Synchronization

Readers–Writers Problem
Shared data
semaphore mutex, wrt;

Initially mutex = 1, wrt = 1, readcount = 0


 
Structure of Writer Process
wait (wrt);

writing is performed

signal (wrt);

Prof. D.S.R. Murthy OS-7 Process Synchronisation 25


Classical Problems of Synchronization

Readers–Writers Problem
Structure of Reader Process
wait (mutex);
readcount++;
if (readcount == 1)
wait (wrt);
signal (mutex);

reading is performed

wait (mutex);
readcount--;
if (readcount == 0)
signal (wrt);
signal (mutex):
Prof. D.S.R. Murthy OS-7 Process Synchronisation 26
Classical Problems of Synchronization
Dining–Philosophers Problem

Prof. D.S.R. Murthy OS-7 Process Synchronisation 27


Classical Problems of Synchronization
Dining–Philosophers Problem
Shared data
semaphore chopstick[5];
Initially all values are 1.
 
Structure of Philosopher i:
do {
wait (chopstick[i])
wait (chopstick[(i+1) % 5])

eat

signal (chopstick[i]);
signal (chopstick[(i+1) % 5]);

think

} while (1);
Prof. D.S.R. Murthy OS-7 Process Synchronisation 28
Monitors
High-level synchronisation construct.

Characterised by set of programmer-defined operations.

Allows the safe sharing of an abstract data type


among concurrent processes.

Prof. D.S.R. Murthy OS-7 Process Synchronisation 29


Monitors
Syntax of a Monitor
monitor monitor-name {
shared variable declarations
procedure body P1 (…) {
...
}
procedure body P2 (…) {
...
}
procedure body Pn (…) {
...
}
{
initialization code
}
}
Prof. D.S.R. Murthy OS-7 Process Synchronisation 30
Monitors
Schematic View of a Monitor

Prof. D.S.R. Murthy OS-7 Process Synchronisation 31


Monitors
Monitor With Condition Variables

Prof. D.S.R. Murthy OS-7 Process Synchronisation 32

You might also like