11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Section 3. True/False Changing the order of semaphores’ operations in a program does not matter. False.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Scalable Synchronous Queues By William N. Scherer III, Doug Lea, and Michael L. Scott Presented by Ran Isenberg.
1 Semaphores and Monitors: High-level Synchronization Constructs.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
Monitors and Blocking Synchronization By Tal Walter.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
CS533 Concepts of Operating Systems Class 3 Monitors.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
Data Structures - Queues
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
CS510 Concurrent Systems Introduction to Concurrency.
Experience with Processes and Monitors in Mesa
Nachos Phase 1 Code -Hints and Comments
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Lecture 12 CV. Last lecture Controlling interrupts Test and set (atomic exchange) Compare and swap Load linked and store conditional Fetch and add and.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
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.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Slides on threads borrowed by Chase Landon Cox. Thread states BlockedReady Running Thread is scheduled Thread is Pre-empted (or yields) Thread calls Lock.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
pThread synchronization
CS703 - Advanced Operating Systems
CPS110: Reader-writer locks
Background on the need for Synchronization
Monitors, Condition Variables, and Readers-Writers
CS510 Operating System Foundations
CS510 Operating System Foundations
Anthony D. Joseph and Ion Stoica
Producer-Consumer Problem
Chapter 30 Condition Variables
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Slides by Prof. Landon Cox and Vamsi Thummala
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Monitors and Inter-Process Communication
EECE.4810/EECE.5730 Operating Systems
CPS110: Thread cooperation
Presentation transcript:

11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King, and Andrew S Tanenbaum

Dequeue if empty? What if you wanted to have dequeue() wait if the queue is empty? Could spin in a loop: dequeue() { lock(queuelock); element = NULL; while (head-next == NULL) {wait;}; if(head->next != NULL) { element = head->next; head->next = head->next->next;} unlock(queuelock); return element; } 11/21/20152

Problem lock(queuelock); … while (head-next == NULL) {wait;}; Holding lock while waiting No one else can access list (if they observe the lock) Wait forever 11/21/20153

Dequeue if empty – Try 2 Could release the lock before spinning: lock(queuelock); … unlock(queuelock); while (head-next == NULL) {wait;}; Will this work? 11/21/20154

Dequeue if empty – Try 3 Could release lock and acquire lock on every iteration lock(queuelock); … while (head-next == NULL) {unlock(queuelock); lock(queuelock);}; This will work, but very ineffecient. 11/21/20155

Dequeue if empty Busy waiting is inefficient, instead you would like to “go to sleep” Waiting list shared between enq and deq Must release locks before going to sleep 11/21/20156

Dequeue if empty – Try 4 Does this work? 11/21/20157 enqueue() { lock(); find tail; add new element; if(waiting deq) { rem deq from wait; wake up deq; } unlock(); } dequeue(){ … if(queue is empty) { release lock(); add to wait list; go to sleep; } … }

Dequeue is empty – try 5 What if we release lock after adding dequeue() to waiting list, but before going to sleep Does this work? … if(queue is empty) { add myself to waiting list; release lock; go to sleep and wait; } 11/21/20158

Two types of synchronization Mutual exclusion Only one thread can do given operation at a time (e.g., only one person goes shopping at a time) Symmetric Ordering constraints Mutual exclusion does not care about order Are situations where ordering of thread operations matter E.g., before and after relationships Asymmetric 11/21/20159

Monitors Note: Differs from Tanenbaum’s treatment Monitors use separate mechanisms for the two types of synchronization Use locks for mutual exclusion Use condition variables for ordering const. A monitor = a lock + the condition variables associated with that lock 11/21/201510

Condition variables Main idea: let threads sleep inside critical section by atomically Releasing lock Putting thread on wait queue and go to sleep Each cond var has a queue of waiting threads Do you need to worry about threads on the wait queue, but not asleep 11/21/201511

Operations on cond. variables Wait(): atomically release lock, put thread on condition wait queue, go to sleep release lock Go to sleep Request lock as part of waking up Signal(): wake up a thread waiting on this condition variable; causes awoken program to request the lock 11/21/201512

