1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of...
-
Upload
hamza-kitching -
Category
Documents
-
view
213 -
download
0
Transcript of 1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of...
Semaphores for Barrier SyncA barrier is a type of
synchronization method. A barrier for a group of threads or processes in the source code means any thread/process must stop at this point and cannot proceed until all other threads/processes reach this barrier.
2
Semaphores for Barrier SyncTake a look at the typical problem:
all processes must finish their phase I before any of them starts phase II
processes must proceed to their phase II in a specific order, for example: 1, 2, 3…
This is called barrier synchronization.
04/18/23 3 3
semaphore s1 = 0, s2 = 0; process P1 process P2 <phase I> <phase I> V (s1) V (s2) P (s2) P (s1) <phase II> <phase II>
Barrier Sync for Three ProcessesA possible copy-cat attempt:
Why it doesn’t work?
04/18/23 4 4
semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 <phase I> <phase I> <phase I> V (s1) V (s2) V (s3) P (s3) P (s1) P (s2) <phase II> <phase II> <phase II>
Barrier Sync for Three ProcessesA possible copy-cat attempt:
Why it doesn’t work?Scenario: 1-6 , process 2 even hasn’t started!None of the requirements are met
04/18/23 5 5
semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P33 <phase I> <phase I> 1 <phase I>4 V (s1) V (s2) 2 V (s3)5 P (s3) P (s1) P (s2)6 <phase II> <phase II> <phase II>
Barrier Sync for Three Processes (2)Another attempt:
What’s wrong now?
04/18/23 6 6
semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 <phase I> <phase I> <phase I> P (s1) V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3) <phase II> <phase II> <phase II>
Barrier Sync for Three Processes (2)Another attempt:
What’s wrong now?Scenario: 1-10, so far so good, but after…The second requirement isn’t met
04/18/23 7 7
semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P37 <phase I> 4 <phase I> 1 <phase I>8 P (s1) 5 V (s1) 2 V (s1)9 P (s1) 6 P (s2) 3 P (s3)10 V (s2) V (s3) <phase II> <phase II> <phase II>
Barrier Sync for Three Processes (3)Last attempt:
A bit “optimized”:
04/18/23 8 8
semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 <phase I> <phase I> <phase I> P (s1) V (s1) V (s1) P (s1) P (s2) P (s3) <phase II> <phase II> <phase II> V (s2) V (s3)
semaphore s1 = -1, s2 = 0, s3 = 0; process P1 process P2 process P3 <phase I> <phase I> <phase I> V (s1) V (s1) P (s1) P (s2) P (s3) <phase II> <phase II> <phase II> V (s2) V (s3)
Barrier Sync: Need for the General Solution
Problem with the proposed solution: # of semaphores == # of processes.
Semaphores as any other resource are limited and take space => overhead
Imagine you need to sync 10 processes in the same manner? 100? 1000?Complete mess and a high possibility of a
deadlock!
04/18/23 9 9
Barrier Sync: Need for the General Solution (2)
Attempt for the first requirement:
The second requirement is left as an exercise to the curious student :-)
04/18/23 10 10
semaphore s1 = -n + 2, s2 = 0; process P1 process P2 ... process Pn <phase I> <phase I> ... <phase I> P (s1) V (s1) ... V (s1) V (s2) P (s2) ... P (s2) V (s2) ... V (s2) <phase II> <phase II> ... <phase II>
Introducing the Semaphore ClassNOTE: Operations Signal and Wait are guaranteed to be atomic!
04/18/23 11 11
class Semaphore{
private int value;
public Semaphore(int value){
this.value = value;}
public Semaphore(){
this(0);}
...
Introducing the Semaphore Class (2)
04/18/23 12 12
...public synchronized void Wait(){
while(this.value <= 0){
try{
wait();}catch(InterruptedException e){
System.out.println(
"Semaphore::Wait() …” + e.getMessage()
);
e.printStackTrace();}
}
this.value--;}
...
Introducing the Semaphore Class (3)
04/18/23 13 13
...public synchronized void Signal(){
++this.value;notify();
}
public synchronized void P(){
this.Wait();}
public synchronized void V(){
this.Signal();}
}