CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Similar documents
Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

CS162 Operating Systems and Systems Programming Lecture 7. Mutual Exclusion, Semaphores, Monitors, and Condition Variables

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

September 23 rd, 2015 Prof. John Kubiatowicz

CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

Operating Systems (1DT020 & 1TT802) Lecture 6 Process synchronisation : Hardware support, Semaphores, Monitors, and Condition Variables

What's wrong with Semaphores?

February 23 rd, 2015 Prof. John Kubiatowicz

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Semaphores and Monitors: High-level Synchronization Constructs

Lecture 8: September 30

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

COMP 300E Operating Systems Fall Semester 2011 Midterm Examination SAMPLE. Name: Student ID:

Last Class: Synchronization

CS162 Operating Systems and Systems Programming Midterm Review"

CS Advanced Operating Systems Structures and Implementation Lecture 8. Synchronization Continued. Goals for Today. Synchronization Scheduling

Lecture 6 (cont.): Semaphores and Monitors

EECS 482 Introduction to Operating Systems

5. Synchronization. Operating System Concepts with Java 8th Edition Silberschatz, Galvin and Gagn

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

EECS 482 Introduction to Operating Systems

CS 153 Design of Operating Systems Winter 2016

CS-537: Midterm Exam (Spring 2001)

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ.

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

CS 318 Principles of Operating Systems

Operating Systems ECE344

W4118 Operating Systems. Instructor: Junfeng Yang

Lecture #7: Implementing Mutual Exclusion

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

CS3502 OPERATING SYSTEMS

Lecture 13 Condition Variables

Locks and Condition Variables Recap. Introducing Monitors. Hoare Monitors: Semantics. Coke Machine Example. Locks. Condition variables

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002

CSE 153 Design of Operating Systems

CS 153 Design of Operating Systems Winter 2016

Synchroniza+on II COMS W4118

Synchronization: Semaphores

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

CSE 153 Design of Operating Systems

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

Opera&ng Systems ECE344

Page 1. CS194-3/CS16x Introduction to Systems. Lecture 6. Synchronization primitives, Semaphores, Overview of ACID.

Concurrency: a crash course

Lecture 9: Midterm Review

Synchronization for Concurrent Tasks

Reminder from last time

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

Last Class: Deadlocks. Today

Page 1. Another Concurrent Program Example" Goals for Today" CS162 Operating Systems and Systems Programming Lecture 4

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

Introduction to Operating Systems

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

PROCESS SYNCHRONIZATION READINGS: CHAPTER 5

Interprocess Communication and Synchronization

CS162 Operating Systems and Systems Programming Lecture 7. Synchronization (Continued) Recall: How does Thread get started?

CS Operating Systems

CS Operating Systems

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Operating Systems (1DT020 & 1TT802)

Chapter 6: Process Synchronization

Today: Synchronization. Recap: Synchronization

Announcements. Class feedback for mid-course evaluations Receive about survey to fill out until this Friday

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Outline Part 2. Semaphores. Semaphore Usage. Objectives: Administrative details:

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Dealing with Issues for Interprocess Communication

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Resource management. Real-Time Systems. Resource management. Resource management

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Lecture 7: CVs & Scheduling

CSE 120 Principles of Operating Systems Spring 2016

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

Main Points. Defini&on. Design pa9ern Example: bounded buffer. Condi&on wait/signal/broadcast

Concept of a process

Synchronization 1. Synchronization

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors

CS370 Operating Systems

PROCESS SYNCHRONIZATION

G52CON: Concepts of Concurrency

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CMSC421: Principles of Operating Systems

Concurrent Processes Rab Nawaz Jadoon

