5process Synchronization
5process Synchronization
critical section
exit section
reminder section
} while (TRUE);
P1 P2
do { do {
P1 P2
do { do {
while(flag[2]); while(flag[1]);
flag[1] = TRUE; flag[2] = TRUE;
P1 P2
do{ do{
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{
number[i] = max(number[0:n-1])+1
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)
critical section
number[i]=0;
reminder section
} while(TRUE)
critical section
Progress: YES
Bounded waiting: NO
lock = FALSE;
reminder section
} while(TRUE)
reminder section
} while(TRUE)
Pi Pj
do{ do{
keyi = TRUE; keyj = TRUE;
while(keyi) while(keyj)
Swap(&lock, &keyi); Swap(&lock, &keyj);
critical section critical section
lock = FALSE; lock = FALSE;
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 27
Counting Semaphore
Useful when we have multiple instances of same
shared resource.
Initialized to the number of instances of the
resource. (e.g. printer)
ES
:Y
Add Pi to countSem->List;
d w ES on
ES
:Y
de : Y s i
block();
un ss clu
ng
Bo ogre al ex
aiti
}
Pr utu
critical section
M
countSem.value++;
if(countSem.value <= 0){
Remove process Pj from countSem->List;
wakeup(Pj);
}
reminder section
} while(TRUE)
tail head
NIT Rourkela Dr. Manmath N. Sahoo (CS) 31
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);
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)
37
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) 38
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
} while(TRUE) reminder section
} while(TRUE)
Office
Integer rcount
Semaphore rlock
Semaphore wSem
Semaphore order_mutuex
NIT Rourkela 41
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)