Outline Monitors Barrier synchronization The sleeping barber problem

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Classic Synchronization Problems
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Sleeping Barber Problem There is one barber, and n chairs for waiting customers If there are no customers, then the barber sits in his chair and sleeps.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
CS3530 OPERATING SYSTEMS Summer 2014 Synchronization Chapter 6.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Tutorial 5 Even More Synchronization! presented by: Antonio Maiorano Paul Di Marco.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Outline  Monitors o Monitors in Java  Barrier synchronization  The sleeping barber problem  Readers and Writers 1 Ben-Gurion University Operating Systems,
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Synchronization, part 3 Monitors, classical sync. problems
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Synchronization, part 3 Monitors, classical sync. problems
Process Synchronization: Semaphores
Synchronization, part 3 Monitors, classical sync. problems
Synchronization, part 3 Monitors, classical sync. problems
Lecture 13: Producer-Consumer and Semaphores
Chapter 5: Process Synchronization (Con’t)
Outline Monitors Barrier synchronization Readers and Writers
Chapter 5: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Synchronization, part 3 Monitors, classical sync. problems
Synchronization, part 3 Monitors, classical sync. problems
Critical section problem
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Chapter 6 Synchronization Principles
Lecture 13: Producer-Consumer and Semaphores
CSE 153 Design of Operating Systems Winter 2019
Synchronization, part 3 Monitors, classical sync. problems
Outline Monitors Barrier synchronization Readers and Writers
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Outline Monitors Barrier synchronization The sleeping barber problem Monitors in Java Barrier synchronization The sleeping barber problem Readers and Writers One-way tunnel Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Monitors - higher-level synchronization (Hoare, Hansen, 1974-5) Semaphores and event-counters are low-level and error-prone Monitors are a programming-language construct Mutual exclusion constructs generated by the compiler. Internal data structures are invisible. Only one process is active in a monitor at any given time - high level mutual exclusion Monitors support condition variables for thread cooperation. Monitor disadvantages: May be less efficient than lower-level synchronization Not available from all programming languages Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Monitors Only one monitor procedure active at any given time monitor example integer i; condition c; procedure p1( ); . end; procedure p2( ); end monitor; Slide taken from a presentation by Gadi Taubenfeld from IDC Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Monitors: Condition variables Monitors guarantee “automatic” mutual exclusion Conditional variables enable other types of synchronization Condition variables support two operations: wait and signal Signaling has no effect if there are no waiting threads! The monitor provides queuing for waiting procedures When one operation waits and another signals there are two ways to proceed: The signaled operation will execute first: signaling operation immediately followed by block() or exit_monitor (Hoare semantics) The signaling operation is allowed to proceed Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

… Shared data x y Queues associated with x, y conditions operations type monitor-name = monitor variable declarations procedure entry P1 (…); begin … end; procedure entry P2 (…); . procedure entry Pn (…); begin initialization code end Figure 6.20 Monitor with Condition Variable Shared data x y Queues associated with x, y conditions … operations Initialization code Entry queue Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Bounded Buffer Producer/Consumer with Monitors Here are 2 scenarios establishing that the code is wrong if the signaled thread is NOT the next to enter the monitor 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. The buffer is full, a single producer p1 sleeps on the full condition variable. A consumer executes and makes p1 ready but then another producer, p2, enters the monitor and fills the buffer. Now p1 continues its execution and adds another item to an already full buffer. In general, if the monitor does not follow Hoare semantics, a thread must check the condition in a loop, since the condition may be violated between the time it is signaled and the time it enters the monitor. This code only works if a signaled thread is the next to enter the monitor (Hoare) Any problem with this code? Slide taken from a presentation by Gadi Taubenfeld from IDC Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Issues if non-Hoare semantics Here are 2 scenarios establishing that the code is wrong if the signaled thread is NOT the next to enter the monitor 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. The buffer is full, a single producer p1 sleeps on the full condition variable. A consumer executes and makes p1 ready but then another producer, p2, enters the monitor and fills the buffer. Now p1 continues its execution and adds another item to an already full buffer. In general, if the monitor does not follow Hoare semantics, a thread must check the condition in a loop, since the condition may be violated between the time it is signaled and the time it enters the monitor. 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, 2012, Danny Hendler and Roie Zivan