Transcription:

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004 Lecture 8: Semaphores, Monitors, & Condition Variables 8.0 Main Points: Definition of semaphores Example of use of semaphores to solve the bounded buffer problem Definition of monitors and condition variables Demonstration of their use in Producer/Consumer problem semaphore->p(): an atomic operation that waits for semaphore to become positive, then decrements it by 1. (Think of this as the wait operation) semaphore->v(): an atomic operation that increments semaphore by 1, waking up a waiting P, if any. (Think of this as the signal operation) Semaphores are like integers, except: 1. No negative values. 2. Only operations allowed are P and V can t read or write value, except to set it initially. 3. Operations must be atomic: two P s that occur together can t decrement the value below zero. Similarly, thread going to sleep in P won t miss wakeup from V, even if they both happen at about the same time. 8.1 Motivation Writing concurrent programs is hard because you need to worry about multiple concurrent activities reading and writing the same memory. It is hard because ordering matters. Synchronization is a way of coordinating multiple concurrent activities that are using shared state. What are the right synchronization abstractions, to make it easy to build correct concurrent programs? Binary semaphore: like a lock (has a boolean value). Initialized to 1. P waits until value is 1, and then sets it to 0. V sets value to 1, waking up a waiting P, if any. 8.3 Two uses of semaphores 8.3.1 Mutual exclusion (initial value = 1) Binary semaphores can be used for mutual exclusion: initial value of 1; P() is called before the critical section; and V() is called after the critical section. This lecture and the next, present a couple ways of structuring the sharing. 8.2 Definition of Semaphores Semaphores are a kind of generalized lock, first defined by Dijkstra in the late 60 s. Semaphores are the main synchronization primitive used in the original UNIX. Semaphores have a non-negative integer value, and support the following two operations: semaphore->p(); // critical section goes here semaphore->v(); 8.3.2 Scheduling constraints (initial value = 0) Locks are fine for mutual exclusion, but what if you want a thread to wait for something? For example, suppose you had to implement Thread::Join, which must wait for a thread to terminate. By setting the initial value to 0 instead of 1, we can implement waiting on a semaphore: Initial value of semaphore = 0 CS 162 Spring 2004 Lecture 8 1/10 CS 162 Spring 2004 Lecture 8 2/10

Fork Thread::Join calls P Thread finish calls V // will wait until something makes // the semaphore positive. // makes the semaphore positive // and wakes up the thread // waiting in Join. // if 0, no coke in machine Semaphore emptybuffers; // producer s constraint // if 0, nowhere to put more coke Semaphore mutex; // mutual exclusion 8.4 Producer-consumer with a bounded buffer 8.4.1 Problem definition Producer puts things into a shared buffer, consumer takes them out. Need synchronization for coordinating producer and consumer. Example: cpp cc1 cc2 as ld (cpp produces bytes for cc1, which consumes them, and in turn produces bytes for cc2...) Don t want producer and consumer to have to operate in lockstep, so put a fixedsize buffer between them; need to synchronize access to this buffer. Producer needs to wait if buffer is full; consumer needs to wait if buffer is empty. Another example: Coke machine. Producer is delivery person; consumers are students and faculty. Solutions use semaphores for both mutex and scheduling. 8.4.2 Correctness constraints for solution 1. Consumer must wait for producer to fill buffers, if none full (scheduling constraint) 2. Producer must wait for consumer to empty buffers, if all full (scheduling constraint) 3. Only one thread can manipulate buffer queue at a time (mutual exclusion) General rule of thumb: Use a separate semaphore for each constraint. Note how semaphores are being used in multiple ways. Semaphore fullbuffers; // consumer s constraint CS 162 Spring 2004 Lecture 8 3/10 CS 162 Spring 2004 Lecture 8 4/10

