! Past few lectures: Ø Locks: provide mutual exclusion Ø Condition variables: provide conditional synchronization



Similar documents
Semaphores and Monitors: High-level Synchronization Constructs

Last Class: Semaphores

Lecture 6: Semaphores and Monitors

Monitors, Java, Threads and Processes

Monitors and Semaphores

Outline of this lecture G52CON: Concepts of Concurrency

Lecture 6: Introduction to Monitors and Semaphores

Monitors & Condition Synchronization

Chapter 6 Concurrent Programming

Parallel Programming

Concurrent Programming

CS11 Java. Fall Lecture 7

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Mutual Exclusion using Monitors

COS 318: Operating Systems. Semaphores, Monitors and Condition Variables

3C03 Concurrency: Condition Synchronisation

Thread Synchronization and the Java Monitor

Real Time Programming: Concepts

Distributed Systems [Fall 2012]

Abstract. 1. Introduction. Butler W. Lampson Xerox Palo Alto Research Center David D. Redell Xerox Business Systems

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

4. Monitors. Monitors support data encapsulation and information hiding and are easily adapted to an object-oriented environment.

Hoare-Style Monitors for Java

Concurrent programming in Java

Shared Address Space Computing: Programming

6 Synchronization with Semaphores

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

SYSTEM ecos Embedded Configurable Operating System

Resurrecting Ada s Rendez-Vous in Java

W4118 Operating Systems. Instructor: Junfeng Yang

Chapter 6, The Operating System Machine Level

Embedded Systems. 6. Real-Time Operating Systems

The Little Book of Semaphores

Java Virtual Machine Locks

Processes and Non-Preemptive Scheduling. Otto J. Anshus

MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors

Tasks Schedule Analysis in RTAI/Linux-GPL

Operating Systems Concepts: Chapter 7: Scheduling Strategies

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

The ConTract Model. Helmut Wächter, Andreas Reuter. November 9, 1999

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

Overview. CMSC 330: Organization of Programming Languages. Synchronization Example (Java 1.5) Lock Interface (Java 1.5) ReentrantLock Class (Java 1.

Operating Systems and Networks

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

Figure 30.1: A Parent Waiting For Its Child What we would like to see here is the following output:

COMMMUNICATING COOPERATING PROCESSES

Synchronization. Todd C. Mowry CS 740 November 24, Topics. Locks Barriers

How To Write A Multi Threaded Software On A Single Core (Or Multi Threaded) System

Lecture 8: Safety and Liveness Properties

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

Facing the Challenges for Real-Time Software Development on Multi-Cores

1.4 SOFTWARE CONCEPTS

CS423 Spring 2015 MP4: Dynamic Load Balancer Due April 27 th at 9:00 am 2015

Linux Driver Devices. Why, When, Which, How?

Road Map. Scheduling. Types of Scheduling. Scheduling. CPU Scheduling. Job Scheduling. Dickinson College Computer Science 354 Spring 2010.

Process Scheduling CS 241. February 24, Copyright University of Illinois CS 241 Staff

Deadlock Victim. dimanche 6 mai 12

CSC 2405: Computer Systems II

Concurrent Programming

Chapter 2: OS Overview

Programming Languages

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Java Concurrency Framework. Sidartha Gracias

Priority Based Implementation in Pintos

Understanding the Python GIL

Bounded Cost Algorithms for Multivalued Consensus Using Binary Consensus Instances

Java the UML Way: Integrating Object-Oriented Design and Programming

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Thesis Proposal: Improving the Performance of Synchronization in Concurrent Haskell

Chapter 1 FUNDAMENTALS OF OPERATING SYSTEM

First-class User Level Threads

Introducing Tetra: An Educational Parallel Programming System

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Comp 204: Computer Systems and Their Implementation. Lecture 12: Scheduling Algorithms cont d

Developing MapReduce Programs

Java Memory Model: Content

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Rigorous Software Development CSCI-GA

Programming real-time systems with C/C++ and POSIX

Concurrency and Parallelism

Linux Scheduler. Linux Scheduler

Middleware. Peter Marwedel TU Dortmund, Informatik 12 Germany. technische universität dortmund. fakultät für informatik informatik 12

A Comparison Of Shared Memory Parallel Programming Models. Jace A Mogill David Haglin

Computer Programming I

Tuple spaces and Object spaces. Distributed Object Systems 12. Tuple spaces and Object spaces. Communication. Tuple space. Mechanisms 2.

Project No. 2: Process Scheduling in Linux Submission due: April 28, 2014, 11:59pm

A Survey of Parallel Processing in Linux

Priority Inversion Problem and Deadlock Situations

CSCI E 98: Managed Environments for the Execution of Programs

A Comparative Study on Vega-HTTP & Popular Open-source Web-servers

W4118 Operating Systems. Instructor: Junfeng Yang

Transcription:

1 Synchronization Constructs! Synchronization Ø Coordinating execution of multiple threads that share data structures Semaphores and Monitors: High-level Synchronization Constructs! 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 Proposed by independently Hoare and Hansen in the 1970s 2 Semaphores Key idea of Semaphores vs. Locks! Study these for history and compatibility Ø Don t use semaphores in new code! A non-negative integer variable with two atomic and isolated operations Semaphoreà P() (Passeren; wait) If sem > 0, then decrement sem by 1 Otherwise wait until sem > 0 and then decrement Semaphoreà V() (Vrijgeven; signal) Increment sem by 1 Wake up a thread waiting in P()! 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.! Locks: Mutual exclusion only (1-exclusion)! Semaphores: k-exclusion Ø k == 1, equivalent to a lock Sometimes called a mutex, or binary semaphore Ø k == 2+, up to k threads at a time! Many semaphore implementations use up and down, rather than Dutch names (P and V, respectively) Ø cause how many programmers speak Dutch?! Semaphore starts at k Ø Acquire with down(), which decrements the count Blocks if count is 0 Ø Release with up(), which increments the count and never blocks 3 4 Important properties of Semaphores! Semaphores are non-negative integers! The only operations you can use to change the value of a semaphore are P()/down() and V()/up() (except for the initial setup) Ø P()/down() can block, but V()/up() never blocks! Semaphores are used both for Ø Mutual exclusion, and Ø Conditional synchronization! How many possible values can a binary semaphore take? Ø A. 0 Ø B. 1 Ø C. 2 Ø D. 3 Ø E. 4! 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) 5 6

