Auburn University http://www.eng.auburn.edu/~xqin COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
Operating Systems Mehdi Naghavi Winter 1385.
Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Chapter 6: Process Synchronization
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
CS510 Concurrent Systems Introduction to Concurrency.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
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.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
SaUML -- Synchronization- adorned UML diagrams Eileen Kraemer April 23, 2007 Michigan State University.
Condition Variables Condition variables support three operations: Wait – add calling thread to the condition variable’s queue and put the thread to sleep.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
COMP7330/7336 Advanced Parallel and Distributed Computing Pthread RW Locks - Implementation Dr. Xiao Qin Auburn University
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
1 COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Overview Dr. Xiao Qin Auburn University
pThread synchronization
Operating Systems NWEN 301 Lecture 6 More Concurrency.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
COMP7330/7336 Advanced Parallel and Distributed Computing Pthread RW Locks – An Application Dr. Xiao Qin Auburn University
Chien-Chung Shen CIS/UD
Auburn University COMP 3500 Introduction to Operating Systems Resource Allocation Graphs Handling Deadlocks Dr. Xiao.
CS 537 – Introduction to Operating Systems
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice Dr. Xiao Qin.
CS703 - Advanced Operating Systems
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
PARALLEL PROGRAM CHALLENGES
January 29, 2004 Adrienne Noble
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Pthread Mutex Dr. Xiao Qin Auburn University.
CS533 Concepts of Operating Systems Class 3
Semaphores and Condition Variables
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
CS510 Operating System Foundations
Dr. Xiao Qin Auburn University
Too Much Milk With Locks
Too Much Milk With Locks
Sarah Diesburg Operating Systems COP 4610
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Too Much Milk With Locks
Producer-Consumer Problem
Too Much Milk With Locks
Too Much Milk With Locks
Chapter 30 Condition Variables
Coordination Lecture 5.
CS533 Concepts of Operating Systems Class 3
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.
Too Much Milk With Locks
Lecture 12 CV and Semaphores
EECE.4810/EECE.5730 Operating Systems
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Auburn University http://www.eng.auburn.edu/~xqin COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Cont. from Lec05c-Project 3 Cats and Mice Implementation 50 Min: 12 slides

Condition Variables: Functions /* Release lock, put thread to sleep until cv is signaled; when thread wakes up again, re-acquire lock before returning */ void cv_wait(struct cv *c, struct lock *mutex); /* If any threads are waiting on cv, wake up one of them. Caller must hold lock, which must be the same as the lock used in the wait call */ void cv_signal(struct cv *c, struct lock *mutex); References: http://web.stanford.edu/class/cs140/cgi-bin/lecture.php?topic=locks Operations: cv_wait - Release the supplied lock, go to sleep, and, after waking up again, re-acquire the lock. cv_signal - Wake up one thread that's sleeping on this CV. cv_broadcast - Wake up all threads sleeping on this CV.

Condition Variables: Functions (cont.) struct cv *cv_create(const char *name); void cv_destroy(struct cv *); /* Same as signal, except wake up all waiting threads */ void cv_broadcast(struct cv *cv, struct lock *lock); References: http://web.stanford.edu/class/cs140/cgi-bin/lecture.php?topic=locks Operations: cv_wait - Release the supplied lock, go to sleep, and, after waking up again, re-acquire the lock. cv_signal - Wake up one thread that's sleeping on this CV. cv_broadcast - Wake up all threads sleeping on this CV.

Producer/Consumer Implementation with Locks char buffer[SIZE]; int count = 0, head = 0, tail = 0; static struct lock *mutex; mutex = lock_create(“mutex for cv”); void producer(char c) { lock_acquire(mutex); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(mutex); char consumer() { char c; lock_acquire(mutex); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(mutex); return c; Let’s consider the producer/consumer sample to show how to use condition variables We start this example with locks. 3. No need to use static here; just a demonstration

How to handle the empty/full cases using locks? void producer(char c) { lock_acquire(mutex); while (count == SIZE) { lock_release(mutex); } count++; buffer[head] = c; head++; if (head == SIZE) head = 0; char consumer() { char c; lock_acquire(mutex); while (count == 0) { lock_release(mutex); } count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; return c; Which lock_acqure and lock_release are a pair? Which lock_acqure() and lock_release() are a pair? Can we improve this code using wait and signal?

Condition Variables: Data Structure Wait until a variable meets a particular condition There is no actual variable in the CV /* kern/include/synch.h */ struct cv { char *name; // add what you need here }; There is little information on condition variables in the textbook

How to implement cv_wait()? void cv_wait(struct cv *cv, struct lock *lock) { use assert to check input cv and lock; turn off interrupts; release the lock; /*Question: thread_sleep() using cv or lock?*/ sleep the thread until someone signals cv; acquire the lock; turn on interrupts to the previous level; } cv shouldn’t be NULL lock shouldn’t be NULL Wait for cv do not wait for lock Thread_sleep Using cv

How to implement cv_signal()? void cv_signal(struct cv *cv, struct lock *lock) { use assert to check cv and lock; turn off interrupts; /* Question: How to implement the following IF */ if (this thread does not hold lock) panic("cv_signal error: cv %s at %p, lock %s at %p.\n", cv->name, cv, lock->name, lock); /* see also how to wakeup a thread Slide 15 */ wakeup one thread using indicator “cv”; turn on interrupts to the previous level; } cv shouldn’t be NULL lock shouldn’t be NULL

Condition Variables: Sample Usage /* Declare a cv */ static struct cv *sample_cv; /* Initialize the cv */ sample_cv = cv_create(”sample cv"); if (sample_cv == NULL) /* Why panic? */ panic(”sample_cv: Out of memory.\n"); /* Destroy the cv in the end */ cv_destroy(sample_cv); sample_cv = NULL; cv_wait(sample_cv, sample_lock); /* Wait */ cv_signal(sample_cv, sample_lock); /*Signal*/

Producer/Consumer How to use condition variables in OS/161? char buffer[SIZE]; int count = 0, head = 0, tail = 0; static struct lock *mutex; static struct cv *notEmpty; static struct cv *notFull; mutex = lock_create(“mutex for cv”); if (mutex == NULL) panic(”mutex: Out of memory.\n"); notEmpty = cv_create(“Buffer not empty”); notFull = cv_create(“Buffer not full”); if (notEmpty == NULL || notFull == NULL) panic(”CV: Out of memory.\n"); Which lock_acqure and lock_release are a pair?

Producer: how to use condition variables in OS/161? void producer(char c) { lock_acquire(mutex); while (count == SIZE) { cv_wait(notFull, mutex); } count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; cv_signal(notEmpty, mutex); lock_release(mutex);

Consumer: how to use condition variables in OS/161? char consumer() { char c; lock_acquire(mutex); while (count == 0) { cv_wait(notEmpty, mutex); } count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; cv_signal(notFull, mutex); lock_release(mutex); return c;

Summary Handle the empty/full cases using locks Condition Variables: Data Structure How to implement cv_wait()? How to implement cv_signal()?