Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Similar documents
Reminder from last time

CSE 153 Design of Operating Systems

Operating Systems ECE344

CSE 120 Principles of Operating Systems Spring 2016

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Opera&ng Systems ECE344

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Models of concurrency & synchronization algorithms

Threading and Synchronization. Fahd Albinali

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

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

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

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Lecture 6 (cont.): Semaphores and Monitors

CSE 120 Principles of Operating Systems Spring 2016

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

Introduction to OS Synchronization MOS 2.3

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

CS 318 Principles of Operating Systems

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

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

CS510 Operating System Foundations. Jonathan Walpole

CS 318 Principles of Operating Systems

CSCI 5828: Foundations of Software Engineering

Lecture 7: CVs & Scheduling

Lecture 8: September 30

G52CON: Concepts of Concurrency

CMSC 330: Organization of Programming Languages

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

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

Monitors & Condition Synchronization

C340 Concurrency: Semaphores and Monitors. Goals

CS 318 Principles of Operating Systems

Performance Throughput Utilization of system resources

Monitors; Software Transactional Memory

Lecture 11: More on invariants and textual reasoning - examples. K. V. S. Prasad Dept of Computer Science Chalmers University Friday 30 Sep 2016

Synchronization 1. Synchronization

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CS 326 Operating Systems Synchronization. Greg Benson Department of Computer Science University of San Francisco

Last Class: Synchronization

Operating Systems (2INC0) 2017/18

Monitors; Software Transactional Memory

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

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Lecture 9: Introduction to Monitors

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

G52CON: Concepts of Concurrency

Synchronized Methods of Old Versions of Java

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

CMSC 330: Organization of Programming Languages

Concurrent Programming Lecture 10

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

More Types of Synchronization 11/29/16

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

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

Monitors & Condition Synchronisation

Concurrent Programming using Threads

Chapter 32 Multithreading and Parallel Programming

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

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Synchronization 1. Synchronization

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

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

CS533 Concepts of Operating Systems. Jonathan Walpole

Event Counts: Producer-Consumer. Sequencers. Concurrent Systems 8L for Part IB Handout 2. Event Counts & Sequencers. Dr.

Multitasking / Multithreading system Supports multiple tasks

Monitors & Condition Synchronization

Advanced Concepts of Programming

What's wrong with Semaphores?

Lecture 5 Threads and Pthreads II

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

Administrivia. Assignments 0 & 1 Class contact for the next two weeks Next week. Following week

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Only one thread can own a specific monitor

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Semaphores and Monitors: High-level Synchronization Constructs

C09: Process Synchronization

Communication & Synchronization. Lecture 3: Synchronization & Communication - Part 1. Shared variables. Communication Using Shared Memory

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Shared-Memory and Multithread Programming

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO

Java Threads. COMP 585 Noteset #2 1

COMP 322: Fundamentals of Parallel Programming

G52CON: Concepts of Concurrency

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

Operating systems and concurrency (B08)

Concurrency: a crash course

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

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

IV. Process Synchronisation

Lecture 10: Introduction to Semaphores

Process Management And Synchronization

Last Class: Deadlocks. Today

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Transcription:

Lecture 4 Monitors

Summary Semaphores Good news Simple, efficient, expressive Passing the Baton any await statement Bad news Low level, unstructured omit a V: deadlock omit a P: failure of mutex Synchronisation code not linked to the data Synchronisation code can be accessed anywere, but good programming style helps! 2

Monitors A combination of data abstraction and mutual exclusion invented by C.A.R. Hoare [1974] Widely used in concurrent programming languages and libraries Java, pthreads, C#, 3

Key Features A collection of encapsulated procedures a module or a class-like structure A single global lock to ensure mutex for all the operations in the monitor Automatic mutex A special type of variables called condition variables which are used for condition synchronisation Programmed conditional synchronisation 4

Aims Understand classical monitors Examples Standard Variations Pseudo-monitor syntax (similar to the book) Understand Java monitors Built-in Library since Java 5 5

