Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.

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.
Operating Systems Mehdi Naghavi Winter 1385.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
1 Semaphores and Monitors: High-level Synchronization Constructs.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 Thursday, June 22, 2006 "I think I've got the hang of it now.... :w :q :wq :wq! ^d X exit ^X^C ~. ^[x X Q :quitbye CtrlAltDel ~~q :~q logout save/quit.
A Introduction to Computing II Lecture 18: Concurrency Issues Fall Session 2000.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Process Synchronization
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
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,
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Atomic Operations David Monismith cs550 Operating Systems.
1 CSC 539: Operating Systems Structure and Design Spring 2005 Process synchronization  critical section problem  synchronization hardware  semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided?
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.
11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
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:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
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.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
INTERPROCESS COMMUNICATION Inter process communication (IPC) is a capability supported by operating system that allows one process to communicate with.
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
Deadlock and Starvation
CS703 - Advanced Operating Systems
Chapter 5: Process Synchronization – Part 3
Monitors, Condition Variables, and Readers-Writers
Chapter 6-7: Process Synchronization
CS510 Operating System Foundations
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Semaphores Questions answered in this lecture:
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Critical section problem
Chapter 7: Synchronization Examples
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitor Giving credit where it is due:
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What are the differences between Hoare and Mesa specifications? UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department CS 537 Introduction to Operating Systems Andrea C. Arpaci-Dusseau Remzi H. Arpaci-Dusseau

Two Classes of Synchronization Problems Uniform resource usage with simple scheduling constraints No other variables needed to express relationships Use one semaphore for every constraint Examples: thread join and producer/consumer Complex patterns of resource usage Cannot capture relationships with only semaphores Need extra state variables to record information Use semaphores such that –One is for mutual exclusion around state variables –One for each class of waiting Always try to cast problems into first, easier type Today: Two examples using second approach

Dining Philosophers Problem Statement: N Philosophers sitting at a round table Each philosopher shares a chopstick with neighbor Each philosopher must have both chopsticks to eat Neighbors can’t eat simultaneously Philosophers alternate between thinking and eating Each philosopher/thread i runs following code: while (1) { think(); take_chopsticks(i); eat(); put_chopsticks(i); }

Dining Philosophers: Attempt #1 Two neighbors can’t use chopstick at same time Must test if chopstick is there and grab it atomically Represent each chopstick with a semaphore Grab right chopstick then left chopstick Code for 5 philosophers: sem_t chopstick[5]; // Initialize each to 1 take_chopsticks(int i) { wait(&chopstick[i]); wait(&chopstick[(i+1)%5]); } put_chopsticks(int i) { signal(&chopstick[i]); signal(&chopstick[(i+1)%5]); } What is wrong with this solution???

Dining Philosophers: Attempt #2 Approach Grab lower-numbered chopstick first, then higher-numbered Code for 5 philosophers: sem_t chopstick[5]; // Initialize to 1 take_chopsticks(int i) { if (i < 4) { wait(&chopstick[i]); wait(&chopstick[i+1]); } else { wait(&chopstick[0]); wait(&chopstick[4]); } What is wrong with this solution???

Dining Philosophers: How to Approach Guarantee two goals Safety: Ensure nothing bad happens (don’t violate constraints of problem) Liveness: Ensure something good happens when it can (make as much progress as possible) Introduce state variable for each philosopher i state[i] = THINKING, HUNGRY, or EATING Safety: No two adjacent philosophers eat simultaneously for all i: !(state[i]==EATING && state[i+1%5]==EATING) Liveness: Not the case that a philosopher is hungry and his neighbors are not eating for all i: !(state[i]==HUNGRY && (state[i+4%5]!=EATING && state[i+1%5]!=EATING))

