OS Process Synchronization Unit 3
OS Process Synchronization Unit 3
OS Process Synchronization Unit 3
Background
Co-Operating Process: that can affect or be affected by other
processes executing in system
Concurrent access to shared data may result in data inconsistency
Process Synchronization: Ensures coordination among processes and
maintains Data Consistency
Process P1 Process P2
1. X=5
2. X=5+2
1. read(x);
2. x=x+5;
3. Printf( x);
Producer Consumer Problem
There can be two situations:
2. Bounded Buffer:
1. Limited buffer size
Producer Consumer Problem
Bounderd Buffer:
while (true) {
while (count == 0) // buffer empty
{
; // do nothing
}
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count- -;
Process P1 Process P2
1. reads i=10
2. i=i+1 =11
Remainder Section
} until false.
Solution to: Critical Section
1. Mutual Exclusion:
It states that if one process is executing in its critical section, then
no other process can execute in its critical section.
2. Bounded Wait:
It states that if a process makes a request to enter its critical
section and before that request is granted, there is a limit on
number of times other processes are allowed to enter that
critical section.
3. Progress:
It states that process cannot stop other process from entering
their critical sections, if it is not executing in its CS.
Peterson’s Solution
• Two process solution (Software-based)
• The two processes share two variables:
– int turn;
– Boolean flag[2]
• The variable turn indicates whose turn it is
to enter the critical section.
• The flag array is used to indicate if a
process is ready to enter the critical section.
flag[i]=true implies that process Pi is ready!
• Only 2 processes,
• General structure of process Pi (other process Pj)
do {
entry section
critical section
exit section
reminder section
} while (1);
Algorithm for Process Pi
do {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
} while (TRUE);
Critical Section Problem solution
Repeat
Disable interrupts
C.S
Enable interrupts
Remainder section
Synchronization Hardware
return rv;
}
23
Mutual Exclusion with Test-and-Set
• Shared data:
boolean lock = false;
• Process Pi
do {
while (TestAndSet(lock)) ;
critical section
lock = false;
remainder section
}
Synchronization Hardware
• Process Pi
do {
key = true;
while (key == true)
Swap(lock, key);
critical section
lock = false;
remainder section
}
Semaphore
Synchronization tool that maintains concurrency using variables
Semaphore S is a integer variable which can take positive values
including 0. It is accessed from 2 operations only.
Operations On Semaphore:
wait() and signal()
Types of Semaphores:
Counting semaphore – when integer value can be
any non-negative value
Binary semaphore – integer value can range only
between 0 and 1
Also known as mutex locks
Semaphore and Busy Waiting
Two operations:
block – place the process invoking the operation on the
waiting queue.
wakeup – remove one of processes in the waiting queue and
place it in the ready queue.
Semaphore Implementation with no Busy
waiting
Implementation of wait:
S=1
wait (S){
value--;
if (value < 0) {
add process P to waiting queue
block();
}
C.S
}
Semaphore Implementation with no Busy
waiting
Implementation of signal:
Signal (S){
value++; //“value” has -ve value i.e -1 here
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
Deadlocks and Starvation
The using of semaphores may cause deadlocks
P0 (Initially, A=B=1) P1 wait(B);
wait(A);
wait(B); wait(A);
S0 deadlock S1
signal(A); signal(B);
signal(B); signal(A);
wait(mutex); ...
typing error
... critical section
(deadlock)
critical section ...
... signal(mutex);
wait(mutex);
Classical Problems of Synchronization
Bounded-Buffer Problem
Dining-Philosophers Problem
Readers-Writers Problem
A data set is shared among a number of concurrent
processes
Readers – only read the data set; they do not perform
any updates
Writers – can both read and write.
Shared Data
Data set
For Readers: Semaphore mutex initialized to 1.
For Writers: Semaphore wrt initialized to 1.
Integer readcount initialized to 0.
Readers-Writers Problem (Cont.)
while (true) {
wait (wrt) ;
// writing is performed
signal (wrt) ;
}
Readers-Writers Problem (Cont.)
The structure of a reader process
while (true) {
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ; //( don’t allow writers)
signal (mutex); //(allow other readers to come)
// reading is performed
philosopher i
get chopsticks
do {
left
wait(chopstick[i]);
right
wait(chopstick[(i+1) % 5]);
...
eat
... free chopsticks
left
signal(chopstick[i]);
signal (chopstick[(i+1) % 5]); right
...
think
deadlock !
...
} while(1);
Dining-Philosophers Problem
Possible solutions to the deadlock problem
Allow at most four philosophers to be sitting
simultaneously at the table.
Allow a philosopher to pick up her chopsticks only if
both chopsticks are available (note that she
must pick them up in a critical section).
Use an asymmetric solution; that is,
odd philosopher: left first, and then right
an even philosopher: right first, and then left
Besides deadlock, any satisfactory solution to
the DPP problem must avoid the problem of
starvation.
Monitors
Schematic view of a Monitor
synchronization schemes.
condition x, y;
DiningPhilosophers
dp.pickup (i)
EAT
dp.putdown (i)
Solution to Dining Philosophers
monitor DP
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
initialization_code() {
for (int i = 1; i <= 5; i++)
state[i] = THINKING;
}
}