Counter Pseudo-Syntax class becomes monitor Mutex for methods monitor sharedcounter { private int counter = 0; public void increment() { counter++; 6

Condition Variables Global to the monitor Associated to each one is a queue for blocked processes Two operations: wait(c), and signal(c) 7

Monitors Behaviour public methods implementation code p q r c d condition variables 8

A Typical Monitor State boundary (mutex) queue max 1 process executing code p q r c d processes blocked on c s queue Running process Blocked process 9

wait(c) p q r c d blocks caller on c s queue 10

wait(c) A bit like the semaphore operation P wait(c) blocks the executing process on c The blocked process must release the mutex lock on the monitor How is it different from P? 11

signal(c) p q r c d Resumes a process blocked on c s queue continues with the instruction following the wait(c) 12

signal(c) A bit like the semaphore operation V signal(c) unblocks the first process blocked on c What happens with the mutex? How is it different from V? 13

Signal and Exit When a process is woken it resumes execution at the instruction after the wait call. What about mutex? Signal and exit monitors The caller executing signal(c) terminates, and hands over the the mutex lock on the monitor to the unblocked process 14

One-slot Buffer Condition variable naming Producers wait until not full Consumers wait until not empty monitor Buffer<E> { private Condition notfull; private Condition notempty; private boolean isempty = true; private E buf = null; //next slide 15

One-slot Buffer public void put(e e) { if (!isempty) wait(notfull); buf = e; isempty = false; signal(notempty); public E get() { if (isempty) wait(notempty); E result = buf; isempty = true; signal(notfull); return result; 16

N-slot Buffer Flag isempty replaced with a counter monitor Buffer<E> { private Condition notfull; private Condition notempty; private int count = 0; private int front = 0; private int rear = 0; private E buf[n] = (E[])new Object[N]; //next slide 17

N-slot Buffer public void put(e e) { if (count == N) wait(notfull); buf[front] = e; front = (front+1)%n; count++; signal(notempty); public E get() { if (count == 0) wait(notempty); E result = buf[rear]; rear = (rear+1)%n; count--; signal(notfull); return result; 18

Buffer Shootout Semaphores vs Monitors void put(e e) { P(empty); P(mutexP); buf[front] = e; front = (front+1)%n; V(mutexP); V(full); void put(e e) { if (count == N) wait(notfull); buf[front] = e; front = (front+1)%n; count++; signal(notempty); 19

General Shootout Semaphores vs Monitors Semaphores Efficient Expressive: any synchronisation (await-statement) Easy to implement Monitors Can monitors implement semaphores? Important theoretical question An illustrative example, but not normal practice Implementing a low-level language construct in a high-level language is not normally a good idea Can semaphores implement monitors? 20

Semaphore Monitor Monitors can easily implement semaphores monitor Semaphore { private int sv; private Condition notzero; public Semaphore(int sv) { this.sv = sv; //next slide 21

Semaphore Monitor Quite nice, but public void P() { if (sv == 0) wait(notzero); sv--; public void V() { sv++; signal(notzero); 22

Signaling Disciplines So far we have looked at the Signal and Exit version of monitors A signal is at the end of the method Mutex is handed over to any woken process Other possibilities What if the signal is not at the end of the procedure? What is the scheduling can be different? Several possible semantics exist Ben-Ari: 13 in total but most are no sense 23

Signal and What Happens Next? boundary queue B waiting queue W p q r c d signaling process S calls signal(c) 24

Signal and Urgent Wait The original 1974: B<S<W signaling process joins the boundary queue at the front p q r c d unblocked process continues after its wait(c) 25

Signal and Wait B=S<W signaling process joins the boundary queue at the end p q r c d unblocked process continues after its wait(c) 26

Signal and Continue SC: B=W<S unblocked process joins the boundary queue at the end p q r c d signaling process continues 27

Signal and Continue Signaler S continues while Unblocked process W joins the boundary Typical pattern so far: W: if (!cond) { wait(cond); //W blocks imt e S: make cond true; signal(cond); do more stuff cond might change?!?! W: continue, assuming cond 28

Signal and Continue We need to adapt our programming style Use passing the condition technique, or New pattern public void P() { while (sv == 0) wait(notzero); sv--; and take a great care about starvation 29

Signal and Continue Perhaps less intuitive to use, but Preferred signaling discipline today Compatible with priority-based scheduling Has simpler formal semantics Widely used UNIX pthreads Java Some possible advantages: broadcast signal signalall operation 30

SC Semaphore Monitor Quite nice, but public void P() { while (sv == 0) wait(notzero); sv--; public void V() { sv++; signal(notzero); 31

SC Semaphore Monitor Not a fair semaphore: signal(notzero) might be stolen by a process on the boundary queue A fair semaphore possible by passing the condition signaler in effect passes the information that sv value is positive to the signalee Use the empty(cv) primitive to test whether a queue is empty 32

Semaphore Monitor Fair semaphore for all signaling disciplines Passing the condition Monitor invariant is important ( empty(notzero) sv==0) public P() { if (sv == 0) wait(notzero); else sv--; public V() { if (empty(notzero)) sv++; else signal(notzero); 33

Java and Monitors The essence of a monitor is the combination of data abstraction class mutual exclusion synchronized condition variables default: implicit, one per object operations for blocking and unblocking on condition variables included in Object 34

Java 5 and Monitors The essence of a monitor is the combination of data abstraction class mutual exclusion explicit locking package java.util.concurrent.locks condition variables unlimited operations for blocking and unblocking on condition variables 35

Java wait Operations public final void wait() throws InterruptedException Blocks on the object s condition variable The waiting thread releases the synchronization lock associated with the object Note: condition variable is not standard Java terminology! Simply condition is used. 36

Java signal Operations public final void notify() Wakes up a single thread that is waiting on this object's queue public final void notifyall() Wakes up all threads that are waiting on this object's queue 37

Barrier Synchronisation N processes must wait for the slowest before continuing with the next activity Widely used in parallel programming p1 p2 p3 p4 waiting for others to finish continue with next activity 38

Barrier Monitor public class CyclicBarrier { private int arrived = 0; private int N; public CyclicBarrier(int N) { this.n = N; //next slide 39

Barrier Monitor Simple but not 100% reliable solution public synchronized void await() throws InterruptedException { arrived++; if (arrived < N) wait(); else { notifyall(); arrived = 0; 40

Java Passing the Condition Cannot be used directly! Both wait() and condition.await() Spurious wakeup is permitted not notified, not interrupted, no timing out Applications must guard against it Always have waiting inside a while loop while (condition) wait(); 41

Java Semaphore Monitor Unfair public class Semaphore { private int sv; public Semaphore(int sv) { this.sv = sv; public synchronized void P() throws IE { while (sv == 0) wait(); sv--; public synchronized void V() { sv++; notify(); 42

Java Semaphore Monitor Almost- fair Keep a local queue of waiting processes to guarantee fair wakeup public class Semaphore { private int sv; private Queue<Thread> w = new ArrayDeque<Thread>(); public Semaphore(int sv) { this.sv = sv; //next slides 43

Java Semaphore Monitor Almost- fair Signal all waiting threads to make sure that the intended one gets the signal Use notifyall() public synchronized void V() { sv++; notifyall(); 44

Java Semaphore Monitor Almost- fair public synchronized void P() throws InterruptedException { Thread ct = Thread.currentThread(); w.add(ct); while (sv==0!ct.equals(w.peek())) { wait(); w.poll(); sv--; 45

Java Semaphore Monitor Analysis Fairness Wakeup uses local queue Entry into synchronized methods? Java does not specify fairness for the boundary queue Sun s JVM is said to be fair There is apparently at least one know JVM that is using LIFO for the boundary queue True fairness Enter Java 5 46

Fair Semaphore Java 5 Locks Package java.util.concurrent.locks public ReallyFairSemaphore { private int sv; private final Lock lock = new ReentrantLock(true); private final Condition notzero = lock.newcondition(); private Queue<Thread> w = new ArrayDeque<Thread>(); public ReallyFairSemaphore(int sv) { this.sv = sv; //next slides 47

Fair Semaphore Java 5 Locks public void V() { lock.lock(); try { sv++; notzero.signalall(); finally { lock.unlock(); 48

Fair Semaphore Java 5 Locks public void P() throws InterruptedException { lock.lock(); try { Thread ct = Thread.currentThread(); w.add(ct); while (sv==0!ct.equals(w.peek())) { notzero.await(); w.poll(); sv--; finally { lock.unlock(); 49

Thread.interrupt() Two possible effects Internal Thread flag is set, or Causes blocked threads to wake up and raise the InterruptedException Will immediately wake the thread if it tries to block/sleep Difficult to use safely as a programming primitive Can leave objects in hard-to-predict states Nevertheless, very useful for final thread termination if threads can be in blocked state 50

Stopping a Process Java Final thread termination public void run() { try { while (!interrupted()) //Do some work here catch (InterruptedException e) { public void shutdown() { interrupt(); 51

Summary Monitors Allow better structured programming As expressive as semaphores Various monitor signaling semantics Practical side: Java monitors Expressive, though complex More classic problems barrier sync Next time More Java monitors 52