Parallel Programming 0024 Recitation Week 7 Spring Semester

  • Slides: 35
Download presentation
Parallel Programming 0024 Recitation Week 7 Spring Semester 2010 – R 7 : :

Parallel Programming 0024 Recitation Week 7 Spring Semester 2010 – R 7 : : 1 – 0024 Spring 2010

Today‘s program Assignment 6 Review of semaphores n n Semaphores Semaphoren and (Java) monitors

Today‘s program Assignment 6 Review of semaphores n n Semaphores Semaphoren and (Java) monitors Semaphore implementation of monitors (review) Assignment 7 – R 7 : : 2 – 0024 Spring 2010

Semaphores Special Integer variable w/2 atomic operations n P() (Passeren, wait/up) n V() (Vrijgeven/Verhogen,

Semaphores Special Integer variable w/2 atomic operations n P() (Passeren, wait/up) n V() (Vrijgeven/Verhogen, signal/down) Names of operations reflect the Dutch origin of the inventor. . . – R 7 : : 3 – 0024 Spring 2010

class Semaphore public class Semaphore { private int value; public Semaphore() { value =

class Semaphore public class Semaphore { private int value; public Semaphore() { value = 0; } public Semaphore(int k) { value = k; } public synchronized void P() { /* see next slide */ } public synchronized void V() { /* see next slide */ } } – R 7 : : 4 – 0024 Spring 2010

P() operation public synchronized void P() { while (value == 0) { try {

P() operation public synchronized void P() { while (value == 0) { try { wait(); } catch (Interrupted. Exception e) { } } value --; } – R 7 : : 5 – 0024 Spring 2010

V() operation public synchronized void V() { ++value; notify. All(); } – R 7

V() operation public synchronized void V() { ++value; notify. All(); } – R 7 : : 6 – 0024 Spring 2010

Comments You can modify the value of an semphore instance only using the P()

Comments You can modify the value of an semphore instance only using the P() and V() operations. n Initialize in constructor Effect n n P() may block V() never blocks Application of semaphores: n n – R 7 : : 7 – Mutual exclusion Conditional synchronization 0024 Spring 2010

2 kinds of semaphores Binary semaphore n Value is either 0 or 1 n

2 kinds of semaphores Binary semaphore n Value is either 0 or 1 n Supports implemention of mutual exclusion Semaphore s = new Semaphore(1); s. p() //critical section s. v() Counting (general) semaphore n – R 7 : : 8 – Value can be any positive integer value 0024 Spring 2010

Fairness A semaphore is considered to be “fair“ if all threads that execute a

Fairness A semaphore is considered to be “fair“ if all threads that execute a P() operation eventually succeed. Different models of fairness – Semaphore is “unfair”: a thread blocked in the P() operation must wait forever while other threads (that executed the operation later) succeed. – R 7 : : 9 – 0024 Spring 2010

Semaphores in Java java. util. concurrent. Semaphore acquire() instead of P() release() instead of

Semaphores in Java java. util. concurrent. Semaphore acquire() instead of P() release() instead of V() Constructors Semaphore(int permits); Semaphore(int permits, boolean fair); n permits: initial value n 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 Both constructs are equivalent

Semaphores and monitors Monitor: model for synchronized methods in Java Both constructs are equivalent 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

Example from Mar 18 See slides for context – R 7 : : 12 – 0024 Spring 2010

Buffer using condition queues class Bounded. Buffer extends Buffer { public Bounded. Buffer(int size)

Buffer using condition queues class Bounded. Buffer extends Buffer { public Bounded. Buffer(int size) { super(size); } public synchronized void insert(Object o) throws Interrupted. Exception { while (is. Full()) wait(); do. Insert(o); notify. All(); }// insert – R 7 : : 13 – 0024 Spring 2010

Buffer using condition queues, continued // in class Bounded. Buffer public synchronized Object extract()

Buffer using condition queues, continued // in class Bounded. Buffer public synchronized Object extract() throws Interrupted. Exception { while (is. Empty()) wait(); Object o = do. Extract(); notify. All(); return o; }// extract } // Bounded. Buffer – R 7 : : 14 – 0024 Spring 2010

Emulation of monitor w/ semaphores We need 2 semaphores: One to make sure that

Emulation of monitor w/ semaphores We need 2 semaphores: One to make sure that only one synchronized method executes at any tiven time call this the “access semaphore” (S) binary semaphore One semaphore to line up threads that are waiting for some condition call this the “condition semaphore” (SCond) – also binary threads that wait must do an “acquire” and this will not complete For convenience: n – R 7 : : 15 – n Counter wait. Thread to count number of waiting threads 0024 Spring 2010 i. e. , threads in queue for SCond

Basic idea 1) Frame all synchronized methods with S. acquire() and S. release() This

Basic idea 1) Frame all synchronized methods with S. acquire() and S. release() This ensures that only one thread executes a synchronized method at any point in time Recall S is binary. 2) Translate wait() and notify. All() to give threads waiting in line a chance to progress (these threads use SCond) To simplify the debate, we require that “notify. All()” is the last action in a synchronized method Java does not enforce this requirement but the mapping of synchronized methods into semaphores is simplified. – R 7 : : 16 – 0024 Spring 2010

Buffer with auxiliary fields class Bounded. Buffer extends Buffer { public Bounded. Buffer(int size)

Buffer with auxiliary fields class Bounded. Buffer extends Buffer { public Bounded. Buffer(int size) { super(size); access = new Semaphore(1); cond = new Semaphore(0); } private Semaphore access; private Semaphore cond; private int wait. Thread = 0; // continued – R 7 : : 17 – 0024 Spring 2010

1) Framing all methods public void insert(Object o)throws Interrupted. Exception access. acquire(); //ensure mutual

1) Framing all methods public void insert(Object o)throws Interrupted. Exception access. acquire(); //ensure mutual exclusion while (is. Full()) wait(); do. Insert(o); notify. All(); access. release(); }// insert – R 7 : : 18 – 0024 Spring 2010 {

Notes There is one semaphore for all synchronized methods of one instance of “Bounded.

Notes There is one semaphore for all synchronized methods of one instance of “Bounded. Buffer” Why? Must make sure that insert and extract don’t overlap. There must be separate semaphore to deal with waiting threads. Why? Imagine the buffer is full. A thread that attempts to insert an item must wait. But it must release the access semaphore. Otherwise a thread that wants to remove an item will not be able to executed the (synchronized!) extract method. – R 7 : : 19 – 0024 Spring 2010

2) Translate wait and notify. All The operation wait() is translated into wait. Thread

2) Translate wait and notify. All The operation wait() is translated into wait. Thread ++ S. release(); // other threads can execute synchronized methods SCond. acquire(); // wait until condition changes S. acquire(); wait. Thread -- – R 7 : : 20 – 0024 Spring 2010

Each occurrrence of Notify. All() is translated if (wait. Thread > 0) { for

Each occurrrence of Notify. All() is translated if (wait. Thread > 0) { for (int i=0; i< wait. Thread; i++) { SCond. release(); } } All threads waiting are released and will compete to (re)acquire S. They decrement wait. Thread after they leave SCond. acquire Note that to enter the line (i. e. , increment wait. Thread) the thread must hold the access semaphore S – R 7 : : 21 – 0024 Spring 2010

Recall that S. release is done at the end of the synchronized method So

Recall that S. release is done at the end of the synchronized method So all the threads that had lined up waiting for SCond compete to get access to S No thread can line up while the SCond. release operations are done since this thread holds S. – R 7 : : 22 – 0024 Spring 2010

Note We wake up only all threads – they might not be able to

Note We wake up only all threads – they might not be able to enter their critical section if the condition they waited for does not hold. But all threads get a chance. n – R 7 : : 23 – This approach is different from what we discussed in the lecture 0024 Spring 2010

Translate “wait()” public void insert(Object o) throws Interrupted. Exception { access. acquire(); while (is.

Translate “wait()” public void insert(Object o) throws Interrupted. Exception { access. acquire(); while (is. Full()) { wait. Thread ++; access. release(); // let other thread access object cond. acquire(); // wait for change of state access. acquire() wait. Thread --; } do. Insert(o); notify. All(); access. release(); }// insert – R 7 : : 24 – 0024 Spring 2010

Translate “notify. All()” public void insert(Object o) throws Interrupted. Exception { access. acquire(); while

Translate “notify. All()” public void insert(Object o) throws Interrupted. Exception { access. acquire(); while (is. Full()) { wait. Thread ++; access. release(); // let other thread access object cond. acquire(); // wait for change of state access. acquire() wait. Thread --; } do. Insert(o); if (wait. Thread > 0) { for (int i=0; i<wait. Thread; i++) { cond. release(); } } access. release(); }// insert – R 7 : : 25 – 0024 Spring 2010

Example Consider the buffer, one slot is empty, four operations insert I 1 insert

Example Consider the buffer, one slot is empty, four operations insert I 1 insert I 2 extract E I 1 – access. acquire I 2 – access. acquire: blocks on access I 3 – access. acquire: blocks on access I 1 – wait. Thread == 0 I 1. release – R 7 : : 26 – 0024 Spring 2010

(cont) I 2 – access. acquire completes I 2 – buffer full wait. Thread

(cont) I 2 – access. acquire completes I 2 – buffer full wait. Thread =1 I 2 – access. release I 2 – cond. acquire – blocks on cond I 3 – access. acquire completes I 3 – buffer full wait. Thread = 2 I 3 – access. release I 3 – cond. acquire – blocks on cond – R 7 : : 27 – 0024 Spring 2010

(cont) E – access. acquire remove item E – SCond. release E – access.

(cont) E – access. acquire remove item E – SCond. release E – access. release One of I 2 or I 3 will succeed with access. acquire and be able to insert the next item – R 7 : : 28 – 0024 Spring 2010

Exercise How would the method “extract” look like (if we used semaphores to emulate

Exercise How would the method “extract” look like (if we used semaphores to emulate monitors)? – R 7 : : 29 – 0024 Spring 2010

Assignment 7 Hint: Thread. current. Thread() returns a handle to the current thread and

Assignment 7 Hint: Thread. current. Thread() returns a handle to the current thread and might be useful for the assignment. – R 7 : : 30 – 0024 Spring 2010

Overview Your task is to implement a Read/Write Lock Max. 4 Threads Max. 2

Overview Your task is to implement a Read/Write Lock Max. 4 Threads Max. 2 Reader Threads (shared access is allowed) and 1 Writer Thread A thread that executes read() is a reader At a later time it can be a writer. . – R 7 : : 31 – 0024 Spring 2010

The challenge No starvation Efficient implementation n n If there are fewer than 2

The challenge No starvation Efficient implementation n n If there are fewer than 2 readers and no waiting writers then the next reader must be allowed to proceed If there is no contention then a thread must be allowed to proceed immediately Your implementation must be fair (you may want to use FIFOQueue. java) – R 7 : : 32 – 0024 Spring 2010

Other comments Keep your solution as simple as possible Decide what you want to

Other comments Keep your solution as simple as possible Decide what you want to use [your decision] n n Monitors (synchronized methods) Semaphores l http: //java. sun. com/j 2 se/1. 5. 0/docs/api/ à Semaphore Only change class Monitor. java n If you feel it‘s necessary to change other files, please let us know. Please comment your code! – R 7 : : 33 – 0024 Spring 2010

You may want to recall Thread. current. Thread(). get. Id() n wait_list. enq(Thread. current.

You may want to recall Thread. current. Thread(). get. Id() n wait_list. enq(Thread. current. Thread(). get. Id()) n wait_list. get. First. Item() == Thread. current. Thread(). get. Id() – R 7 : : 34 – 0024 Spring 2010

Your log could look like this … READ LOCK ACQUIRED 1 READ LOCK RELEASED

Your log could look like this … READ LOCK ACQUIRED 1 READ LOCK RELEASED 0 WRITE LOCK ACQUIRED 1 WRITE LOCK RELEASED 0 READ LOCK ACQUIRED 1 READ LOCK ACQUIRED 2 READ LOCK RELEASED 1 READ LOCK ACQUIRED 2 – R 7 : : 35 – 0024 Spring 2010