8.4.3 Semaphore solution Semaphore fullbuffers = 0 // initially, no coke! Semaphore emptybuffers = numbuffers; // initially, number of empty slots // semaphore used to count how many // resources there are! Semaphore mutex = 1; // no one using the machine Producer() { emptybuffers.p(); // check if there s space // for more coke mutex.p(); // make sure no one else // is using machine put 1 Coke in machine mutex.v(); // ok for others to use machine fullbuffers.v(); // tell consumers there s now a // Coke in the machine Consumer() { fullbuffers.p(); // check if there s a coke in // the machine mutex.p(); // make sure no one else // is using machine take 1 coke out; mutex.v(); // next person s turn emptybuffers.v(); // tell producer we need more 8.5 Motivation for monitors and condition variables Semaphores are a huge step up; just think of trying to do the bounded buffer with only loads and stores. But the problem with semaphores is that they are dual purpose. They re used for both mutex and scheduling constraints. This makes the code hard to read, and hard to get right. Idea in monitors is to separate these concerns: use locks for mutual exclusion and condition variables for scheduling constraints. 8.6 Monitor Definition Monitor: a lock and zero or more condition variables for managing concurrent access to shared data Note: Textbook describes monitors as a programming language construct, where the monitor lock is acquired automatically on calling any procedure in a C++ class. No widely-used language actually does this however! (although Java comes close, with its synchronized objects). In Nachos, and in many real-life operating systems, such as Windows NT, OS/2, or Solaris, monitors are used with explicit calls to locks and condition variables. 8.4.4 Questions Why does producer P + V different semaphores than the consumer? Is order of P s important? Is order of V s important? What if we have 2 producers or 2 consumers? Do we need to change anything? CS 162 Spring 2004 Lecture 8 5/10 CS 162 Spring 2004 Lecture 8 6/10

Monitor with condition variables Simple example: a synchronized list AddToQueue() { shared data put item on queue; data with shared // lock before using // ok to access shared // unlock after done // data Operating System Concepts 6.43 Silberschatz and Galvin 1999 8.6.1 Lock The lock provides mutual exclusion to the shared data. Remember: Lock::Acquire wait until lock is free, then grab it Lock::Release unlock, wake up anyone waiting in Acquire Rules for using a lock: Always acquire before accessing shared data structure Always release after finishing with shared data. Lock is initially free. RemoveFromQueue() { // lock before using shared data if something on queue // ok to access shared data remove it; // unlock after done with shared // data return item; 8.6.2 Condition variables How do we change RemoveFromQueue to wait until something is on the queue? Logically, we want to go to sleep inside of the critical section, but if we hold the lock when we go to sleep, other threads won t be able to get in to add things to the queue, to wake up the sleeping thread. Key idea with condition variables: make it possible to go to sleep inside critical section, by atomically releasing lock at same time we go to sleep CS 162 Spring 2004 Lecture 8 7/10 CS 162 Spring 2004 Lecture 8 8/10

Condition variable: a queue of threads waiting for something inside a critical section Condition variables support three operations: Wait() Release lock, go to sleep, re-acquire lock Releasing lock and going to sleep is atomic Signal() Wake up a waiter, if any Broadcast() Wake up all waiters Rule: must hold lock when doing condition variable operations. Note: In Birrell paper, he says can do signal outside of lock IGNORE HIM (this is only a performance optimization, and likely to lead you to write incorrect code). A synchronized queue, using condition variables: AddToQueue() { put item on queue; condition.signal(); RemoveFromQueue() { while nothing on queue condition.wait(&lock);// release lock; go to // sleep; re-acquire lock remove item from queue; return item; 8.6.3 Mesa vs. Hoare monitors Need to be careful about the precise definition of signal and wait. Mesa-style: (Nachos, most real operating systems) Signaler keeps lock, processor Waiter simply put on ready queue, with no special priority. (in other words, waiter may have to wait for lock) Hoare-style: (most textbooks) Signaler gives up lock, CPU to waiter; waiter runs immediately Waiter gives lock, processor back to signaler when it exits critical section or if it waits again. Above code for synchronized queuing happens to work with either style, but for many programs it matters which one you are using. With Hoare-style, you can change while in RemoveFromQueue to an if, because the waiter only gets woken up if there s an item is on the list. With Mesa-style monitors, waiter may need to wait again after being woken up, because some other thread may have acquired the lock, and removed the item, before the original waiting thread gets to the front of the ready queue. This means as a general principle, you almost always need to check the condition after the wait, with Mesa-style monitors (in other words, use a while instead of an if ). CS 162 Spring 2004 Lecture 8 9/10 CS 162 Spring 2004 Lecture 8 10/10