Issues if non-Hoare semantics (cont'd) Here are 2 scenarios establishing that the code is wrong if the signaled thread is NOT the next to enter the monitor 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. The buffer is full, a single producer p1 sleeps on the full condition variable. A consumer executes and makes p1 ready but then another producer, p2, enters the monitor and fills the buffer. Now p1 continues its execution and adds another item to an already full buffer. In general, if the monitor does not follow Hoare semantics, a thread must check the condition in a loop, since the condition may be violated between the time it is signaled and the time it enters the monitor. The buffer is full, a single producer p1 sleeps on the full condition variable. A consumer executes and makes p1 ready but then another producer, p2, enters the monitor and fills the buffer. Now p1 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, 2012, Danny Hendler and Roie Zivan

Monitors - some comments Condition variables do not accumulate signals for later use wait() must come before signal() in order to be signaled No race conditions, because monitors have mutual exclusion More complex to implement – but done by compiler Implementation issues: How to interpret nested monitors? How to define wait, priority scheduling, timeouts, aborts ? How to Handle all exception conditions ? How to interact with process creation and destruction ? Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Implementing Monitors with Semaphores – take 1 semaphore mutex=1; /*control access to monitor*/ semaphore c /*represents condition variable c */ void enter_monitor(void) { down(mutex); /*only one-at-a-time*/ } void leave(void) { 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*/ If a process signals but no processes wait deadlock results. Any problem with this code? May deadlock. 10 Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

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

Outline Monitors Barrier synchronization The sleeping barber problem Monitors in Java Barrier synchronization The sleeping barber problem Readers and writers One-way tunnel Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Monitors in Java No condition variables (actually, only a single implicit one) Procedures are designated as synchronized Synchronization operations: Wait Notify Notifyall Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Producer-consumer in Java (cont’d) Class ProducerConsumer { Producer prod = new Producer(); Consumer cons = new Consumer(); BundedBuffer bb = new BoundedBuffer(); Public static void main(String[] args) { prod.start(); cons.start(); }} Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Producer-consumer in Java Class Producer extends Thread { void run() { while(true) { int item = produceItem(); BoundedBuffer.insert(item); }}} Class Consumer extends Thread { int item while(true) { item = BoundedBuffer.extract(); consume(item); }}} Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Producer-consumer in Java (cont’d) Class BoundedBuffer { private int[] buffer = new int buff[N]; int first = 0, last = 0; public synchronized void insert(int item) { while((last – first) == N) wait(); buff[last % N] = item; notify(); last++; } What is the problem with this code? public synchronized int extract(int item) { while(last == first) wait(); int item = buff[first % N]; first++; notify(); return item; }} Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The problem with the code in previous slide Assume a buffer of size 1 The buffer is empty, consumers 1, 2 enter the monitor and wait A producer enters the monitor and fills it, performs a notify and exits. Consumer 1 is ready. The producer enters the monitor again and waits. Consumer 1 empties the buffer, performs a notify and exits. Consumer 2 gets the signal and has to wait again. DEADLOCK. We must use notifyAll()! Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Monitors in Java: comments notify() does not have to be the last statement wait() adds the calling Thread to the queue of waiting threads a Thread performing notify() is not blocked - just moves one waiting Thread to state ready once the monitor is open, all queued ready Threads (including former waiting ones) are contesting for entry To ensure correctness, wait() operations must be part of a condition-checking loop Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Outline Monitors Barrier synchronization The sleeping barber problem Monitors in Java Barrier synchronization The sleeping barber problem Readers and writers One-way tunnel Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Barriers Useful for computations that proceed in phases 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, 2012, Danny Hendler and Roie Zivan

The fetch-and-increment instruction Fetch-and-increment(w) do atomically prev:=w w:=w+1 return prev Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

A simple barrier using fetch-and-inc shared integer counter=0, bit go local local-go, local-counter Barrier() local-go := go local-counter := Fetch-and-increment(counter) if (local-counter = n) counter := 0 go := 1-go else await (local-go ≠ go) Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Outline Monitors Barrier synchronization The sleeping barber problem Monitors in Java Barrier synchronization The sleeping barber problem Readers and writers One-way tunnel Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The Sleeping Barber Problem Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The sleeping barber problem (cont’d) Barber shop - one service provider; many customers A finite waiting queue One customer is served at a time Service provider, barber, sleeps when no customers are waiting Customer leaves if shop is full Customer sleeps while waiting in queue Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The sleeping barber: implementation #define CHAIRS 5 semaphore customers = 0; // number of waiting customers Semaphore barbers = 0; // number of available barbers: either 0 or 1 int waiting = 0; // copy of customers for reading Semaphore mutex = 1; // mutex for accessing ‘waiting’ void barber(void) { 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(); } } Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The sleeping barber: implementation (cont’d) void customer(void) { down(mutex); // access to `waiting’ if(waiting < CHAIRS) { waiting = waiting + 1; // increment waiting up(customers); // wake up barber up(mutex); // release ‘waiting’ down(barbers); // go to sleep if barbers=0 get_haircut(); } else { up(mutex); /* shop full .. leave */ }} If a customer stops just before get_haircut, the barber will continue with cut_hair and may then awaken another customer. Any problem with this code? Two customers on chair at once Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The sleeping barber: correct synchronization #define CHAIRS 5 semaphore customers = 0; // number of waiting customers Semaphore barbers = 0; // number of available barbers: either 0 or 1 Semaphore mutex = 1; // mutex for accessing ‘waiting’ Semaphore synch = 0; // synchronizing the service operation int waiting = 0; // copy of customers for reading void barber(void) { 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, 2012, Danny Hendler and Roie Zivan

The sleeping barber: correct synchronization (cont’d) void customer(void) { down(mutex); // access to `waiting’ if(waiting < CHAIRS) { waiting = waiting + 1; // increment waiting up(customers); // wake up barber up(mutex); // release ‘waiting’ down(barbers); // go to sleep if barbers=0 get_haircut(); up(sync); //synchronize service } else { up(mutex); /* shop full .. leave */ }} Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Outline Monitors Barrier synchronization The sleeping barber problem Monitors in Java Barrier synchronization The sleeping barber problem Readers and writers One-way tunnel Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The readers and writers problem Motivation: database access Two groups of processes: readers, writers Multiple readers may access database simultaneously A writing process needs exclusive database access Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Readers and Writers: 1'st algorithm Int rc = 0 // # of reading processes semaphore mutex = 1; // controls access to rc semaphore db = 1; // controls database access void reader(void){ while(TRUE){ down(mutex); rc = rc + 1; if(rc == 1) down(db); up(mutex); read_data_base(); rc = rc - 1; if(rc == 0) up(db); up(mutex); } } void writer(void){ while(TRUE){ down(db); write_data_base() up(db) } Who is more likely to run: readers or writers? Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Comments on 1'st algorithm No reader is kept waiting, unless a writer has already obtained the db semaphore Writer processes may starve - if readers keep coming in and hold the semaphore db 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, 2012, Danny Hendler and Roie Zivan

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(void){ 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(void){ 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) Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Comments on 2’nd algorithm When readers are holding Wdb, the first writer to arrive decreases Rdb All Readers arriving later are blocked on Rdb all writers arriving later are blocked on Wdb only the last writer to leave Wdb releases Rdb – readers can wait… If a writer and a few readers are waiting on 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, 2012, Danny Hendler and Roie Zivan

Readers and Writers: improved writers' priority Int rc, wc = 0 // # of reading/writing processes semaphore Rmutex, Wmutex, Mutex2 = 1; semaphore Rdb, Wdb = 1; void reader(void){ while(TRUE){ down(Mutex2) down(Rdb); down(Rmutex) rc = rc + 1; if(rc == 1) down(Wdb); up(Rmutex); up(Rdb) up(Mutex2) read_data_base(); down(Rmutex); rc = rc - 1; if(rc == 0) up(Wdb); up(Rmutex); } } void writer(void){ 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) Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Improved writers' priority After the first writer does down(Rdb), the first reader that enters is blocked after doing down(Mutex2) and before doing up(Mutex2). Thus no other readers can block on Rdb. This guarantees that the writer has to wait for at most a single reader. Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Readers-writers from Monitors Monitor reader_writer{ int numberOfReaders = 0; boolean writing = FALSE; condition okToRead, okToWrite; public: procedure startRead() { if(writing || (notEmpty(okToRead.queue))) okToRead.wait; numberOfReaders = numberOfReaders + 1; okToRead.signal; }; procedure finishRead() { numberOfReaders = numberOfReaders - 1; if(numberOfReaders == 0) okToWrite.signal; Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Readers-writers from Monitors (cont'd) procedure startWrite() { if((numberOfReaders != 0) || busy) okToWrite.wait; writing = TRUE }; procedure finishWrite() { writing = FALSE; if(notEmpty(okToWrite.queue)) okToWrite.signal else okToRead.signal; } Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Outline Monitors Barrier synchronization The sleeping barber problem Monitors in Java Barrier synchronization The sleeping barber problem Readers and writers One-way tunnel Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

The one-way tunnel problem Allows any number of processes in the same direction If there is traffic in the opposite direction – have to wait A special case of readers/writers Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

One-way tunnel - solution int count[2]; Semaphore mutex = 1, busy = 1; Semaphore waiting[2] = {1,1}; void arrive(int direction) { down(waiting[direction]); down(mutex); count[direction] += 1; if(count[direction] == 1) up(mutex); down(busy) else up(waiting[direction]); } void leave(int direction) { down(mutex); count[direction] -= 1; if(count[direction] == 0) up(busy)} up(mutex); } Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan

Synchronization in Solaris Supports adaptive mutexes, condition variables, semaphores, reader-writer locks, turnstiles Two types of mutex locks: Spin – for long waiting periods Adaptive – when waiting may be short Spin-wait if lock is held by another running process If holder is blocked – block also Readers/writer locks: allow either multiple readers or a single writer to be in the critical section Turnstiles are used to manage queues of threads waiting on mutex and synchronize wakeup. Assigned dynamically according to needs Signal sent to highest-priority waiting thread Ben-Gurion University Operating Systems, 2012, Danny Hendler and Roie Zivan