Semaphores and Monitors: High-level Synchronization Constructs

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

Concurrent Programming Issues & Readers/Writers

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

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

February 23 rd, 2015 Prof. John Kubiatowicz

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

Operating Systems ECE344

Opera&ng Systems ECE344

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

Lecture #10: Synchronization wrap up

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

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

r ~ c.. Q.) 0\ 7 < - \1") ::J - ::r 3 ::J,... ::J Q.) 0!:t. !:t. ::J ::J (/') C

CSE 153 Design of Operating Systems

What's wrong with Semaphores?

Condition Synchronization

CS 318 Principles of Operating Systems

CS 318 Principles of Operating Systems

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

CSE 120 Principles of Operating Systems Spring 2016

CSE 120 Principles of Operating Systems Spring 2016

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

CS 318 Principles of Operating Systems

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

Synchroniza+on. Today: Implementa+on issues

CS510 Operating System Foundations. Jonathan Walpole

Lecture 8: September 30

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

September 23 rd, 2015 Prof. John Kubiatowicz

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ.

EECS 482 Introduction to Operating Systems

Critical Section Problem: Example

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

Lecture 7: CVs & Scheduling

Introduction to Operating Systems

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

CSE 153 Design of Operating Systems

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

2 Threads vs. Processes

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

CS 153 Design of Operating Systems Winter 2016

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

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

Last Class: Synchronization

Chapter 6: Process Synchronization

Synchronization 1. Synchronization

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

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

9/17/12! Ion Stoica CS162 UCB Fall 2012!

Concurrency: a crash course

Concept of a process

Reminder from last time

Silberschatz and Galvin Chapter 6

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Lecture 9: Midterm Review

Dealing with Issues for Interprocess Communication

Concurrency and Synchronisation. Leonid Ryzhyk

Lecture #7: Implementing Mutual Exclusion

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

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

Critical Section Problem: Example

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

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

Synchronization. Dr. Yingwu Zhu

Synchronization 1. Synchronization

Concurrency and Synchronisation

Midterm I February 28 th, 2019 CS162: Operating Systems and Systems Programming

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

CSE 120 Principles of Operating Systems Spring 2016

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

Concurrency and Synchronisation

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

EECS 482 Introduction to Operating Systems

Today: Synchronization. Recap: Synchronization

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Topic 4: Synchronization with Semaphores

Outline. Monitors. Barrier synchronization The sleeping barber problem Readers and Writers One-way tunnel. o Monitors in Java

1 Process Coordination

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

CS3502 OPERATING SYSTEMS

Concurrency: Deadlock and Starvation

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

CS 333 Introduction to Operating Systems. Class 6 Monitors and Message Passing. Jonathan Walpole Computer Science Portland State University

Chapter 6: Process Synchronization

Last Class: Deadlocks. Today

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Lecture #7: Shared objects and locks

Chapter 6: Process Synchronization

Operating Systems. Lecture 8

Transcription:

1 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Semaphores and Monitors High-level Synchronization Constructs A Historical Perspective Past few lectures Locks provide mutual exclusion Condition variables provide conditional synchronization Today Historical perspective Semaphores Introduced by Dijkstra in 1960s Main synchronization primitives in early operating systems Monitors Alternate high-level language constructs 2 Semaphores Important properties of Semaphores An abstract data type A non-negative integer variable with two atomic operations Semaphore P() (Passeren; wait) Atomically If sem > 0, then decrement sem by 1 Otherwise wait until sem > 0 Semaphore V() (Vrijgeven; signal) Atomically Increment sem by 1 We assume that a semaphore is fair No thread t that is blocked on a P() operation remains blocked if the V() operation on the semaphore is invoked infinitely often In practice, FIFO is mostly used, transforming the set into a queue. Semaphores are non-negative integers The only operations you can use to change the value of a semaphore are P() and V() (except for the initial setup) P() can block, but V() never blocks Semaphores are used both for Mutual exclusion, and Conditional synchronization Two types of semaphores Binary semaphores Can either be 0 or 1 General/Counting semaphores Can take any non-negative value Binary semaphores are as expressive as general semaphores (given one can implement the other) 3 4

