Operating Systems (2INC0) 2017/18

Similar documents
Operating Systems ECE344

CSE 153 Design of Operating Systems

CSE 120 Principles of Operating Systems Spring 2016

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Opera&ng Systems ECE344

More Types of Synchronization 11/29/16

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

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

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Operating systems and concurrency (B08)

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

More Shared Memory Programming

Lecture 6 (cont.): Semaphores and Monitors

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

Synchronization (Part 2) 1/40

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

CSE 120 Principles of Operating Systems Spring 2016

Operating Systems 2010/2011

W4118 Operating Systems. Instructor: Junfeng Yang

Synchroniza+on II COMS W4118

Reminder from last time

Threads need to synchronize their activities to effectively interact. This includes:

CS 318 Principles of Operating Systems

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

EECS 482 Introduction to Operating Systems

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

Concept of a process

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Lecture 8: September 30

Concurrency: Signaling and Condition Variables

IT 540 Operating Systems ECE519 Advanced Operating Systems

Semaphore Use In Synchronization

CSE 120 Principles of Operating Systems Spring 2016

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

Synchronization. Dr. Yingwu Zhu

Paralleland Distributed Programming. Concurrency

Concurrent Programming Lecture 10

Concurrency: a crash course

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Project 3-2. Mutex & CV

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

Multithreading Programming II

Process Management And Synchronization

CS 318 Principles of Operating Systems

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

1 Process Coordination

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

IV. Process Synchronisation

POSIX Condition Variables ADT

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Synchronization Mechanisms

Monitors & Condition Synchronization

Solving the Producer Consumer Problem with PThreads

Synchronization I. Jo, Heeseung

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

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

Reminder from last time

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

CMSC421: Principles of Operating Systems

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS11 Java. Fall Lecture 7

What's wrong with Semaphores?

CS3502 OPERATING SYSTEMS

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

Models of concurrency & synchronization algorithms

Operating Systems 2006/2007

Lecture 7: CVs & Scheduling

CS510 Operating System Foundations. Jonathan Walpole

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Condition Variables. Dongkun Shin, SKKU

CS533 Concepts of Operating Systems. Jonathan Walpole

Real Time Operating Systems and Middleware

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

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

CS3733: Operating Systems

The University of Texas at Arlington

CSE Traditional Operating Systems deal with typical system software designed to be:

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

ANSI/IEEE POSIX Standard Thread management

Chapter 6: Process Synchronization

February 23 rd, 2015 Prof. John Kubiatowicz

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

The University of Texas at Arlington

Locks and semaphores. Johan Montelius KTH

EEE3052: Introduction to Operating Systems. Fall Project #3

CS 153 Design of Operating Systems Winter 2016

PROCESS SYNCHRONIZATION

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

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

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Today: Synchronization. Recap: Synchronization

Synchronising Threads

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

Sharing Objects Ch. 3

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

POSIX Threads. HUJI Spring 2011

Dealing with Issues for Interprocess Communication

Condition Variables. Dongkun Shin, SKKU

Transcription:

Operating Systems (2INC0) 2017/18 Condition Synchronization (07) Dr. Courtesy of Prof. Dr. Johan Lukkien System Architecture and Networking Group

Agenda Condition synchronization motivation condition variables monitors scheduling / signalling disciplines 2

Agenda Condition synchronization motivation condition variables monitors scheduling / signalling disciplines 3

Recall: Last lecture Cooperation à Synchronization N concurrent processes working for a common goal goal1: avoid program traces that may violate a certain invariant goal2: steer the execution to have some property e.g. such that a certain assertion holds at certain control points Typically, by blocking thread execution until an assertion becomes true. Competition à Mutual exclusion N concurrent processes competing for a resource repeatedly executing a critical section (CS) Introduce extra synchronization requirement: Multiple processes cannot be in their CS at the same time 4

Condition synchronization: motivation Action (or event) synchronization the invariant refers to ordering of actions: à counting actions is enough BUT: Just counting does not solve all synchronization problems e.g. modification that cannot easily be seen as counting x : = y+z e.g. conditions that contain disjunctions (OR operations) wait until x=0 y=0 Condition synchronization the invariant refers to a certain (combined) state needs explicit communication (signalling) between processes Communicating via shared variables is not good enough! 5

