Professional Documents
Culture Documents
Background Critical-Section Problem Semaphores Classical Problems of Synchronization Monitors
Background Critical-Section Problem Semaphores Classical Problems of Synchronization Monitors
Background
Critical–Section Problem
Semaphores
Classical Problems of Synchronization
Monitors
Critical Section
Code segment of each process.
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.
do {
entry section
critical section
exit section
reminder section
} while (1);
Structure of Process Pi
do {
while (turn != i);
critical section
turn = j;
reminder section
} while (1);
Structure of Process Pi
do {
flag [i] := true;
while (flag [j]);
critical section
flag [i] = false;
remainder section
} while (1);
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)
Shared data:
semaphore mutex = 1
Structure of Process Pi:
do {
wait (mutex);
critical section
signal (mutex);
remainder section
} while (1);
Starvation
Indefinite blocking.
A process may never be removed from the semaphore queue
in which it is suspended.
Binary Semaphore
Integer value
range only between 0 and 1.
Simple to implement.
Initialization:
S1 = 1
S2 = 0
C = initial value of semaphore S
wait (S1);
C--;
if (C < 0) {
signal (S1);
wait (S2);
}
signal (S1);
wait (S1);
C++;
if (C <= 0)
signal (S2);
else
signal (S1);
Bounded–Buffer Problem
Dining–Philosophers Problem
Bounded–Buffer Problem
Used to illustrate the power of synchronisation primitives.
Initialisation:
full = 0, empty = n, mutex = 1
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.
Readers–Writers Problem
Shared data
semaphore mutex, wrt;
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