Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of synchronization method. A barrier for a group of threads or.

Similar presentations


Presentation on theme: "1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of synchronization method. A barrier for a group of threads or."— Presentation transcript:

1 1 C OMP 346 – W INTER 2015 Tutorial # 5

2 Semaphores for Barrier Sync A 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

3 Semaphores for Barrier Sync Take 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. 4/13/ semaphore s1 = 0, s2 = 0; process P1 process P2 V (s1) V (s2) P (s2) P (s1)

4 Barrier Sync for Three Processes A possible copy-cat attempt: Why it doesn’t work? 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 V (s1) V (s2) V (s3) P (s3) P (s1) P (s2)

5 Barrier Sync for Three Processes A possible copy-cat attempt: Why it doesn’t work? Scenario: 1-6, process 2 even hasn’t started! None of the requirements are met 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P V (s1) V (s2) 2 V (s3) 5 P (s3) P (s1) P (s2) 6

6 Barrier Sync for Three Processes (2) Another attempt: What’s wrong now? 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 P (s1) V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3)

7 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 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P P (s1) 5 V (s1) 2 V (s1) 9 P (s1) 6 P (s2) 3 P (s3) 10 V (s2) V (s3)

8 Barrier Sync for Three Processes (3) Last attempt: A bit “optimized”: 4/13/ semaphore s1 = 0, s2 = 0, s3 = 0; process P1 process P2 process P3 P (s1) V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3) semaphore s1 = -1, s2 = 0, s3 = 0; process P1 process P2 process P3 V (s1) V (s1) P (s1) P (s2) P (s3) V (s2) V (s3)

9 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! 4/13/201599

10 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 :-) 4/13/ semaphore s1 = -n + 2, s2 = 0; process P1 process P2... process Pn... P (s1) V (s1)... V (s1) V (s2) P (s2)... P (s2) V (s2)... V (s2)...

11 Introducing the Semaphore Class NOTE: Operations Signal and Wait are guaranteed to be atomic! 4/13/ class Semaphore { private int value; public Semaphore(int value) { this.value = value; } public Semaphore() { this(0); }...

12 Introducing the Semaphore Class (2) 4/13/ public synchronized void Wait() { while(this.value <= 0) { try { wait(); } catch(InterruptedException e) { System.out.println ( "Semaphore::Wait() …” + e.getMessage() ); e.printStackTrace(); } this.value--; }...

13 Introducing the Semaphore Class (3) 4/13/ public synchronized void Signal() { ++this.value; notify(); } public synchronized void P() { this.Wait(); } public synchronized void V() { this.Signal(); }

14 References / / barrier barrier oncurrency/sync.html oncurrency/sync.html 14


Download ppt "1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of synchronization method. A barrier for a group of threads or."

Similar presentations


Ads by Google