Condition synchronization: motivation Needed when just counting is not enough to solve a synchronization problem Simplifies sequences of semaphore operations P(a); P(a); P(b); P(m);... Two condition synchronization principles: At places where the truth of a condition is required: check and block At places where a condition may have become true: signal waiters 6

Agenda Condition synchronization motivation condition variables monitors scheduling / signalling disciplines 7

Condition variables (basic operations) var cv: Condition; (Boolean function B defined on cv) 4 basic operations on variable cv: Wait(..., cv) suspend execution of caller Signal(cv) free one process/thread suspended on Wait(cv) (void if there is none) Sigall(cv) free all processes suspended on Wait(cv) Empty(cv) there is no process suspended on Wait(cv) (boolean function, returns true or false) Many extra operations in specific implementations. 8

Example Maintain x 0 in a program with arbitrary assignments to x. var cv: Condition; m: Semaphore (initially 1) 2) Allow others to join waiting queue 3) Only one waiter at a time is allowed to: re-check & leave the loop [... [... 4) Beware! P(m); P(m); Sigall(cv) may be called while x<10 do before Wait(cv) à too early! V(m); { Beware... } Wait (cv); P(m); od; { x 10 } x := x-10; x := x+100; Sigall (cv); V(m); V(m); ] ] 1) Releases all proc blocked on Wait(cv) --------------------------- Make sure all processes on the left reach Wait(cv) and they all get a chance to proceed! So: V(m); Wait(cv); P(m) 9

Combine: condition variable & semaphore Need to combine V(m); Wait(cv) atomically: define Wait(m,cv): <V(m); Wait(cv)>; P(m) var cv: Condition; m: Semaphore (initially 1) [... [... P(m); P(m); while x<10 do x := x+100; Wait (m, cv); Sigall (cv); od; V(m); { x 10 }... x := x-10; ] V(m); ] 10

Program structure Structure program in condition critical regions check truth of condition for executing the critical section use a repetition rather than a (e.g. if) selection, to be safe take competition into account, e.g. more instances decide carefully which variables must be protected together usually, these occur together in at least one condition access to these variables only within critical sections reads in other parts are harmless, but unstable A typical region looks like: mutex lock while not condition do Wait od; critical section possible signals mutex unlock 11

Using condition variables Signalling and waiting must be in critical sections needs an associated exclusion mechanism (e.g. a semaphore) cv is sometimes extended with a timeout mechanism. recheck even though there is no signal. Each condition variable cv is associated with a condition B(cv) Signaling means B(cv) may have become true. B(cv) can be a simple condition such as (x 10) or a combined condition such as B(cv)=(x 10 V y 5 V x+y 12) alternatively, use 3 condition vars B(cv1)=(x 10), B(cv2)=(y 5), B(cv3)=(x+y 12) leading to a single semaphore associated with all critical sections (e.g. POSIX). Usually, a single condition var. is used for all conditions hence, using more than one is an efficiency choice For example: JAVA allows a single implicit condition var. per object (actually, JAVA provides a Monitor mechanism) Rule: signal when a condition might have become true. 12

Signalling strategies The only problem remaining is the choice of signalling type. Two signalling strategies Signal one waiter of cv satisfying: B(cv) is true and there are waiters on it à Empty(cv)=false Sigall all waiters of the condition variable (of which the associated condition may have changed from false to true.) Which one to choose: Signal or Sigall Careful analysis leads to efficiency. Sigall is always correct, but inefficient. 13

POSIX: condition variables (1003.1c) Usage: 1) mutex lock 2) while not condition do Wait od; 3) critical section 4) possible signals 5) mutex unlock pthread_cond_t cond = PTHREAD_COND_INITIALIZER; status = pthread_cond_init (&cond, attr); /* should return 0 */ status = pthread_cond_destroy (&cond); /* idem */ status = pthread_cond_wait (&cond, m); /* semaphore m is associated with * all critical sections */ status = pthread_cond_timedwait (&cond, m, exp); /* exp: max. waiting time; returns * ETIMEDOUT after exp.*/ status = pthread_cond_signal (&cond); /* signal one waiter */ status = pthread_cond_broadcast (&cond); /* signal all waiters */ 14