Operations on cond. variables Broadcast(): wake up all threads waiting on this condition variable; all will request lock Note: thread must hold lock when it calls wait() Should thread re-establish the invariant before calling wait? How about signal? 11/21/201513

Example: Cell phone case at AT&T Store Bought a Cell Phone from AT&T store “Came” with choice of case I looked (lock(case)) and didn’t find one I wanted They told me they would call me when the next shipment was in I left (wait (case, case_arrives)) They call mid-day to say shipment in (signal(case, case_arrives)) I go at end of day to get one (lock(case)) Cases are sold out (wait(case, case_arrives)) 11/21/201514

Thread-safe queues with monitors Is this good enough? 11/21/ enqueue() { lock(queueLock); find tail; add elem to tail; signal(queueLock, queueCond); unlock(queueLock); } dequeue() { lock(queueLock); if(queue empty) { wait(queueLock, queueCond)}; remove from queue; unlock(queueLock); return item;

Problem Scenario lockOwner: Lock Queue: Cond Queue: Thread1(deq)Thread2(enq)Thread3(deq) 11/21/201516

Problem Scenario lockOwner:T1 Lock Queue: Cond Queue: Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock();

Problem Scenario lockOwner: Lock Queue: Cond Queue:T1 Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); if(queue empty) {wait()};

Problem Scenario lockOwner:T2 Lock Queue: Cond Queue:T1 Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); if(queue empty) {wait()};

Problem Scenario lockOwner:T2 Lock Queue: Cond Queue:T1 Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); find tail; add elem; lock(); if(queue empty) {wait()};

Problem Scenario lockOwner:T2 Lock Queue:T3, Cond Queue:T1 Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); find tail; add elem; lock(); if(queue empty) {wait()}; lock();

Problem Scenario lockOwner:T2 Lock Queue:T3,T1 Cond Queue: Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); find tail; add elem; signal(); lock(); if(queue empty) {wait()}; lock();

Problem Scenario lockOwner: Lock Queue:T3,T1 Cond Queue: Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); find tail; add elem; signal(); unlock(); lock(); if(queue empty) {wait()}; lock();

Problem Scenario lockOwner:T3 Lock Queue:T1 Cond Queue: Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); find tail; add elem; signal(); unlock(); lock(); if(queue empty) {wait()}; lock(); if(queue empty) { … }. remove from queue;

Problem Scenario lockOwner:T3 Lock Queue:T1 Cond Queue: Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); find tail; add elem; signal(); unlock(); lock(); if(queue empty) {wait()}; lock(); if(queue empty) { … }. remove from queue; unlock();

Problem Scenario lockOwner:T1 Lock Queue: Cond Queue: Thread1(deq)Thread2(enq)Thread3(deq) 11/21/ lock(); find tail; add elem; signal(); unlock(); lock(); if(queue empty) {wait()}; remove from queue; (Bug!) lock(); if(queue empty) { … }. remove from queue; unlock();

Thread-safe queues with monitors Is this good enough? 11/21/ enqueue() { lock(queueLock); find tail; add elem to tail; signal(queueLock, queueCond); unlock(queueLock); } dequeue() { lock(queueLock); while(queue empty) { wait(queueLock, queueCond)}; remove from queue; unlock(queueLock); return item;

Mesa vs. Hoare monitors So far have described Mesa monitors When waiter is woken, must contend for the lock with other threads Must re-check condition What would be required to ensure that the condition is met when the waiter returns from the wait and start running again? 11/21/201528

Mesa vs. Hoare monitors Hoare monitors give special priority to woken-up waiter Signaling thread gives up lock Woken-up waiter acquires lock Signaling thread re-acquires lock after waiter unlocks We’ll stick with Mesa monitors Mesa most common, why? 11/21/201529

Programming with Monitors List shared data needed to solve problem Decide which locks protect which data More locks allows different data to be accessed simultaneously, more complicated One lock usually enough in this class Put lock...unlock calls around code that uses shared data 11/21/201530

Programming with Monitors List ordering constraints One condition variable per constraint Condition variable’s lock should be the lock that protects the shared data used to eval condition Call wait() when thread needs to wait for a condition to be true Use a while loop 11/21/201531

Programming with Monitors Call signal when a condition changes Make sure invariant is established whenever lock is not held E.g., before you call wait 11/21/201532