1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CHAPTER3 Higher-Level Synchronization and Communication
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
1 Mutual Exclusion: Primitives and Implementation Considerations.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
1 Semaphores and Monitors: High-level Synchronization Constructs.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Monitors CSCI 444/544 Operating Systems Fall 2008.
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.
ThreadThread Thread Basics Thread Synchronization Animations.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
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.
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.
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.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Synchronization via Transactions. 2 Concurrency Quiz If two threads execute this program concurrently, how many different final values of X are there?
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Chapter 71 Monitors (7.7)  A high-level-language object-oriented concept that attempts to simplify the programming of synchronization problems  A synchronization.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
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.
pThread synchronization
Operating Systems NWEN 301 Lecture 6 More Concurrency.
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Multithreading Chapter 9.
Monitors, Condition Variables, and Readers-Writers
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Multithreading Chapter 23.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Anthony D. Joseph and Ion Stoica
Thread Synchronization
Producer-Consumer Problem
Semaphores Chapter 6.
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitor Giving credit where it is due:
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
Don Porter Portions courtesy Emmett Witchel
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

1 Condition Synchronization

2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel program.  People are still trying to figure that out. Compromises:  between making it easy to modify shared variables AND  restricting when you can modify shared variables.  between really flexible primitives AND  simple primitives that are easy to reason about.

3 Beyond Locks Synchronizing on a condition.  When you start working on a synchronization problem, first define the mutual exclusion constraints, then ask “when does a thread wait”, and create a separate synchronization variable representing each constraint. Bounded Buffer problem – producer puts things in a fixed sized buffer, consumer takes them out.  What are the constraints for bounded buffer?  1) only one thread can manipulate buffer queue at a time (mutual exclusion)  2) consumer must wait for producer to fill buffers if none full (scheduling constraint)  3) producer must wait for consumer to empty buffers if all full (scheduling constraint)

4 Beyond Locks Locks ensure mutual exclusion Bounded Buffer problem – producer puts things in a fixed sized buffer, consumer takes them out.  Synchronizing on a condition. Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } BoundedBuffer::Deposit(c){ lock  acquire(); while (count == n); //spin Add c to the buffer; count++; lock  release(); } BoundedBuffer::Deposit(c){ lock  acquire(); while (count == n); //spin Add c to the buffer; count++; lock  release(); } BoundedBuffer::Remove(c){ lock  acquire(); while (count == 0); // spin Remove c from buffer; count--; lock  release(); } BoundedBuffer::Remove(c){ lock  acquire(); while (count == 0); // spin Remove c from buffer; count--; lock  release(); } What is wrong with this? What is wrong with this?

5 Beyond Locks Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } BoundedBuffer::Deposit(c){ while (count == n); //spin lock  acquire(); Add c to the buffer; count++; lock  release(); } BoundedBuffer::Deposit(c){ while (count == n); //spin lock  acquire(); Add c to the buffer; count++; lock  release(); } BoundedBuffer::Remove(c){ while (count == 0); // spin lock  acquire(); Remove c from buffer; count--; lock  release(); } BoundedBuffer::Remove(c){ while (count == 0); // spin lock  acquire(); Remove c from buffer; count--; lock  release(); } What is wrong with this? What is wrong with this?

6 Beyond Locks Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } BoundedBuffer::Deposit(c){ if (count == n) sleep(); lock->acquire(); Add c to the buffer; count++; lock->release(); if(count == 1) wakeup(remove); } BoundedBuffer::Deposit(c){ if (count == n) sleep(); lock->acquire(); Add c to the buffer; count++; lock->release(); if(count == 1) wakeup(remove); } BoundedBuffer::Remove(c){ if (count == 0) sleep(); lock->acquire(); Remove c from buffer; count--; lock->release(); if(count==n-1) wakeup(deposit); } BoundedBuffer::Remove(c){ if (count == 0) sleep(); lock->acquire(); Remove c from buffer; count--; lock->release(); if(count==n-1) wakeup(deposit); } What is wrong with this? What is wrong with this?

7 Beyond Locks Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } BoundedBuffer::Deposit(c){ lock  acquire(); if (count == n) sleep(); Add c to the buffer; count++; if(count == 1) wakeup(remove); lock  release(); } BoundedBuffer::Deposit(c){ lock  acquire(); if (count == n) sleep(); Add c to the buffer; count++; if(count == 1) wakeup(remove); lock  release(); } BoundedBuffer::Remove(c){ lock  acquire(); if (count == 0) sleep(); Remove c from buffer; count--; if(count==n-1) wakeup(deposit); lock  release(); } BoundedBuffer::Remove(c){ lock  acquire(); if (count == 0) sleep(); Remove c from buffer; count--; if(count==n-1) wakeup(deposit); lock  release(); } What is wrong with this? What is wrong with this?