Agenda Condition synchronization motivation condition variables monitors scheduling / signalling disciplines 15

Monitors Definition: a monitor is combination of data and operations (procedures, methods) on data in fact: an object, like in object-oriented languages Procedures of a monitor are executed under mutual exclusion. Procedure executing is said to be inside the monitor (only 1 can be inside) Synchronization is through condition variables. The effect on the caller of a signal depends on the monitor implementation some implementations give up the monitor immediately this signalling semantics is called: signalling discipline Communication is through the local variables of the monitor. 16

Example: just mutual exclusion Monitor Exclusion = [ { local variables } Proc CritSect1 = [ { implementation } ] ; Proc CritSect2 = [ { implementation } ] { initialization } ] { global variables } var Excl: Exclusion; Proc User1 = [ while true do... Excl.CritSect1;... od ] Proc User2 = [ while true do... Excl.CritSect2;... od ] 17

Example: readers-writers problem Reader Writer Database Reader Writer At any given time several readers (and no writers) or one writer (and no readers) may be active. à read/write actions are critical 18

Formalization Add entry and exit protocols around the read and write actions, in a similar way as for mutual exclusion. Variables nr and nw record the numbers of readers and writers. Proc Reader = [ while true do... nr := nr+1; Read actions ; nr := nr-1;... od ] Proc Writer = [ while true do... nw := nw+1; Write actions ; nw := nw-1;... od ] What is the invariant? Maintain I: (nr = 0 nw = 0) (nw 1) 19

Maintain the invariant From topology: I0: 0 nr I1: 0 nw From topology: precondition before the decrements of nr (resp. nw): nr>0 nw>0 From topology: compute preconditions for those statements that might disturb the invariant. I(nr := nr+1) = { substitution } = (nr+1= 0 nw = 0) nw 1 (nr = -1 nw = 0) nw 1 = { topology: nr 0 } nw = 0 I(nw := nw+1) = { substitution } The other assignments (the decrements) are true since they won t disturb I. = (nr= 0 nw+1= 0) (nw+1 1) (nr = 0 nw = -1) nw 0 = { topology: nw 0 } nr = 0 nw = 0 20

Required assertions Proc Reader = [ while true do... { nw=0 } nr := nr+1; Read actions ; { true } nr := nr-1;... od ] Proc Writer = [ while true do... { nr=0 nw=0 } nw := nw+1; Write actions ; { true } nw := nw-1;... od ] The monitor method requires that all accesses to the relevant variables are in critical sections. The italic (red) parts must be in critical sections. The statements don t have to be atomic. 21

Four critical sections { nw=0 } nr := nr+1; A { nr = 0 nw=0 } nw := nw+1; C { true } nr := nr-1; B { true } nw := nw-1; D Method: Introduce one condition variable per assertion; Introduce a monitor, with a method per critical section; Perform a systematic translation/implementation. 22

Adapting the text { global variable } var RW: ReaderWriter; Proc Reader = [ while true do... RW.Rentry; Read actions ; RW.Rexit; od ] Proc Writer = [ while true do... RW.Wentry; Write actions ; RW.Wexit od ] 23

Full program text of ReaderWriter Monitor ReaderWriter = [ var nr, nw: int; Rblock, { nw=0 } Wblock { nw=0 nr=0 }: Condition; Proc Rentry = [ while nw>0 do Wait (Rblock) od; { nw=0 } nr := nr+1 MonExit ] ; Proc Rexit = { nr>0 nw=0 } [ nr := nr-1; MonExit ] ; Proc Wentry = [ while nw>0 nr>0 do Wait (Wblock) od; { nw=0 nr=0 } nw := nw+1 MonExit ] ; Proc Wexit = { nr=0 nw>0 } [ nw := nw-1; MonExit ] ; { initialization } nr := 0; nw := 0 ] 24

