Outline q Monitors o Monitors in Java q

  • Slides: 51
Download presentation
Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber problem q Readers and Writers Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 1

Monitors - higher-level synchronization (Hoare, Hansen, 1974 -5) q Semaphores and event-counters are low-level

Monitors - higher-level synchronization (Hoare, Hansen, 1974 -5) q Semaphores and event-counters are low-level and errorprone q Monitors are a programming-language construct q Mutual exclusion constructs generated by the compiler. Internal data structures are invisible. q Monitors combine 2 synchronization mechanisms: o Only one process is active at any given time inside the monitor. o Condition variables for signaling (wait / signal). Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 2

Monitors - Definition q A monitor is a program module for concurrent programming o

Monitors - Definition q A monitor is a program module for concurrent programming o Encapsulate shared storage and operations. q A monitor has entry procedures o Entry procedures are called by processes (threads); the monitor is passive. o The monitor guarantees mutual exclusion for calls of entry procedures: q Condition variables are defined in the monitor o Condition Variables are used within entry procedures for condition synchronization. Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 3

Monitors Only one monitor procedure active at any given time monitor example integer i;

Monitors Only one monitor procedure active at any given time monitor example integer i; condition c; entry procedure p 1( ); . . . end; entry procedure p 2( ); . . . Slide end; taken from a presentation by Gadi Taubenfeld from IDC end monitor; Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 4

Monitors: Condition variables q Monitors guarantee “automatic” mutual exclusion q Conditional variables enable signaling.

Monitors: Condition variables q Monitors guarantee “automatic” mutual exclusion q Conditional variables enable signaling. q Condition variables support two operations: wait and signal o Signaling has no effect if there are no waiting threads q The monitor provides queuing for waiting procedures q When one operation waits and another signals there are two ways to proceed: o The signaled operation will execute first: signaling operation immediately followed by block() or exit_monitor (Hoare semantics) o The signaling operation is allowed to proceed (Java semantics) Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 5

Monitors: Wait/Signal wait (c) The executing process leaves the monitor and waits in a

Monitors: Wait/Signal wait (c) The executing process leaves the monitor and waits in a set associated to c, until it is released by a subsequent call signal(c); then the process accesses the monitor again and continues. signal (c): The executing process releases one arbitrary process that waits for c. Which of the two processes immediately continues its execution in the monitor depends on the variant of the signal semantics: signal-and-exit semantics (Hoare’s semantics): The process that executes signal terminates the entry procedure call and leaves the monitor. The released process enters the monitor immediately -without a state change in between and without the mutex being released ever (hand to hand transfer of the mutex from signaling process to waiting process). signal-and-wait semantics: The process that executes signal leaves the monitor and waits to re-enter the monitor. The released process enters the monitor immediately - without a state change in between (hand to hand transfer of mutex). signal-and-continue semantics (Java semantics): The process that executes signal continues execution in the monitor. The released process has to wait until the monitor is free. The state that held at the signal call may be changed meanwhile; the waiting condition has to be checked again. (no hand transfer of mutex. ) Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 6

type monitor-name = monitor variable declarations procedure entry P 1 (…); begin … end;

type monitor-name = monitor variable declarations procedure entry P 1 (…); begin … end; procedure entry P 2 (…); begin … end; . . . procedure entry Pn (…); begin … end; begin initialization code Entry queue end Shared data Queues associated with x, y conditions x y … operations Figure 6. 20 Monitor with Condition Variable Initialization code Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 7

Bounded Buffer Producer/Consumer with Monitors This code only works if a signaled thread is

Bounded Buffer Producer/Consumer with Monitors This code only works if a signaled thread is the next to enter the monitor (Hoare) Slide taken from a presentation by Gadi Taubenfeld from IDC Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 8

Issues if non-Hoare semantics 1. The buffer is full, k producers (for some k>1)

Issues if non-Hoare semantics 1. The buffer is full, k producers (for some k>1) are waiting on the full condition variable. Now, N consumers enter the monitor one after the other, but only the first sends a signal (since count==N-1) holds for it. Therefore only a single producer is released and all others are not. The corresponding problem can occur on the empty semaphore. Slide taken from a presentation by Gadi Taubenfeld from IDC Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 9

