5Process Synchronization
5Process Synchronization
critical section
exit section
reminder section
} while (TRUE);
P1 P2
do { do {
} while(TRUE) } while(TRUE)
P1 P2
do { do {
flag[1] = TRUE; flag[2] = TRUE;
while(flag[2]); while(flag[1]);
Progress: Satisfied.
Bounded waiting: Not Satisfied
P1 may continuously enter into the critical section even
when P2 is waiting in its while loop
Pi
do{
number[i] = max(number[0:n-1])+1
for j = 0 to n-1 {
while( number[j] && (number[j] < number[i] );
}
critical section
number[i]=0;
reminder section
} while(TRUE)
NIT Rourkela Numbers of multiple processes may be same => mutual exclusion will be violated14
Shared variables
int number[0:n-1]; //initialized to 0
Pi
do{
for j = 0 to n-1 {
while( number[j] && (number[j],pid[j]) < (number[i],pid[i]) );
}
critical section
number[i]=0;
reminder section
} while(TRUE)
number[i]=0;
reminder section
Critical section
lock = false
…reminder section
}
critical section
EI
reminder section
} while(TRUE)
critical section
Progress: YES
Bounded waiting: NO
lock = FALSE;
reminder section
} while(TRUE)
critical section
j = (i+1) % n;
while(j!=i && waiting[j]==FALSE)
j = (j+1) % n;
if(j==i)
lock = FALSE;
else waiting[j] = FALSE;
reminder section
} while(TRUE)
NIT Rourkela Dr. Manmath N. Sahoo (CS) 22
Swap Instruction
Swap instruction - defined below as if it were a
function
boolean Swap (boolean *a, *b){
boolean temp = *a;
*a = *b;
*b = temp;
}
Pi Pj
do{ do{
keyi = TRUE; keyj = TRUE;
while(keyi) while(keyj)
Swap(&lock, &keyi); Swap(&lock, &keyj);
critical section
j = (i+1) % n;
while(j!=i && waiting[j]==FALSE)
j = (j+1) % n;
if(j==i)
lock = FALSE;
else waiting[j] = FALSE;
reminder section
} while(TRUE)
NIT Rourkela Dr. Manmath N. Sahoo (CS) 25
Semaphore
A synchronization primitive proposed by
Dijkstra in 1968.
Consists of an integer value
Two operations
P(S) or wait(S): waits for semaphore to become positive
V(S) or signal(S): increments semaphore by 1
P(S) and V(S) operations are atomic.
Two Types: (i) Binary (ii) Counting
critical section
reminder section
} while(TRUE)
NIT Rourkela 28
Counting Semaphore
Useful when we have multiple instances of same
shared resource.
Initialized to the number of instances of the
resource. (e.g. printer)
} while(TRUE)
critical section
countSem.value++;
if(countSem.value <= 0){
Remove process Pj from countSem->List;
wakeup(Pj);
}
reminder section
} while(TRUE)
NIT Rourkela Dr. Manmath N. Sahoo (CS) 31
Bounded Buffer Problem
AKA “producer/consumer” problem
there is a buffer in memory with N entries
producer threads insert entries into it (one at a time)
consumer threads remove entries from it (one at a time)
tail head
NIT Rourkela Dr. Manmath N. Sahoo (CS) 32
Bounded Buffer Problem
Constraints
The consumer must wait if buffers are empty
(synchronization constraint)
The producer must wait if buffers are full (synchronization
constraint)
Only one thread can manipulate the buffer at a time
(mutual exclusion)
Producer Consumer
P(nFreeBuffers); P(nLoadedBuffers);
P(mutex); P(mutex);
// put 1 item in the buffer // take 1 item from buffer
V(mutex); V(mutex);
V(nLoadedBuffers); V(nFreeBuffers);
P0
Ri
do{
P(rlock);
rcount++;
if (rcount==1) P(wSem);
V(rlock); Wi
READ do{
P(rlock); P(wSem);
rcount--;
WRITE
if (rcount==0) V(wSem);
V(rlock); V(wSem);
} while(TRUE) } while(TRUE)
38
Readers Writers Problem: Solution 1 –
Preference to Readers
Readers only
All readers are allowed to READ
Writers only
One writer at a time
Both readers and writers with read first
Writer has to wait on P(wrt)
Both readers and writers with write first
Reader has to wait on P(wrt)
Writers may starve !
NIT Rourkela Dr. Manmath N. Sahoo (CS) 39
Readers Writers Problem: Solution 2 –
Preference to Writers
Integer rcount – keeps track of number of readers.
Integer wcount – keeps track of number of writers.
Semaphore rlock – controls the updating of rdcount.
Semaphore wlock – controls the updating of wrtcount.
Semaphore rSem – inhibits all readers while there is at least one writer
desiring access to critical section.
Semaphore wSem – inhibits all writers while there is at least one reader
desiring access to critical section.
Semaphore rQueue – to avoid long queue on rSem. So that waiting
writer processes get preference.
Ri Wi
do{ do{
P(rQueue); P(wlock);
P(rSem); wcount++;
P(rlock); if(wcount == 1) P(rSem);
rcount++; V(wlock);
if(rcount == 1) P(wSem);
V(rlock); P(wSem);
V(rSem);
V(rQueue); WRITE
READ V(wSem);
P(rlock); P(wlock);
rcount--; wcount--;
if (rcount == 0) V(wSem); if (wcount == 0) V(rSem);
V(rlock); V(wlock);
reminder section reminder section
} while(TRUE) } while(TRUE)
Office
Integer rcount
Semaphore rlock
Semaphore wSem
Semaphore order_mutuex
NIT Rourkela 42
Readers Writers Problem: Solution 3 –
Based on the arrival order
Ri Wi
do{ do{
P(order_mutex);
P(order_mutex);
P(rlock); P(wSem));
rcount++; V(order_mutex);
if (rcount==1) WRITE
P(wSem));
V(rlock); V(wSem);
V(order_mutex);
} while(TRUE)
READ
P(rlock);
rcount--;
if (rcount==0)
V(wSem);
V(rlock);
} while(TRUE)