MonExit: Many possibilities... This is where the signalling takes place. Proc MonExit = [ if Empty(Wblock) nw=0 nr=0 then Signal (Wblock) else if Empty(Rblock) nw=0 then Signal (Rblock) fi fi ] ; Proc MonExit = [ if nw=0 then Signal (Rblock) fi; if nw=0 nr=0 then Signal (Wblock) fi ] ; Proc MonExit = [ Signal (Rblock); Signal (Wblock) ] ; 25

Alternatively,... Sigall every condition that may have become true during critical section. maintain system invariants, while blocking only if guard holds. Proc Rentry = [ while nw>0 do Wait (Rblock) od; nr := nr+1 ] ; Proc Rexit = [ nr := nr-1; if nr=0 then Sigall (Wblock) fi { or Signal } ] ; Proc Wentry = [ while nw>0 nr>0 do Wait (Wblock) od; nw := nw+1 ] ; Proc Wexit = [ nw := nw-1; Sigall (Rblock); Sigall (Wblock) { or Signal } ] ; The signaller did not really have to check if { nr=0 } holds or not. It could as well rely on the signalled processes to (re)check if the condition { nw=0 nr=0 } holds. But, would that be efficient? 26

Agenda Condition synchronization motivation condition variables monitors scheduling / signalling disciplines 27

Structuring by monitors Condition variables are rather useless without combining with proper mutual exclusion. Monitors provide the exclusion. At most one thread may be inside the monitor at any given time. Programs with monitors employ: active processes, passive monitors relevant shared variables are encapsulated well-defined operations on shared variables Question: Which process is in the monitor right after the signal? The answer depends on the signalling discipline. 28

Scheduling/signalling disciplines The discipline defines what happens to the monitor upon a signal. Four disciplines are used in various implementations signal & exit signal & continue signal & wait signal & urgent wait Correctness of solution depends on signalling discipline! 29

Signal and Exit The process executing a signal exits the monitor immediately. Monitor access is given to a waiter on that variable, if any. Sigall is not supported Signalling strategy: upon leaving the monitor, a process performs at most one signal on a condition variable that is non-empty and for which the corresponding condition holds. Property: The condition, i.e. B(cv), holds after Wait. 30

Signal and Continue The process executing a signal continues to use the monitor, until a Wait or until the end of the routine. Any processes released by this signal compete with the other processes to obtain monitor access again. Signalling strategy: during execution of a monitor routine all relevant conditions are signalled. after each wait, the condition is evaluated again. Property: The condition may or may not hold after Wait. 31

Signal and (Urgent) Wait The process executing a signal is stopped in favour of the signalled process. The signaller waits to access the monitor again after the Signal, and competes with all other processes again. Urgent wait: signaller has priority over other users. Signalling strategy: similar to Signal & Exit. signalling is done upon leaving the monitor. Property: The condition holds after Wait. 32

General strategies Use a repetition to evaluate and wait on a guard. most disciplines have unpredictability in the choice of the next process in the monitor Upon each monitor exit, SIGNAL: employ cascaded wakeup by using just Signal(cv) if B(cv) holds and cv is not empty, SIGALL: wake up the entire group waiting on each variable cv for which B(cv) may have become true. Signal at the end of a routine. 33

General strategies (cnt d) Do not change the state before waiting (otherwise, signalling has to occur there as well). Beware that monitor entry and exits include the Wait(). By not modifying the state, signalling is not needed before the Wait() Two ways to determine which variable to signal: Just Signal (resp. Sigall in 2 nd strategy) each cv with a true guard. Evaluate the status of each cv and don t signal more than necessary. E.g. only wakeup a single waiter, which then needs to properly cascade this to other waiters. In Sigall strategy several conditions per cv can be used; the Sigall must then be performed if any of these conditions may have become true. e.g. Java has a single, implicit cv per class. 34

Notes Too much signalling is not wrong! It may only be less efficient. Most signalling disciplines are unfair because of competition between signalled processes and new processes. In principle, one condition variable is enough (as in Java), more condition variables may increase efficiency. 35

Synchronization: summary Action synchronization allows systematically introducing semaphores requires explicit synchronization invariants on actions / action counts. Condition synchronization principle: At places where the truth of a condition is required: check and block At places where a condition may have become true: signal waiters Signalling strategy vs efficiency. 36