Chapter 30 Condition Variables

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.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
CS533 Concepts of Operating Systems Class 3 Monitors.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
CS510 Concurrent Systems Introduction to Concurrency.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
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.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
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.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chien-Chung Shen CIS/UD
CS 537 – Introduction to Operating Systems
Interprocess Communication Race Conditions
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
Background on the need for Synchronization
CIS Operating Systems Synchronization
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
CS533 Concepts of Operating Systems Class 3
Monitors, Condition Variables, and Readers-Writers
Thread synchronization
Chien-Chung Shen CIS/UD
Lecture 14: Pthreads Mutex and Condition Variables
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Producer-Consumer Problem
Jonathan Walpole Computer Science Portland State University
Chien-Chung Shen CIS/UD
CS533 Concepts of Operating Systems Class 3
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Implementing Mutual Exclusion
Lecture 14: Pthreads Mutex and Condition Variables
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
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
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Lecture 20: Synchronization
Lecture 12 CV and Semaphores
CS 144 Advanced C++ Programming May 7 Class Meeting
Monitors and Inter-Process Communication
CSE 542: Operating Systems
Chien-Chung Shen CIS/UD
Presentation transcript:

Chapter 30 Condition Variables Chien-Chung Shen CIS/UD cshen@udel.edu

Review of Locks Built from the right combination of hardware (assembly instructions) and OS support (queue, yield, sleep, etc.)

Basic Ideas A thread check whether a condition is true before continuing (parent thread waits for child thread) void *child(void *arg) { printf("child\n"); // how to indicate I am done? return NULL; } main(int argc, char *argv[]) { pthread_t c; printf("parent: begin\n"); Pthread_create(&c, NULL, child, NULL); // create child // how to wait for child? printf("parent: end\n"); return 0; P C

How to Wait for a Condition ? Does a shared variable work here? yes How does this run on one CPU? What is the main issue? spin and waste CPU time What could be a better solution? how to wait for a condition? “Shared” (global) variable volatile int done = 0; void *child(void *arg) { printf("child\n"); done = 1; return NULL; } main(int argc, char *argv[]) { printf("parent: begin\n"); pthread_t c; Pthread_create(&c, NULL, child, NULL); // create child while (done == 0) ; // spin printf("parent: end\n"); return 0;

How to Wait for a Condition ? Ideas: to put the parent to sleep until the condition we are waiting for (e.g., the child is done executing) comes true Condition variable (CV) an explicit queue that threads can put themselves on when some state of execution (i.e., some condition) is not as desired (by waiting on the condition) some other thread, when it changes said state, wake one (or more) of those waiting threads and allow them to continue (by signaling on the condition)

Condition Variable A queue Two operations wait() is executed when a thread wishes to put itself to sleep signal() is executed when a thread has changed something and thus wants to wake a sleeping thread waiting on this condition

Pthread API for CV Pthread_cond_wait(&c, &m): Pthread_cond_signal(&c): release lock m and put calling thread to sleep atomically (assuming m is locked when wait is called) when thread wakes up (after some other thread has signaled it), re-acquire lock m before returning to caller Pthread_cond_signal(&c): signal

Two cases Parent create child and continue to run; call into thr_join() to wait for child to complete parent acquires the lock, check if child is done [it is not], and put itself to sleep by calling wait() (hence release the lock) child eventually runs and calls thr_exit() to wake parent: grabs the lock, set done to 1, signal parent to wake it finally parent runs (return from wait() with lock held), release the lock Child runs immediately upon creation, set done to 1, calls signal to wake a sleeping thread [but there is none, so it just returns] parent then runs, call thr_join(). Sees done is 1, and thus does not wait and returns

Why We Need done? Will the following code work? Assume child runs immediately and calls thr_exit() to signal But there is no thread asleep on CV c When parent runs, it will wait and be stuck (no thread will ever wake it) Variable done records the value both threads are interested in knowing - sleeping, waking, and locking all are built around it

Why We Need Lock for CV? i A subtle race condition Usage of CV // by child A subtle race condition if the parent calls thr_join() and then checks the value of done, it will see that it is 0 and thus try to go to sleep But just before it calls wait() to go to sleep, the parent is interrupted, and the child runs The child changes done to 1 and signals, but no thread is waiting and thus no thread is woken When the parent runs again, it calls wait() and sleeps forever Usage of CV wait() always (a) assumes lock is held when called, (b) releases said lock when putting the caller to sleep, and (c) re-acquires lock just before returning Also, always hold lock while signaling // by parent i

Need both done and Lock void thr_exit() { Pthread_mutex_lock(&m) Pthread_cond_signal(&c); Pthread_mutex_unlock(&m); } Do we need done? failed when child runs immediately and calls thr_exit() done = 1; Does not hold a lock when wait and signal? failed when parent proceeds first (and will sleep forever) void thr_join() { Pthread_mutex_lock(&m); Pthread_cond_wait(&c, &m); Pthread_mutex_unlock(&m); } if (done == 0) Pthread_cond_wait(&c); interrupt

CV Design Pattern

Properties of CV CV is memoryless cv.wait(&lock) atomically release the lock when sleep When a waiting thread is re-enabled via signal or broadcast, it may not run immediately

Producer/Consumer Problem A.k.a. bounded buffer problem – by E. Dijkstra One item buffer Only put data into the buffer when the buffer is empty (count is 0) Only get data from the buffer when the buffer is full (count is 1) What/where are the critical sections? Will lock be enough? need to know when it is OK to put or get data

buffer P C

P C P C P C Efficiency? more buffer Concurrency? more P and C

One Solution Using CV p1-p3 wait for buffer to become empty; c1-c3 wait for buffer to become full Is the code OK for single P and single C ? yes How about two C’s ? C1 waits, P puts one data, and another C2 sneaks in to consume it; C1 wakes up and get ??? if statement is to blame Mesa semantics – signaling a thread only wakes it up; the waken thread does not run immediately Always use while loop

Signaling logic between P and C For single P and single C, this code works One item buffer