Issues if non-Hoare semantics (cont'd) 2) The buffer is full, a single producer p

Issues if non-Hoare semantics (cont'd) 2) The buffer is full, a single producer p 1 sleeps on the full condition variable. A consumer executes and makes p 1 ready but then another producer, p 2, enters the monitor and fills the buffer. Now p 1 continues its execution and adds another item to an already full buffer. Slide taken from a presentation by Gadi Taubenfeld from IDC Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 10

Monitors - some comments q Condition variables do not accumulate signals for later use

Monitors - some comments q Condition variables do not accumulate signals for later use q wait() must come before signal() in order to be signaled q No race conditions, because monitors have mutual exclusion q Complex to implement – but “difficult work” done by compiler q Implementation issues: o How to interpret nested monitors? o How to define wait, priority scheduling, timeouts, aborts? o How to handle exception conditions? o How to interact with process creation and destruction? Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 11

Implementing Monitors with Semaphores – take 1 semaphore mutex=1; /*control access to monitor*/ semaphore

Implementing Monitors with Semaphores – take 1 semaphore mutex=1; /*control access to monitor*/ semaphore c /*represents condition variable c */ void enter_monitor() { down(mutex); /*only one-at-a-time*/ } void leave() { up(mutex); /*allow other processes in*/ } void leave_with_signal(semaphore c) { /* leave with signaling c*/ up(c); /*release the condition variable, mutex not released */ } void wait(semaphore c) { /* block on a condition c */ up(mutex); /*allow other processes*/ down (c); /*block on the condition variable*/ } Any problem with this code? May deadlock. Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 12

Implementing Monitors with Semaphores – take 1 semaphore mutex=1; semaphore c /*control access to

Implementing Monitors with Semaphores – take 1 semaphore mutex=1; semaphore c /*control access to monitor*/ /*represents condition variable c */ void enter_monitor() { down(mutex); /*only one-at-a-time*/ } void leave() { up(mutex); /*allow other processes in*/ } void leave_with_signal(semaphore c) { /* leave with signaling c*/ up(c); /*release the condition variable, mutex not released */ } void wait(semaphore c) { /* block on a condition c */ up(mutex); /*allow other processes*/ down (c); /*block on the condition variable*/ } Signal as last call in a procedure – unblocks a process that did wait and leave the monitor without freeing mutex. When wait resumes from down(c) it does not need to acquire mutex because mutex was not freed by signal(c) Hand to hand passing of mutex – but if “missed signal” (signal when no one waiting) – mutex is no freed deadlock. Solution is that signal must distinguish between signal->wait transitions and signal->exit transitions. Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 13

Implementing Monitors with Semaphores - Correct Semaphore mutex = 1; Cond c; void enter_monitor()

Implementing Monitors with Semaphores - Correct Semaphore mutex = 1; Cond c; void enter_monitor() { down(mutex); } void leave() { up(mutex); } void leave_with_signal(cond c) { if (c. count == 0) up(mutex); else { c. count--; up(c. s); } } void wait(cond c) { c. count++; up(mutex); down(c. s); } /* control access to monitor */ /* c = {count; semaphore} */ /* only one-at-a-time */ /* allow other processes in */ /* cond c is a struct */ /* no waiting, just leave. . */ /* /* block on a condition */ count waiting processes */ allow other processes */ block on the condition */ Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 14

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber problem q Readers and writers Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 15

Monitors in Java (before Java 5) q Java directly adapted the Monitor model with

Monitors in Java (before Java 5) q Java directly adapted the Monitor model with some changes. q Procedures designated as synchronized are like entry procedures. o All Java Object instances include an implicit mutex lock. q No explicit condition variables o Only a single implicit condition variable on “this” o Signaling operations: § Wait() § Notify. All() Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 16

Monitors in Java Util Concurrent q. Explicit lock interface: o Use when synchronized blocks

Monitors in Java Util Concurrent q. Explicit lock interface: o Use when synchronized blocks don’t apply § Time−outs, back−offs § Assuring interruptibility § Hand−over−hand locking § Need multiple condition variables. q. Multiple condition variables per lock o Condition interface with lock. new. Condition() Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 17

Producer-consumer in Java class Bounded. Buffer { final Lock lock = new Reentrant. Lock();

Producer-consumer in Java class Bounded. Buffer { final Lock lock = new Reentrant. Lock(); final Condition not. Full = lock. new. Condition(); final Condition not. Empty = lock. new. Condition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(Object x) throws Interrupted. Exception { lock(); try { while (count == items. length) not. Full. await(); items[putptr] = x; if (++putptr == items. length) putptr = 0; ++count; not. Empty. signal(); } finally { lock. unlock(); } } public Object take() throws Interrupted. Exception { lock(); try { while (count == 0) not. Empty. await(); Object x = items[takeptr]; if (++takeptr == items. length) takeptr = 0; --count; not. Full. signal(); return x; } finally { lock. unlock(); } } } Java 7 supports multiple condition variables on a single lock using the Lock interface and the new. Condition() factory. The java. util. concurrent. Array. Blocking. Queue class provides this functionality (with generic types) so there is no reason to implement this class. condition. await() and signal() require that the underlying lock be acquired – else throw Illegal. Monitor. State. Observe the pattern: lock(); try {…} finally { lock. unlock(); } to enforce mutex using an explicit lock (instead of the implicit synchronized). Explicit locks support testing for locking and timeout. From http: //www. docjar. com/docs/api/java/util/concurrent/locks/Condition. html Written by Doug Lea with assistance from members of JCP JSR-166 Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 18

Producer-consumer in Java class Producer implements Runnable { private Bounded. Buffer bb; public Producer(Bounded.

Producer-consumer in Java class Producer implements Runnable { private Bounded. Buffer bb; public Producer(Bounded. Buffer b) {bb = b; } void run() { while(true) { Integer item = produce. Item(); bb. insert(item); } } protected Integer produce. Item() { …} } class Consumer implements Runnable { private Bounded. Buffer bb; public Consumer(Bounded. Buffer b) {bb = b; } void run() { while(true) { int item = bb. extract(); consume(item); } } protected void consume(Integer item) { …} } class Producer. Consumer { private Producer p; private Consumer c; private Bounded. Buffer b; public Producer. Consumer() { b = new Bounded. Buffer(); p = new Producer(b); c = new Producer(b); } public static int main(String args[]){ (new Thread(p)). start(); (new Thread(c)). start(); } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 19

Monitors in Java: comments q notify() does not have to be the last statement

Monitors in Java: comments q notify() does not have to be the last statement q wait() adds the calling Thread to the queue of waiting threads q a Thread performing notify() is not blocked - just moves one waiting Thread to state ready q once the monitor is open, all queued ready Threads (including former waiting ones) are contesting for entry (NO hand to hand passing of mutex). q To ensure correctness, wait() operations must be part of a condition-checking loop (because of the “hole” in the mutex between notify() and resume of wait()). Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 20

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber problem q Readers and writers Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 21

Barriers q Useful for computations that proceed in phases q Use of a barrier:

Barriers q Useful for computations that proceed in phases q Use of a barrier: (a) processes approaching a barrier (b) all processes but one blocked at barrier (c) last process arrives, all are let through Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 22

Using Cyclic. Barrier in Java import java. util. concurrent. Broken. Barrier. Exception ; import

Using Cyclic. Barrier in Java import java. util. concurrent. Broken. Barrier. Exception ; import java. util. concurrent. Cyclic. Barrier ; import java. util. logging. Level; import java. util. logging. Logger; public class Cyclic. Barrier. Example { //Runnable task for each thread private static class Task implements Runnable { private Cyclic. Barrier barrier; public Task(Cyclic. Barrier barrier) { this. barrier = barrier; } public void run() { try { System. out. println(Thread. current. Thread(). get. Name () + " is waiting on barrier"); barrier. await(); System. out. println(Thread. current. Thread(). get. Name () + " has crossed the barrier"); } catch (Interrupted. Exception ex) { Logger. get. Logger(Cyclic. Barrier. Example. class. get. Name()). log(Level. SEVERE, null, ex); } catch (Broken. Barrier. Exception ex) { Logger. get. Logger(Cyclic. Barrier. Example. class. get. Name()). log(Level. SEVERE, null, ex); } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 23

Using Cyclic. Barrier in Java public static void main(String args[]) { //create a Cyclic.

Using Cyclic. Barrier in Java public static void main(String args[]) { //create a Cyclic. Barrier with 3 parties, i. e. , 3 Threads needs to call await() final Cyclic. Barrier cb = new Cyclic. Barrier(3, new Runnable() { public void run(){ //This task will be executed once all threads reach barrier System. out. println("All parties are arrived at barrier, lets play"); }); new Thread(new Task(cb), "Thread 1"). start(); new Thread(new Task(cb), "Thread 2"). start(); new Thread(new Task(cb), "Thread 3"). start(); } } Output: Thread 1 is waiting on barrier Thread 3 is waiting on barrier Thread 2 is waiting on barrier All parties are arrived at barrier, lets play Thread 3 has crossed the barrier Thread 1 has crossed the barrier Thread 2 has crossed the barrier Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 24

Typical Use of Cyclic. Barrier // Dispatch work to N workers // Then merge

Typical Use of Cyclic. Barrier // Dispatch work to N workers // Then merge the results. Fork and Join class Solver { final int N; final float[][] data; final Cyclic. Barrier barrier; class Worker implements Runnable { int my. Row; Worker(int row) { my. Row = row; } public void run() { while (!done()) { process. Row(my. Row); try { barrier. await(); } catch (Interrupted. Exception ex) { return; } catch (Broken. Barrier. Exception ex) { return; } } public Solver(float[][] matrix) { data = matrix; N = matrix. length; barrier = new Cyclic. Barrier(N, new Runnable() { public void run() { merge. Rows(. . . ); }); for (int i = 0; i < N; ++i) new Thread(new Worker(i)). start(); wait. Until. Done(); } } Read more on the Fork-Join Framework http: //gee. cs. oswego. edu/dl/cpjslides/fj. pdf Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 25

Implementing Cyclic. Barrier The fetch-and-increment instruction Fetch-and-increment(w) do atomically prev: =w w: =w+1 return

Implementing Cyclic. Barrier The fetch-and-increment instruction Fetch-and-increment(w) do atomically prev: =w w: =w+1 return prev Called Interlocked. Increment in Windows Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 26

Why can’t we just do simple? shared integer counter=0 Barrier() Fetch-and-increment(counter) await (counter ==

Why can’t we just do simple? shared integer counter=0 Barrier() Fetch-and-increment(counter) await (counter == n) We want to reset the barrier and re-use it (recycle) and We want to execute a follow-up action only once. Who should reset? Beware the race condition! Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 27

A simple barrier using fetch-and-inc shared integer counter=0, bit go local-go, local-counter Barrier() local-go

A simple barrier using fetch-and-inc shared integer counter=0, bit go local-go, local-counter Barrier() local-go : = go local-counter : = Fetch-and-increment(counter) if (local-counter == n-1) counter : = 0 go : = 1 -go else await (local-go ≠ go) This is a busy-wait solution – look at Java solution for lock-based higher latency barrier: http: //www. docjar. com/html/api/java/util/concurrent/Cyclic. Barrier. java. html Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 28

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber problem q Readers and writers Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 29

Cyclic. Barrier Java Implementation (1/4) public class Cyclic. Barrier { /** * Each use

Cyclic. Barrier Java Implementation (1/4) public class Cyclic. Barrier { /** * Each use of the barrier is represented as a generation instance. * The generation changes whenever the barrier is tripped, or * is reset. There can be many generations associated with threads * using the barrier - due to the non-deterministic way the lock * may be allocated to waiting threads - but only one of these * can be active at a time (the one to which <tt>count</tt> applies) * and all the rest are either broken or tripped. * There need not be an active generation if there has been a break * but no subsequent reset. */ private static class Generation { boolean broken = false; } /** The lock for guarding barrier entry */ private final Reentrant. Lock lock = new Reentrant. Lock(); /** Condition to wait on until tripped */ private final Condition trip = lock. new. Condition(); /** The number of parties */ private final int parties; /* The command to run when tripped */ private final Runnable barrier. Command; /** The current generation */ private Generation generation = new Generation(); /** * Number of parties still waiting. Counts down from parties to 0 * on each generation. It is reset to parties on each new * generation or when broken. */ private int count; Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 30

Cyclic. Barrier Java Implementation (2/4) /** * Updates state on barrier trip and wakes

Cyclic. Barrier Java Implementation (2/4) /** * Updates state on barrier trip and wakes up everyone. * Called only while holding lock. */ private void next. Generation() { // signal completion of last generation trip. signal. All(); // set up next generation count = parties; generation = new Generation(); } /** * Sets current barrier generation as broken and wakes up everyone. * Called only while holding lock. */ private void break. Barrier() { generation. broken = true; count = parties; trip. signal. All(); } public Cyclic. Barrier(int parties, Runnable barrier. Action) { if (parties <= 0) throw new Illegal. Argument. Exception(); this. parties = parties; this. count = parties; this. barrier. Command = barrier. Action; } public int await() throws Interrupted. Exception, Broken. Barrier. Exception { try { return dowait(false, 0 L); } catch (Timeout. Exception toe) { throw new Error(toe); // cannot happen; } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 31

Cyclic. Barrier Java Implementation (3/4) /** * Main barrier code, covering the various policies.

Cyclic. Barrier Java Implementation (3/4) /** * Main barrier code, covering the various policies. */ private int dowait(boolean timed, long nanos) throws Interrupted. Exception, Broken. Barrier. Exception, Timeout. Exception { final Reentrant. Lock lock = this. lock; lock(); try { final Generation g = generation; if (g. broken) throw new Broken. Barrier. Exception(); if (Thread. interrupted()) { break. Barrier(); throw new Interrupted. Exception(); } int index = --count; if (index == 0) { // tripped boolean ran. Action = false; try { final Runnable command = barrier. Command; if (command != null) command. run(); ran. Action = true; next. Generation(); return 0; } finally { if (!ran. Action) break. Barrier(); } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 32

Cyclic. Barrier Java Implementation (4/4) // loop until tripped, broken, interrupted, or timed out

Cyclic. Barrier Java Implementation (4/4) // loop until tripped, broken, interrupted, or timed out for (; ; ) { try { if (!timed) trip. await(); else if (nanos > 0 L) nanos = trip. await. Nanos(nanos); } catch (Interrupted. Exception ie) { if (g == generation && ! g. broken) { break. Barrier(); throw ie; } else { // We're about to finish waiting even if we had not // been interrupted, so this interrupt is deemed to // "belong" to subsequent execution. Thread. current. Thread(). interrupt(); } } if (g. broken) throw new Broken. Barrier. Exception(); if (g != generation) return index; if (timed && nanos <= 0 L) { break. Barrier(); throw new Timeout. Exception(); } } } finally { lock. unlock(); } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 33

Cyclic. Barrier Java Implementation: Notes What makes the code complex? - Interruptibility: what happens

Cyclic. Barrier Java Implementation: Notes What makes the code complex? - Interruptibility: what happens if any thread that is waiting on barrier is interrupted? - Exceptions: what happens if the thread that executes the followup action throws an exception? - Timeouts: how can we wait for the barrier for a limited amount of time and give up after timeout? That is – Barrier can exit for 4 different reasons: (1) tripped (good), (2) broken, (3) interrupted or (4) timed out. Introduce “broken barrier” case and “Generation” object: - Can break barrier from the outside to “free” the blocked threads. - Barrier is broken in case of interruption - Threads of different generations can co-exist Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 34

The Sleeping Barber Problem 35 Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad,

The Sleeping Barber Problem 35 Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels

The sleeping barber problem (cont’d) q Barber shop - one service provider; many customers

The sleeping barber problem (cont’d) q Barber shop - one service provider; many customers q A finite waiting queue q One customer is served at a time q Service provider, barber sleeps when no customers are waiting q Customer leaves if shop is full q Customer sleeps while waiting in queue Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 36

The sleeping barber: implementation #define CHAIRS 5 Semaphore customers = 0; Semaphore barbers =

The sleeping barber: implementation #define CHAIRS 5 Semaphore customers = 0; Semaphore barbers = 0; int waiting = 0; Semaphore mutex = 1; // // number of waiting customers number of available barbers: either 0 or 1 copy of customers for reading mutex for accessing ‘waiting’ void barber() { while (TRUE) { down(customers); // block if no customers down(mutex); // access to ‘waiting’ waiting = waiting - 1; up(barbers); // barber is in. up(mutex); // release ‘waiting’ // Synchronization complete: // One barber faces one customer. cut_hair(); } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 37

The sleeping barber: implementation (cont’d) void customer() { down(mutex); // access to `waiting’ if

The sleeping barber: implementation (cont’d) void customer() { down(mutex); // access to `waiting’ if (waiting < CHAIRS) { waiting = waiting + 1; up(customers); // wake up barber up(mutex); // release ‘waiting’ down(barbers); // go to sleep if barbers=0 // Synchronization complete: // one customer, one barber get_haircut(); } else { up(mutex); } // shop full - leave } Any problem with this code? Two customers on chair at once Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 38

The sleeping barber: correct synchronization #define CHAIRS 5 Semaphore customers = 0; Semaphore barbers

The sleeping barber: correct synchronization #define CHAIRS 5 Semaphore customers = 0; Semaphore barbers = 0; Semaphore mutex = 1; Semaphore synch = 0; int waiting = 0; // // // number of waiting customers number of available barbers: 0 or 1 mutex for accessing ‘waiting’ synchronize the service operation copy of customers for reading void barber() { while (TRUE) { down(customers); // block if no customers down(mutex); // access to ‘waiting’ waiting = waiting - 1; up(barbers); // barber is in. . up(mutex); // release ‘waiting’ cut_hair(); down(synch) //wait for customer to leave } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 39

The sleeping barber: correct synchronization (cont’d) void customer() { down(mutex); if (waiting < CHAIRS)

The sleeping barber: correct synchronization (cont’d) void customer() { down(mutex); if (waiting < CHAIRS) { waiting = waiting + 1; up(customers); up(mutex); down(barbers); get_haircut(); up(sync); } else { up(mutex); } // access to `waiting’ // wake up barber // release ‘waiting’ // go to sleep if barbers=0 // synchronize service // customer leaves chair // shop full . . leave } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 40

The sleeping barber in Java - Barber public class Barber implements Runnable { }

The sleeping barber in Java - Barber public class Barber implements Runnable { } private Queue<Customer> customers; private Shop shop; public Barber(Shop shop) { this. shop = shop; customers = new Linked. Blocking. Queue<Customer>(3); } public void add. Customer. To. Queue(Customer customer) { customers. add(customer); } public void run() { while (! customers. is. Empty()) { Customer customer = customers. remove(); perform. Haircut(customer); shop. release. Chair(); } } private void perform. Haircut(Customer customer) { System. out. println("Customer " + customer. get. Id() + " has got a haircut"); } http: //bluepi. in/blogs/2013/03/18/actors-in-action-sleeping-barber-problem-with-akka/ Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 41

The sleeping barber in Java - Shop public class Shop { private Barber barber;

The sleeping barber in Java - Shop public class Shop { private Barber barber; private Semaphore semaphore; private Atomic. Integer count = new Atomic. Integer(0); // customers served public Shop() { semaphore = new Semaphore(3); // How many chairs in shop } public void init() { barber = new Barber(this); Executors. new. Single. Thread. Scheduled. Executor (). schedule. At. Fixed. Rate(barber , 1000, Time. Unit. MILLISECONDS); } public void acquire. Chair(Customer customer) throws Interrupted. Exception { boolean acquired = semaphore. try. Acquire(1, 100, Time. Unit. MILLISECONDS); if (acquired) { count. get. And. Increment(); barber. add. Customer. To. Queue(customer ); } else { System. out. println("Turning customer " + customer. get. Id() + " away"); } } public void release. Chair() { semaphore. release(1); } public int get. Successful. Haircut. Count () { return count. int. Value(); } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 42

The sleeping barber in Java - Notes 1. Barber is an active object (Runnable)

The sleeping barber in Java - Notes 1. Barber is an active object (Runnable) which works in the context of a Shop activates Barber in an executor. 2. Shop at the time of initialization passes itself as a callback to the Barber so that all the activities performed by Barber can be recorded by the shop. 3. Shop in the init method starts the barber in a periodic executor to periodically invoke run method. This is instead of the “while (true)” loop of the naïve code (reduces contention on the blocking. Queue when many customers arrive at once). 4. When a customer arrives, it tries to acquire a chair which is represented by a Semaphore(N), N is the number of spots in the waiting room of the shop. No more than N customers can wait at any given point. Note how we use the “try. Acquire” method of the semaphore with timeout instead of testing explicitly for a counter as in the naïve code. 5. Note usage of Atomic. Integer to safely count served customers. 6. Questions: 7. Complete code that simulates arrival of Customers. 8. Why is init() needed in Shop? (Review “Escape reference” problem from SPL) 9. Is the Barber queue used in a safe manner? (Any possible conflict between thread inserting new Customers in add. Customer. To. Queue() and Barber taking them in run()? ) 10. Write a simpler version exploiting the Blocking. Queue synchronization capabilities – compare performance. 11. How does the size of the waiting room impact overall performance? Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 43

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber

Outline q Monitors o Monitors in Java q Barrier synchronization q The sleeping barber problem q Readers and writers Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 44

The readers and writers problem q Motivation: database access q Two groups of processes:

The readers and writers problem q Motivation: database access q Two groups of processes: readers, writers q Multiple readers may access database simultaneously (no conflicts on read-only) q A writing process needs exclusive database access Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 45

Readers and Writers: 1 st algorithm Int rc = 0 semaphore mutex = 1;

Readers and Writers: 1 st algorithm Int rc = 0 semaphore mutex = 1; semaphore db = 1; void reader() { while (TRUE) { down(mutex); rc = rc + 1; if (rc == 1) down(db); up(mutex); read_data_base(); down(mutex); rc = rc - 1; if (rc == 0) up(db); up(mutex); } } // # of reading processes // controls access to rc // controls database access void writer() { while(TRUE) { down(db); write_data_base() up(db) } Who is more likely to run: readers or writers? Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 46

Comments on 1 st algorithm q No reader is kept waiting, waiting unless a

Comments on 1 st algorithm q No reader is kept waiting, waiting unless a writer has already obtained the db semaphore q Writer processes may starve - if readers keep coming in and hold the semaphore db q An alternative version of the readers-writers problem requires that no writer is kept waiting once it is “ready” when a writer is waiting, no new reader can start reading Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 47

Readers and Writers: Writers priority Int rc, wc = 0 // # of reading/writing

Readers and Writers: Writers priority Int rc, wc = 0 // # of reading/writing processes semaphore Rmutex, Wmutex = 1; // controls readers/writers access to rc/wc semaphore Rdb, Wdb = 1; // controls readers/writers database access void reader() { while(TRUE) { down(Rdb); down(Rmutex); rc = rc + 1; if(rc == 1) down(Wdb); up(Rmutex); up(Rdb) read_data_base(); down(Rmutex); rc = rc - 1; if (rc == 0) up(Wdb); up(Rmutex); } } void writer() { while(TRUE) { down(Wmutex); wc = wc + 1; if (wc == 1) down (Rdb); up(Wmutex); down(Wdb); write_data_base(); up(Wdb); down(Wmutex); wc = wc-1; if (wc == 0) up(Rdb); up(Wmutex); }} Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 48

Comments on 2 nd algorithm q When Readers are holding Wdb, Wdb the first

Comments on 2 nd algorithm q When Readers are holding Wdb, Wdb the first Writer to arrive decreases Rdb q All Readers arriving later are blocked on Rdb q All Writers arriving later are blocked on Wdb q Only the last Writer to leave Wdb releases Rdb – Readers can wait… q If a writer and a few readers are waiting on Rdb, Rdb the writer may still have to wait for these readers If Rdb is unfair, the writer may again starve. Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 49

Readers and Writers: improved writers priority Int rc, wc = 0 // # of

Readers and Writers: improved writers priority Int rc, wc = 0 // # of reading/writing processes semaphore Rmutex, Wmutex, Mutex 2 = 1; semaphore Rdb, Wdb = 1; void reader() { while(TRUE) { down(Mutex 2) down(Rdb); down(Rmutex); rc = rc + 1; if (rc == 1) down(Wdb); up(Rmutex); up(Rdb) up(Mutex 2) read_data_base(); down(Rmutex); rc = rc - 1; if(rc == 0) up(Wdb); up(Rmutex); } } void writer() { while(TRUE) { down(Wmutex); wc = wc + 1 if (wc == 1) down (Rdb); up(Wmutex); down(Wdb); write_data_base(); up(Wdb); down(Wmutex); wc = wc-1; if (wc == 0) up(Rdb); up(Wmutex); } } Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 50

Improved writers priority q After the first writer does down(Rdb), the first reader that

Improved writers priority q After the first writer does down(Rdb), the first reader that enters is blocked after doing down(Mutex 2) and before doing up(Mutex 2) Thus no other readers can block on Rdb q This guarantees that the writer has to wait for at most a single reader. Ben-Gurion University Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 51