8 Beyond Locks Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } Class BoundedBuffer{ … void* buffer[]; Lock lock; int count = 0; } BoundedBuffer::Deposit(c){ while(1) { lock  acquire(); if(count == n) { lock->release(); continue;} Add c to the buffer; count++; lock  release(); break;} BoundedBuffer::Deposit(c){ while(1) { lock  acquire(); if(count == n) { lock->release(); continue;} Add c to the buffer; count++; lock  release(); break;} BoundedBuffer::Remove(c){ while(1) { lock  acquire(); if (count == 0) { lock->release(); continue; } Remove c from buffer; count--; lock  release(); break; }} BoundedBuffer::Remove(c){ while(1) { lock  acquire(); if (count == 0) { lock->release(); continue; } Remove c from buffer; count--; lock  release(); break; }} What is wrong with this? What is wrong with this?

9 Introducing Condition Variables Correctness requirements for bounded buffer producer- consumer problem  Only one thread manipulates the buffer at any time (mutual exclusion)  Consumer must wait for producer when the buffer is empty (scheduling/synchronization constraint)  Producer must wait for the consumer when the buffer is full (scheduling/synchronization constraint) Solution: condition variables  An abstraction that supports conditional synchronization  Condition variables are associated with a monitor lock  Enable threads to wait inside a critical section by releasing the monitor lock.

10 Condition Variables: Operations Three operations  Wait()  Release lock  Go to sleep  Reacquire lock upon return  Java Condition interface await() and awaitUninterruptably()  Notify() (historically called Signal())  Wake up a waiter, if any  Condition interface signal()  NotifyAll() (historically called Broadcast())  Wake up all the waiters  Condition interface signalAll() Implementation  Requires a per-condition variable queue to be maintained  Threads waiting for the condition wait for a notify() Wait() usually specified a lock to be released as a parameter Wait() usually specified a lock to be released as a parameter

11 Implementing Wait() and Notify() Condition::Wait(lock){ schedLock->acquire(); lock->numWaiting++; lock  release(); Put TCB on the waiting queue for the CV; schedLock->release() switch(); lock  acquire(); } Condition::Wait(lock){ schedLock->acquire(); lock->numWaiting++; lock  release(); Put TCB on the waiting queue for the CV; schedLock->release() switch(); lock  acquire(); } Condition::Notify(lock){ schedLock->acquire(); if (lock->numWaiting > 0) { Move a TCB from waiting queue to ready queue; lock->numWaiting--; } schedLock->release(); } Condition::Notify(lock){ schedLock->acquire(); if (lock->numWaiting > 0) { Move a TCB from waiting queue to ready queue; lock->numWaiting--; } schedLock->release(); } Why do we need schedLock? Why do we need schedLock?

12 Using Condition Variables: An Example Coke machine as a shared buffer Two types of users  Producer: Restocks the coke machine  Consumer: Removes coke from the machine Requirements  Only a single person can access the machine at any time  If the machine is out of coke, wait until coke is restocked  If machine is full, wait for consumers to drink coke prior to restocking How will we implement this?  What is the class definition?  How many lock and condition variables do we need?

13 Coke Machine Example Class CokeMachine{ … Storge for cokes (buffer) Lock lock; int count = 0; Condition notFull, notEmpty; } Class CokeMachine{ … Storge for cokes (buffer) Lock lock; int count = 0; Condition notFull, notEmpty; } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.notify(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.notify(); lock  release(); }

14 Coke Machine Example Class CokeMachine{ … Storge for cokes (buffer) Lock lock; int count = 0; Condition notFull, notEmpty; } Class CokeMachine{ … Storge for cokes (buffer) Lock lock; int count = 0; Condition notFull, notEmpty; } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Deposit(){ lock  acquire(); while (count == n) { notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.notify(); lock  release(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; lock  release(); notFull.notify(); } CokeMachine::Remove(){ lock  acquire(); while (count == 0) { notEmpty.wait(&lock); } Remove coke from to the machine; count--; lock  release(); notFull.notify(); } Liveness issue

15 Java syntax for condition variables Condition variables created from locks import java.util.concurrent.locks.ReentrantLock; public static final aLock = new ReentrantLock(); public static ok = aLock.newCondition(); public static int count; aLock.lock(); try { while(count < 16){ok.awaitUninterruptably()} } finally { aLock.unlock(); } return 0;

16 Java syntax for condition variables DON’T confuse wait with await (notify with signal) import java.util.concurrent.locks.ReentrantLock; public static final aLock = new ReentrantLock(); public static ok = aLock.newCondition(); public static int count; aLock.lock(); try { // IllegalMonitorState exception while(count < 16){ok.wait()} } finally { aLock.unlock(); } return 0;

17 Summary Non-deterministic order of thread execution  concurrency problems  Multiprocessing  A system may contain multiple processors  cooperating threads/processes can execute simultaneously  Multi-programming  Thread/process execution can be interleaved because of time-slicing Goal: Ensure that your concurrent program works under ALL possible interleaving Define synchronization constructs and programming style for developing concurrent programs  Locks  provide mutual exclusion  Condition variables  provide conditional synchronization