Parallel Programming Parallel Programming 0024 0024 Recitation Week 7 Recitation Week 7 Spring Semester 2010 Spring Semester 2010 – R 7 :: 1 – 0024 Spring 2010
Today‘s program Assignment 6 Assignment 6 Review of semaphores Review of semaphores Semaphores Semaphoren and (Java) monitors Semaphore implementation of monitors (review) Semaphore implementation of monitors (review) Assignment 7 Assignment 7 – R 7 :: 2 – 0024 Spring 2010
Semaphores Special Integer variable w/2 atomic operations Special Integer variable w/2 atomic operations P() ( Passeren , wait/up) V() ( Vrijgeven/Verhogen , signal/down ) Names of operations reflect the Dutch origin of the Names of operations reflect the Dutch origin of the inventor ... inventor ... – R 7 :: 3 – 0024 Spring 2010
class Semaphore public class Semaphore { public class Semaphore { private int value; private int value; public Semaphore() { public Semaphore() { value = 0; value = 0; } } public Semaphore(int k) { public Semaphore(int k) { value = k; value = k; } } public synchronized void P() { /* see next slide */ } public synchronized void P() { /* see next slide */ } public synchronized void V() { /* see next slide */ } public synchronized void V() { /* see next slide */ } } } – R 7 :: 4 – 0024 Spring 2010
P() operation public synchronized void P() { public synchronized void P() { while (value == 0) { while (value == 0) { try { try { wait(); wait(); } } catch (InterruptedException e) { } catch (InterruptedException e) { } } } value --; value --; } } – R 7 :: 5 – 0024 Spring 2010
V() operation public synchronized void V() { public synchronized void V() { ++value; ++value; notifyAll(); notifyAll(); } } – R 7 :: 6 – 0024 Spring 2010
Comments You can modify the value of an semphore instance only You can modify the value of an semphore instance only using the P() and V() operations. using the P() and V() operations. Initialize in constructor Effect Effect P() may block V() never blocks Application of semaphores: Application of semaphores: Mutual exclusion Conditional synchronization – R 7 :: 7 – 0024 Spring 2010
2 kinds of semaphores Binary semaphore Binary semaphore Value is either 0 or 1 Supports implemention of mutual exclusion Semaphore s = new Semaphore(1); s.p() //critical section s.v() Counting (general) semaphore Counting (general) semaphore Value can be any positive integer value – R 7 :: 8 – 0024 Spring 2010
Fairness A semaphore is considered to be “fair“ if all threads A semaphore is considered to be “fair“ if all threads that execute a P() operation eventually succeed. that execute a P() operation eventually succeed. Different models of fairness – Different models of fairness – Semaphore is “unfair”: a thread blocked in the P() Semaphore is “unfair”: a thread blocked in the P() operation must wait forever while other threads (that operation must wait forever while other threads (that executed the operation later) succeed. executed the operation later) succeed. – R 7 :: 9 – 0024 Spring 2010
Semaphores in Java java.util.concurrent.Semaphore java.util.concurrent.Semaphore acquire() instead of instead of P() acquire() P() release() instead of instead of V() release() V() Constructors Constructors Semaphore(int permits); Semaphore(int permits); Semaphore(int permits, boolean fair); Semaphore(int permits, boolean fair); permits: initial value fair: if true then the semphore uses a FIFO to manage blocked threads – R 7 :: 10 – 0024 Spring 2010
Semaphores and monitors Monitor: model for synchronized methods in Java Monitor: model for synchronized methods in Java Both constructs are equivalent Both constructs are equivalent One can be used to implement the other One can be used to implement the other – R 7 :: 11 – 0024 Spring 2010
Example from Mar 18 See slides for context – R 7 :: 12 – 0024 Spring 2010
Buffer using condition queues class BoundedBuffer extends Buffer { public BoundedBuffer(int size) { super(size); } public synchronized void insert(Object o) throws InterruptedException { while (isFull()) { wait(); } doInsert(o); notifyAll(); } – R 7 :: 13 – 0024 Spring 2010
Buffer using condition queues, continued // in class BoundedBuffer public synchronized Object extract() throws InterruptedException { while (isEmpty()) { wait(); } Object o = doExtract(); notifyAll(); return o; }// extract } // BoundedBuffer – R 7 :: 14 – 0024 Spring 2010
Emulation of monitor w/ semaphores We need 2 semaphores: We need 2 semaphores: One to make sure that only one synchronized method executes One to make sure that only one synchronized method executes at any tiven time at any tiven time call this the “access semaphore” (access) call this the “access semaphore” (access) binary semaphore binary semaphore One semaphore to line up threads that are waiting for some One semaphore to line up threads that are waiting for some condition condition call this the “condition semaphore” (cond) – also binary call this the “condition semaphore” (cond) – also binary threads that wait must do an “acquire” and this will not complete threads that wait must do an “acquire” and this will not complete For convenience: For convenience: Counter waitThread to count number of waiting threads i.e., threads in queue for cond – R 7 :: 15 – 0024 Spring 2010
Basic idea 1) Frame all synchronized methods with access.acquire() and Frame all synchronized methods with access.acquire() and 1) access.release() access.release() This ensures that only one thread executes a synchronized This ensures that only one thread executes a synchronized method at any point in time method at any point in time Recall access is binary. Recall access is binary. 1) Translate wait() and notifyAll() to give threads waiting in line Translate wait() and notifyAll() to give threads waiting in line 1) a chance to progress (these threads use cond) a chance to progress (these threads use cond) To simplify the debate, we require that “notifyAll()” is the To simplify the debate, we require that “notifyAll()” is the last action in a synchronized method last action in a synchronized method Java does not enforce this requirement but the mapping Java does not enforce this requirement but the mapping of synchronized methods into semaphores is simplified. of synchronized methods into semaphores is simplified. – R 7 :: 16 – 0024 Spring 2010
Buffer with auxiliary fields class BoundedBuffer extends Buffer { public BoundedBuffer(int size) { super(size); access = new Semaphore(1); cond = new Semaphore(0); } private Semaphore access; private Semaphore cond; private int waitThread = 0; // continued ... } – R 7 :: 17 – 0024 Spring 2010
1) Framing all methods public void insert(Object o) throws InterruptedException { access.acquire(); //ensure mutual exclusion while (isFull()) { wait(); } doInsert(o); notifyAll(); access.release(); } // insert – R 7 :: 18 – 0024 Spring 2010
Notes There is one semaphore for all all synchronized methods synchronized methods There is one semaphore for of one instance of “BoundedBuffer” of one instance of “BoundedBuffer” Why? Why? Must make sure that insert and extract don’t overlap. Must make sure that insert and extract don’t overlap. There must be separate semaphore to deal with waiting There must be separate semaphore to deal with waiting threads. threads. Why? Why? Imagine the buffer is full. A thread that attempts to insert Imagine the buffer is full. A thread that attempts to insert an item must wait. But it must release the access semaphore. an item must wait. But it must release the access semaphore. Otherwise a thread that wants to remove an item will not be Otherwise a thread that wants to remove an item will not be able to executed the (synchronized!) extract method. able to executed the (synchronized!) extract method. – R 7 :: 19 – 0024 Spring 2010
2) Translate wait and notifyAll The operation The operation wait() wait() is translated into is translated into waitThread++ waitThread++ access.release(); // other threads can execute methods access.release(); // other threads can execute methods cond.acquire(); // wait until condition changes cond.acquire(); // wait until condition changes access.acquire(); access.acquire(); waitThread-- waitThread-- – R 7 :: 20 – 0024 Spring 2010
Each occurrrence of NotifyAll() is translated to Each occurrrence of NotifyAll() is translated to for (int i=0; i < waitThread; i++) { for (int i=0; i < waitThread; i++) { cond.release(); cond.release(); } } All threads waiting are released and will compete to All threads waiting are released and will compete to (re)acquire access. (re)acquire access. They decrement waitThread after they leave cond.acquire They decrement waitThread after they leave cond.acquire Note that to enter the line (i.e., increment waitThread) the Note that to enter the line (i.e., increment waitThread) the thread must hold the access semaphore access thread must hold the access semaphore access – R 7 :: 21 – 0024 Spring 2010
Recommend
More recommend