Dining Philosophers: Solution sem_t mayEat[5]; // how to initialize? sem_t mutex; // how to init? int state[5] = {THINKING}; take_chopsticks(int i) { wait(&mutex); // enter critical section state[i] = HUNGRY; testSafetyAndLiveness(i); // check if I can run signal(&mutex); // exit critical section wait(&mayEat[i]); } put_chopsticks(int i) { wait(&mutex); // enter critical section state[i] = THINKING; test(i+1 %5); // check if neighbor can run now test(i+4 %5); signal(&mutex); // exit critical section } testSafetyAndLiveness(int i) { if (state[i]==HUNGRY && state[i+4%5]!=EATING&&state[i+1%5]!=EATING) { state[i] = EATING; signal(&mayEat[i]); }

Dining Philosophers: Example Execution

Monitors Motivation Users can inadvertently misuse locks and semaphores (e.g., never unlock a mutex) Idea Provide language support to automatically lock and unlock monitor lock when in critical section –Lock is added implicitly; never seen by user Provide condition variables for scheduling constraints Examples Mesa language from Xerox Java from Sun –Use synchronized keyword when defining method synchronized deposit(int amount) { balance += amount; }

Condition Variables Idea Used to specify scheduling constraints Always used with a monitor lock No value (history) associated with condition variable Allocate: Cannot initialize value! Must allocate a monitor lock too (implicit with language support, explicit in POSIX and C) pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t monitor_lock = PTHREAD_MUTEX_INITIALIZER; Wait Call with monitor lock held; Releases monitor lock, sleeps until signalled, reacquires lock when woken NOTE: No test inside of wait(); will always sleep! pthread_mutex_lock(&monitor_lock); if (expression) pthread_cond_wait(&cond, &monitor_lock); pthread_mutex_unlock(&monitor_lock);

Condition Variables Signal (or Notify) Call with monitor lock held Wake one thread waiting on this condition variable (if any) Hoare (signal-and-exit): Signaller relinquishes lock and CPU to waiter (Theory) Mesa (signal-and-continue): Signaller can keep lock and CPU (Practice) pthread_mutex_lock(&monitor_lock); pthread_cond_signal(&cond); pthread_mutex_unlock(&monitor_lock); Broadcast (or NotifyAll) Wake all threads waiting on condition variable

Producer/Consumer: Hoare Attempt #1 Final case: Multiple producer threads, multiple consumer threads Shared buffer with N elements between producer and consumer Producer While (1) { mutex_lock(&monitor); cond_wait(&empty,&monitor); myi = findempty(&buffer); Fill(&buffer[myi]); cond_signal(&full); mutex_unlock(&monitor); } Consumer While (1) { mutex_lock(&monitor); cond_wait(&full,&monitor); myj = findfull(&buffer); Use(&buffer[myj]); cond_signal(&empty); mutex_unlock(&monitor); } Shared variables lock_t monitor; cond_t empty, full; Why won’t this work?

Producer/Consumer: Hoare Attempt #2 Producer While (1) { mutex_lock(&monitor); if (slots==N) cond_wait(&empty,&monitor); myi = findempty(&buffer); Fill(&buffer[myi]); slots++; cond_signal(&full); mutex_unlock(&monitor); } Consumer While (1) { mutex_lock(&monitor); if (slots==0) cond_wait(&full,&monitor); myj = findfull(&buffer); Use(&buffer[myj]); slots--; cond_signal(&empty); mutex_unlock(&monitor); } Shared variables lock_t monitor; cond_t empty, full; int slots = 0;

Producer/Consumer: Hoare Example Two producers, two consumers...

Producer/Consumer: Mesa Mesa: Another thread may be scheduled and acquire lock before signalled thread runs Repeat Example: Two producers, two consumers... What can go wrong?

Producer/Consumer: Mesa Mesa: Another thread may be scheduled and acquire lock before signalled thread runs Implication: Must recheck condition with while() loop instead of if() Producer While (1) { mutex_lock(&lock); while (slots==N) cond_wait(&empty,&lock); myi = findempty(&buffer); Fill(&buffer[myi]); slots++; cond_signal(&full); mutex_unlock(&lock); } Consumer While (1) { mutex_lock(&lock); while(slots==0) cond_wait(&full,&lock); myj = findfull(&buffer); Use(&buffer[myj]); slots--; cond_signal(&empty); mutex_unlock(&lock); } Shared variables cond_t empty, full; int slots = 0;