Process Synchronization-1
Process Synchronization-1
Process
Synchronization
Interprocess Communication 12-2
Any solution to the critical section problem must satisfy three requirements:
Mutual Exclusion: If a process is executing in its critical section, then no
other process is allowed to execute in the critical section.
Progress : If no process is executing in the critical section and other
processes are waiting outside the critical section, then only those
processes that are not executing in their remainder section can participate
in deciding which will enter in the critical section next, and the selection
cannot be postponed indefinitely.
Bounded Waiting: A bound must exist on the number 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.
Two process solution for critical
section 12-9
P0 P1
while(1) while(1)
{ {
While(turn!=0); While(turn!=1);
Critical Section
Critical Section
Turn=1; Turn=0;
Remainder
RemainderSection
Section Remainder Section
}
}
12-10
sequentially.
Two process solution for critical
section 12-11
F F
Second Solution: Boolean Flag[2]=
0 1
P0 P1
while(1) while(1)
{ {
Flag[0]= T; Flag[1]= T;
while(flag[1]); while(flag[0]);
Critical Section Critical Section
flag[0]=F; flag[1]=F;
} }
Peterson’s Solution 12-12
int turn : The process whose turn is to enter the critical section.
Peterson’s Solutions 12-13
i = 1-j
Two process solution for critical
section 12-14
F F
Second Solution: Boolean Flag[2]=
0 1
P0 P1
while(1) while(1)
{ {
Flag[0]= T; Flag[1]= T;
Turn=1; Turn=0
while(turn==1 &&flag[1]==T); while(turn==0 &&flag[0]==T);
Critical Section Critical Section
flag[0]=F; flag[1]=F;
Remainder Section Remainder Section
} }
Semaphore 12-15
Shared data:
semaphore mutex; //initially mutex = 1
Semaphore synch0;
_______________________
S1;
Signal(synch);
_______________________
Wait(synch);
S2;
________________________
12-19
Classical Problems of Synchronization
Producer Consumer with Bounded-Buffer Problem
Dining-Philosophers Problem
do { do {
… wait(full)
produce an item in wait(mutex);
…
… remove an item from buffer to
wait(empty); nextc
wait(mutex); …
… signal(mutex);
add nextp to buffer signal(empty);
… …
signal(mutex); consume the item in nextc
signal(full); …
} while (1); } while (1);
Readers-Writers Problem 12-22
Shared data
semaphore chopstick[5];
Initially all values are 1
Dining-Philosophers Problem 12-24
Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])
…
eat
…
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
…
think
…
} while (1);
12-25