5 Using Semaphores for Mutual Exclusion Revisiting Coke Machine Example Use a binary semaphore for mutual exclusion Semaphore = new Semaphore(1); Semaphore P(); Critical Section; Semaphore V(); Using Semaphores for producer-consumer with bounded buffer Semaphore mutex; Semaphore fullbuffers; Semaphore emptybuffers; Use a separate semaphore for each constraint Class CokeMachine{ Semaphore new mutex(1); Semaphores new fullbuffers(0); Semaphores new emptybuffers(numbuffers); emptybuffers P(); fullbuffers V(); CokeMachineRemove(){ fullbuffers P(); Remove coke from to the machine; emptybuffers V(); 6 Comparing code Implementing Semaphores while (count == n) { notfull.wait(&lock); count++; notempty.signal(); emptybuffers P(); fullbuffers V(); emptybuffers P(); fullbuffers V(); Does the order of P matter? V? 7 SemaphoreP() { Disable interrupts; if (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread else {value--; Enable interrupts; SemaphoreV() { Disable interrupts; if wait queue is not empty { Move a waiting thread to ready queue; else {value++; Enable interrupts; 8

9 Implementing Semaphores The Problem with Semaphores SemaphoreP() { Disable interrupts; while (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread value--; Enable interrupts; SemaphoreV() { Disable interrupts; if wait queue is not empty { Move a waiting thread to ready queue; value++; Enable interrupts; Semaphores are used for dual purpose Mutual exclusion Conditional synchronization Difficult to read/develop code Waiting for condition is independent of mutual exclusion Programmer needs to be clever about using semaphores emptybuffers P(); fullbuffers V(); CokeMachineRemove(){ fullbuffers P(); Remove coke from to the machine; emptybuffers V(); 10 Introducing Monitors Separate the concerns of mutual exclusion and conditional synchronization What is a monitor? One lock, and Zero or more condition variables for managing concurrent access to shared data General approach Collect related shared data into an object/module Define methods for accessing the shared data Monitors were first introduced as a programming language construct Calling a method defined in the monitor automatically acquires the lock Examples Mesa, Java (synchronized methods) Monitors also define a programming convention Can be used in any language (C, C++, ) Locks and Condition Variables Recap Locks Provide mutual exclusion Support two methods LockAcquire() wait until lock is free, then grab it LockRelease() release the lock, waking up a waiter, if any Condition variables Support conditional synchronization Three operations Wait() Release lock; wait for the condition to become true; reacquire lock upon return Signal() Wake up a waiter, if any Broadcast() Wake up all the waiters Two semantics for the implementation of wait() and signal() Hoare monitor semantics Hansen monitor semantics 11 12

13 Coke Machine Example Hoare Monitors Semantics while (count == n) { notfull.wait(&lock); count++; notempty.signal(); Class CokeMachine{ Lock lock; int count = 0; Condition notfull, notempty; CokeMachineRemove(){ while (count == 0) { notempty.wait(&lock); Remove coke from to the machine; count--; notfull.signal(); Hoare monitor semantics Assume thread T1 is waiting on condition x Assume thread T2 is in the monitor Assume thread T2 calls x.signal T2 gives up monitor, T2 blocks! T1 takes over monitor, runs T1 gives up monitor T2 takes over monitor, resumes Example fn1() x.wait // T1 blocks // T1 resumes Lock release(); fn4() x.signal T2 resumes // T2 blocks 14 Hansen Monitors Semantics Hansen monitor semantics Assume thread T1 waiting on condition x Assume thread T2 is in the monitor Assume thread T2 calls x.signal; wake up T1 T2 continues, finishes When T1 get a chance to run,t1 takes over monitor, runs T1 finishes, gives up monitor Example Tradeoff Hoare Claims Cleaner, good for proofs When a condition variable is signaled, it does not change Used in most textbooks but Inefficient implementation Hansen Signal is only a hint that the condition may be true Need to check condition again before proceeding Can lead to synchronization bugs Used by most systems Benefits Efficient implementation Condition guaranteed to be true once you are out of while! fn1() x.wait // T1 resumes // T1 finishes // T1 blocks fn4() x.signal // T2 continues // T2 finishes if (count == n) { notfull.wait(&lock); count++; notempty.signal(); while (count == n) { notfull.wait(&lock); count++; notempty.signal(); 15 16

17 Hansen v. Hoare semantics The priority inversion problem Summary Consider a set of communicating processes with varying priority With Hoare semantics a low priority process can delay the progress of a high priority process process P begin deposit() end P High-Priority Producer signal(notempty) Shared Buffers wait(notempty) process Q begin remove() end Q process R begin remove() end R High- Priority Consumer Low- Priority Consumer Synchronization Coordinating execution of multiple threads that share data structures Past lectures Locks provide mutual exclusion Condition variables provide conditional synchronization Today Historical perspective Semaphores Introduced by Dijkstra in 1960s Two types binary semaphores and counting semaphores Supports both mutual exclusion and conditional synchronization Monitors Separate mutual exclusion and conditional synchronization 18

1 Summary of Our Discussions Developing and debugging concurrent programs is hard Non-deterministic interleaving of instructions Concurrent Programming Issues Summary Synchronization constructs Locks mutual exclusion Condition variables conditional synchronization Other primitives Semaphores Binary vs. counting Can be used for mutual exclusion and conditional synchronization How can you use these constructs effectively? Develop and follow strict programming style/strategy 2 Programming Strategy General Programming Strategy Decompose the problem into objects Object-oriented style of programming Identify shared chunk of state Encapsulate shared state and synchronization variables inside objects Two step process Threads Identify units of concurrency these are your threads Identify chunks of shared state make each shared thing an object; identify methods for these objects (how will the thread access the objects?) Write down the main loop for the thread Shared objects Identify synchronization constructs Mutual exclusion vs. conditional synchronization Create a lock/condition variable for each constraint Develop the methods using locks and condition variables for coordination 3 4

5 Coding Style and Standards Always do things the same way Always use locks and condition variables Always hold locks while operating on condition variables Always acquire lock at the beginning of a procedure and release it at the end If it does not make sense to do this split your procedures further Always use while to check conditions, not if while (predicate on state variable) { conditionvariable wait(&lock); ; (Almost) never sleep() in your code Use condition variables to synchronize Readers/Writers A Complete Example Motivation Shared databases accesses Examples bank accounts, airline seats, Two types of users Readers Never modify data Writers read and modify data Problem constraints Using a single lock is too restrictive Allow multiple readers at the same time but only one writer at any time Specific constraints Readers can access database when there are no writers Writers can access database when there are no readers/writers Only one thread can manipulate shared variables at any time 6 Readers/Writer Solution Structure Basic structure two methods DatabaseRead() { Wait until no writers; Access database; check out wake up waiting writers; Solution Details Readers Public DatabaseRead() { StartRead(); Access database; DoneRead(); State variables DatabaseWrite() { Wait until no readers/writers; Access database; check out wake up waiting readers/writers; AR = 0; // # of active readers AW = 0; // # of active writers WR = 0; // # of waiting readers WW = 0; // # of waiting writers Condition oktoread; Condition oktowrite; Lock lock; Provate DatabaseStartRead() { lock.acquire(); while ((AW+WW) > 0) { WR++; oktoread.wait(&lock); WR--; AR++; lock.release(); Provate DatabaseDoneRead() { lock.acquire(); AR--; if (AR ==0 && WW > 0) { oktowrite.signal(); lock.release(); 7 8

9 Solution Details Writers DatabaseWrite() { StartWrite(); Access database; DoneWrite(); Provate DatabaseStartWrite() { lock.acquire(); while ((AW+AR) > 0) { WW++; oktowrite.wait(&lock); WW--; AW++; lock.release(); Provate DatabaseDoneWrite() { lock.acquire(); AW--; if (WW > 0) { oktowrite.signal(); else if (WR > 0) { oktoread.broadcast(); lock.release();