7 Using Semaphores for Mutual Exclusion 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 int count; Semaphore mutex; Semaphore fullbuffers; Semaphore emptybuffers; Use a separate semaphore for each constraint! Coke machine as a shared buffer! Two types of users Ø Producer: Restocks the coke machine Ø Consumer: Removes coke from the machine! Requirements Ø Only a single person can access the machine at any time Ø If the machine is out of coke, wait until coke is restocked Ø If machine is full, wait for consumers to drink coke prior to restocking! How will we implement this? Ø How many lock and condition variables do we need? A. 1 B. 2 C. 3 D. 4 E. 5 8 Revisiting Coke Machine Example Implementing Semaphores Class CokeMachine{ int count; Semaphore new mutex(1); Semaphores new fullbuffers(0); Semaphores new emptybuffers(numbuffers); Semaphore::P() { if (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread else {value--; Does the order of P matter? Order of V matter? 9 Does this work? Semaphore::V() { if wait queue is not empty { Move a waiting thread to ready queue; else value++; 10 Implementing Semaphores Semaphore::P() { while (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread value--; The Problem with Semaphores! 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 Semaphore::V() { if wait queue is not empty { Move a waiting thread to ready queue; value++; 11 12

13 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 first introduced as 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++, ) Critical Section: Monitors! Basic idea: Ø Restrict programming model Ø Permit access to shared variables only within a critical section! General program structure Ø Entry section Lock before entering critical section Wait if already locked, or invariant doesn t hold Key point: synchronization may involve wait Ø Critical section code Ø Exit section Unlock when leaving the critical section! Object-oriented programming style Ø Associate a lock with each shared object Ø Methods that access shared object are critical sections Ø Acquire/release locks when entering/exiting a method that defines a critical section 14 Remember Condition Variables! Locks Ø Provide mutual exclusion Ø Support two methods Lock::Acquire() wait until lock is free, then grab it Lock::Release() 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 (Java wait()) Signal(): Wake up a waiter, if any (Java notify()) Broadcast(): Wake up all the waiters (Java notifyall()) Ø Two semantics for implementation of wait() and signal() Hoare monitor semantics Hansen (Mesa) monitor semantics So what is the big idea?! (Editorial) Integrate idea of condition variable with language Ø Facilitate proof Ø Avoid error-prone boiler-plate code 15 16 Coke Machine Example Monitor Class CokeMachine{ Lock lock; int count = 0; Condition notfull, notempty; notfull.wait(&lock); Does the order of aquire/while(){wait matter? Order of release/signal matter? while (count == 0) { notempty.wait(&lock); notfull.signal(); Monitors: Recap! Lock acquire and release: often incorporated into method definitions on object Ø E.g., Java s synchronized methods Ø Programmer may not have to explicitly acquire/release! But, methods on a monitor object do execute under mutual exclusion! Introduce idea of condition variable 17 18

19 Hoare Monitors: Semantics! Every monitor function should start with what? Ø A. wait Ø B. signal Ø C. lock acquire Ø D. lock release Ø E. signalall! 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 T1 fn1() x.wait // T1 blocks // T1 resumes Lockà release(); T2 fn4() x.signal T2 resumes // T2 blocks 20 Hansen (Mesa) 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 Ø Not modular correctness depends on correct use and implementation of signal 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 (e.g., Java)! 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); notfull.wait(&lock); 21 22 Problems with Monitors Nested Monitor Calls More Monitor Headaches The priority inversion problem! What happens when one monitor calls into another? Ø What happens to CokeMachine::lock if thread sleeps in CokeTruck::Unload? Ø What happens if truck unloader wants a coke? notfull.wait(&lock); truck->unload(); CokeTruck::Unload(){ while (soda.atdoor()!= coke) { cokeavailable.wait(&lock); Unload soda closest to door; soda.pop(); Signal availability for soda.atdoor();! Three processes (P1, P2, P3), and P1 & P3 communicate using a monitor M. P3 is the highest priority process, followed by P2 and P1.! 1. P1 enters M.! 2. P1 is preempted by P2.! 3. P2 is preempted by P3.! 4. P3 tries to enter the monitor, and waits for the lock.! 5. P2 runs again, preventing P3 from running, subverting the priority system.! A simple way to avoid this situation is to associate with each monitor the priority of the highest priority process which ever enters that monitor. 23 24

25 Comparing Semaphores and Monitors Other Interesting Topics Which is better? A. Semaphore B. Monitors notfull.wait(&lock); notempty.notify(); while (count == 0) { notempty.wait(&lock); notfull.notify();! Exception handling Ø What if a process waiting in a monitor needs to time out?! Naked notify Ø How do we synchronize with I/O devices that do not grab monitor locks, but can notify condition variables.! Butler Lampson and David Redell, Experience with Processes and Monitors in Mesa. 26 Summary! Synchronization Ø Coordinating execution of multiple threads that share data structures! Past lectures: Ø Locks à provide mutual exclusion Ø Condition variables à provide conditional synchronization! Today: Ø 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 27