Operating Systems (2INC0) 2017/18

Size: px
Start display at page:

Download "Operating Systems (2INC0) 2017/18"

Transcription

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

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

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

4 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

5 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

6 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

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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

Operating Systems ECE344

Operating Systems ECE344 Operating Systems ECE344 Ding Yuan Announcement & Reminder Lab 0 mark posted on Piazza Great job! One problem: compilation error I fixed some for you this time, but won t do it next time Make sure you

More information

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems CSE 153 Design of Operating Systems Winter 2018 Lecture 10: Monitors Monitors A monitor is a programming language construct that controls access to shared data Synchronization code added by compiler, enforced

More information

CSE 120 Principles of Operating Systems Spring 2016

CSE 120 Principles of Operating Systems Spring 2016 CSE 120 Principles of Operating Systems Spring 2016 Condition Variables and Monitors Monitors A monitor is a programming language construct that controls access to shared data Synchronization code added

More information

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions CS 470 Spring 2018 Mike Lam, Professor Semaphores and Conditions Synchronization mechanisms Busy-waiting (wasteful!) Atomic instructions (e.g., LOCK prefix in x86) Pthreads Mutex: simple mutual exclusion

More information

Opera&ng Systems ECE344

Opera&ng Systems ECE344 Opera&ng Systems ECE344 Lecture 6: Synchroniza&on (II) Semaphores and Monitors Ding Yuan Higher- Level Synchroniza&on We looked at using locks to provide mutual exclusion Locks work, but they have some

More information

More Types of Synchronization 11/29/16

More Types of Synchronization 11/29/16 More Types of Synchronization 11/29/16 Today s Agenda Classic thread patterns Other parallel programming patterns More synchronization primitives: RW locks Condition variables Semaphores Message passing

More information

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

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018 CS 31: Intro to Systems Misc. Threading Kevin Webb Swarthmore College December 6, 2018 Classic thread patterns Agenda Pthreads primitives and examples of other forms of synchronization: Condition variables

More information

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

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois 1 Synchronization primitives Mutex locks Used for exclusive access to a shared resource (critical section) Operations:

More information

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data

More information

Operating systems and concurrency (B08)

Operating systems and concurrency (B08) Operating systems and concurrency (B08) David Kendall Northumbria University David Kendall (Northumbria University) Operating systems and concurrency (B08) 1 / 20 Introduction Semaphores provide an unstructured

More information

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

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018 CIS 3207 - Operating Systems Application of Semaphores Professor Qiang Zeng Spring 2018 Big picture of synchronization primitives Busy-waiting Software solutions (Dekker, Bakery, etc.) Hardware-assisted

More information

More Shared Memory Programming

More Shared Memory Programming More Shared Memory Programming Shared data structures We want to make data structures that can be shared by threads. For example, our program to copy a file from one disk to another used a shared FIFO

More information

Lecture 6 (cont.): Semaphores and Monitors

Lecture 6 (cont.): Semaphores and Monitors Project 1 Due Thursday 10/20 Lecture 6 (cont.): Semaphores and Monitors CSE 120: Principles of Operating Systems Alex C. Snoeren Higher-Level Synchronization We looked at using locks to provide mutual

More information

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

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data 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

More information

Synchronization (Part 2) 1/40

Synchronization (Part 2) 1/40 1/40 Learning Objectives Understand and apply the monitor concept to solve synchronization problems in concurrent programs Understand and apply the concept of condition variables in monitors Understand

More information

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

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables 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

More information

CSE 120 Principles of Operating Systems Spring 2016

CSE 120 Principles of Operating Systems Spring 2016 CSE 120 Principles of Operating Systems Spring 2016 Semaphores and Monitors Higher-Level Synchronization We looked at using locks to provide mutual exclusion Locks work, but they have limited semantics

More information

Operating Systems 2010/2011

Operating Systems 2010/2011 Operating Systems 2010/2011 Blocking and locking (with figures from Bic & Shaw) Johan Lukkien 1 Blocking & locking Blocking: waiting for a certain condition to become true Starvation: unpredictable, even

More information

W4118 Operating Systems. Instructor: Junfeng Yang

W4118 Operating Systems. Instructor: Junfeng Yang W4118 Operating Systems Instructor: Junfeng Yang Outline Semaphores Producer-consumer problem Monitors and condition variables 2 Semaphore motivation Problem with lock: mutual exclusion, but no ordering

More information

Synchroniza+on II COMS W4118

Synchroniza+on II COMS W4118 Synchroniza+on II COMS W4118 References: Opera+ng Systems Concepts (9e), Linux Kernel Development, previous W4118s Copyright no2ce: care has been taken to use only those web images deemed by the instructor

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 3: CCR, monitors, and concurrency in practice DrRobert N. M. Watson 1 Reminder from last time Implementing mutual exclusion: hardware support for atomicity and inter-processor

More information

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

Threads need to synchronize their activities to effectively interact. This includes: KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 8 Threads Synchronization ( Mutex & Condition Variables ) Objective: When multiple

More information

CS 318 Principles of Operating Systems

CS 318 Principles of Operating Systems CS 318 Principles of Operating Systems Fall 2017 Lecture 7: Semaphores and Monitors Ryan Huang Higher-Level Synchronization We looked at using locks to provide mutual exclusion Locks work, but they have

More information

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

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift CS 537 Lecture 8 Monitors Michael Swift Two Classes of Synchronization Problems Uniform resource usage with simple scheduling constraints No other variables needed to express relationships Use one semaphore

More information

EECS 482 Introduction to Operating Systems

EECS 482 Introduction to Operating Systems EECS 482 Introduction to Operating Systems Winter 2018 Harsha V. Madhyastha Recap Multi-threaded code with monitors: Locks for mutual exclusion Condition variables for ordering constraints Every thread

More information

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T Operating Systems Operating Systems Summer 2017 Sina Meraji U of T More Special Instructions Swap (or Exchange) instruction Operates on two words atomically Can also be used to solve critical section problem

More information

Concept of a process

Concept of a process Concept of a process In the context of this course a process is a program whose execution is in progress States of a process: running, ready, blocked Submit Ready Running Completion Blocked Concurrent

More information

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

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2019 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

Lecture 8: September 30

Lecture 8: September 30 CMPSCI 377 Operating Systems Fall 2013 Lecture 8: September 30 Lecturer: Prashant Shenoy Scribe: Armand Halbert 8.1 Semaphores A semaphore is a more generalized form of a lock that can be used to regulate

More information

Concurrency: Signaling and Condition Variables

Concurrency: Signaling and Condition Variables Concurrency: Signaling and Condition Variables Questions Answered in this Lecture: How do we make fair locks perform better? How do we notify threads of that some condition has been met? Hale CS450 Thanks

More information

IT 540 Operating Systems ECE519 Advanced Operating Systems

IT 540 Operating Systems ECE519 Advanced Operating Systems IT 540 Operating Systems ECE519 Advanced Operating Systems Prof. Dr. Hasan Hüseyin BALIK (5 th Week) (Advanced) Operating Systems 5. Concurrency: Mutual Exclusion and Synchronization 5. Outline Principles

More information

Semaphore Use In Synchronization

Semaphore Use In Synchronization Lesson 7 Semaphore Use In Synchronization Ch 6 [BenA 06] Consumer Producer Revisited Readers and Writers Baton Passing Private Semaphores Resource Management 1 Synchronization with Semaphores R1 R2 R3

More information

CSE 120 Principles of Operating Systems Spring 2016

CSE 120 Principles of Operating Systems Spring 2016 CSE 120 Principles of Operating Systems Spring 2016 Using Semaphores and Condition Variables Higher-Level Synchronization We looked at using locks to provide mutual exclusion Locks work, but they have

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

More information

Synchronization. Dr. Yingwu Zhu

Synchronization. Dr. Yingwu Zhu Synchronization Dr. Yingwu Zhu Synchronization Threads cooperate in multithreaded programs To share resources, access shared data structures Threads accessing a memory cache in a Web server To coordinate

More information

Paralleland Distributed Programming. Concurrency

Paralleland Distributed Programming. Concurrency Paralleland Distributed Programming Concurrency Concurrency problems race condition synchronization hardware (eg matrix PCs) software (barrier, critical section, atomic operations) mutual exclusion critical

More information

Concurrent Programming Lecture 10

Concurrent Programming Lecture 10 Concurrent Programming Lecture 10 25th September 2003 Monitors & P/V Notion of a process being not runnable : implicit in much of what we have said about P/V and monitors is the notion that a process may

More information

Concurrency: a crash course

Concurrency: a crash course Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Concurrency: a crash course Concurrent computing Applications designed as a collection of computational units that may execute

More information

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Operating Systems. Thread Synchronization Primitives. Thomas Ropars. 1 Operating Systems Thread Synchronization Primitives Thomas Ropars thomas.ropars@univ-grenoble-alpes.fr 2017 2 Agenda Week 42/43: Synchronization primitives Week 44: Vacation Week 45: Synchronization

More information

Project 3-2. Mutex & CV

Project 3-2. Mutex & CV SSE3044 Introduction to Operating Systems Prof. Jinkyu Jeong Project 3-2. Mutex & CV 2018.05.30 (Wed.) TAs 이규선 (gyusun.lee@csl.skku.edu) / 안민우 (minwoo.ahn@csl.skku.edu) Project Plan Total 4 projects 1)

More information

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

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } Semaphore Semaphore S integer variable Two standard operations modify S: wait() and signal() Originally called P() and V() Can only be accessed via two indivisible (atomic) operations wait (S) { while

More information

Multithreading Programming II

Multithreading Programming II Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure

More information

Process Management And Synchronization

Process Management And Synchronization Process Management And Synchronization In a single processor multiprogramming system the processor switches between the various jobs until to finish the execution of all jobs. These jobs will share the

More information

CS 318 Principles of Operating Systems

CS 318 Principles of Operating Systems CS 318 Principles of Operating Systems Fall 2018 Lecture 7: Semaphores and Monitors Ryan Huang Slides adapted from Geoff Voelker s lectures Administrivia HW2 is out Do the exercise to check your understanding

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018 Deadlock and Monitors CS439: Principles of Computer Systems February 7, 2018 Last Time Terminology Safety and liveness Atomic Instructions, Synchronization, Mutual Exclusion, Critical Sections Synchronization

More information

1 Process Coordination

1 Process Coordination COMP 730 (242) Class Notes Section 5: Process Coordination 1 Process Coordination Process coordination consists of synchronization and mutual exclusion, which were discussed earlier. We will now study

More information

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

Announcements. Class feedback for mid-course evaluations Receive  about survey to fill out until this Friday Announcements Project 2: Part 2a will be graded this week Part 2b take longer since we compare all graphs Project 3: Shared memory segments Linux: use shmget and shmat across server + client processes

More information

IV. Process Synchronisation

IV. Process Synchronisation IV. Process Synchronisation Operating Systems Stefan Klinger Database & Information Systems Group University of Konstanz Summer Term 2009 Background Multiprogramming Multiple processes are executed asynchronously.

More information

POSIX Condition Variables ADT

POSIX Condition Variables ADT 28 POSIX Condition Variables ADT One actual Attributes * 1. CV-list = list ids of thread waiting on CV - Domain: empty-list, list w/ 1 tid,... Two logical Attributes * 1. Boolean Condition C - Condition

More information

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

Resource management. Real-Time Systems. Resource management. Resource management Real-Time Systems Specification Implementation Verification Mutual exclusion is a general problem that exists at several levels in a real-time system. Shared resources internal to the the run-time system:

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,

More information

Synchronization Mechanisms

Synchronization Mechanisms Synchronization Mechanisms CSCI 4061 Introduction to Operating Systems Instructor: Abhishek Chandra Mutex Locks Enforce protection and mutual exclusion Condition variables Allow atomic checking of conditions

More information

Monitors & Condition Synchronization

Monitors & Condition Synchronization Feb. 15, 2012 Monitors & condition Synchronization Concepts: monitors: encapsulated data + access procedures mutual exclusion + condition synchronization single access procedure active in the monitor Models:

More information

Solving the Producer Consumer Problem with PThreads

Solving the Producer Consumer Problem with PThreads Solving the Producer Consumer Problem with PThreads Michael Jantz Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads: Producer-Consumer 1 Introduction This lab is an extension of last week's lab.

More information

Synchronization I. Jo, Heeseung

Synchronization I. Jo, Heeseung Synchronization I Jo, Heeseung Today's Topics Synchronization problem Locks 2 Synchronization Threads cooperate in multithreaded programs To share resources, access shared data structures Also, to coordinate

More information

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

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling. Background The Critical-Section Problem Background Race Conditions Solution Criteria to Critical-Section Problem Peterson s (Software) Solution Concurrent access to shared data may result in data inconsistency

More information

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

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34 Semaphores INF4140 13.09.12 Lecture 3 0 Book: Andrews - ch.04 (4.1-4.4) INF4140 (13.09.12 ) Semaphores Lecture 3 1 / 34 Overview Last lecture: Locks and Barriers (complex techniques) No clear difference

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 2: More mutual exclusion, semaphores, and producer-consumer relationships DrRobert N. M. Watson 1 Reminder from last time Definition of a concurrent system Origins of concurrency

More information

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo CSE 120 Principles of Operating Systems Fall 2007 Lecture 6: Semaphores Keith Marzullo Announcements Homework #2 out Homework #1 almost graded... Discussion session on Wednesday will entertain questions

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

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

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017 CS 471 Operating Systems Yue Cheng George Mason University Fall 2017 1 Review: Sync Terminology Worksheet 2 Review: Semaphores 3 Semaphores o Motivation: Avoid busy waiting by blocking a process execution

More information

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Semaphores Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) Synchronization

More information

CS11 Java. Fall Lecture 7

CS11 Java. Fall Lecture 7 CS11 Java Fall 2006-2007 Lecture 7 Today s Topics All about Java Threads Some Lab 7 tips Java Threading Recap A program can use multiple threads to do several things at once A thread can have local (non-shared)

More information

What's wrong with Semaphores?

What's wrong with Semaphores? Next: Monitors and Condition Variables What is wrong with semaphores? Monitors What are they? How do we implement monitors? Two types of monitors: Mesa and Hoare Compare semaphore and monitors Lecture

More information

CS3502 OPERATING SYSTEMS

CS3502 OPERATING SYSTEMS CS3502 OPERATING SYSTEMS Spring 2018 Synchronization Chapter 6 Synchronization The coordination of the activities of the processes Processes interfere with each other Processes compete for resources Processes

More information

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

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

More information

Models of concurrency & synchronization algorithms

Models of concurrency & synchronization algorithms Models of concurrency & synchronization algorithms Lecture 3 of TDA383/DIT390 (Concurrent Programming) Carlo A. Furia Chalmers University of Technology University of Gothenburg SP3 2016/2017 Today s menu

More information

Operating Systems 2006/2007

Operating Systems 2006/2007 Operating Systems 2006/2007 Blocking and locking Johan Lukkien 1 Blocking & locking Blocking: waiting for a certain condition to become true Starvation: unpredictable, even infinite blocking times the

More information

Lecture 7: CVs & Scheduling

Lecture 7: CVs & Scheduling Lecture 7: CVs & Scheduling CSE 120: Principles of Operating Systems Alex C. Snoeren HW 2 Due 10/17 Monitors A monitor is a programming language construct that controls access to shared data Synchronization

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole Monitors 2 Programming Complexity There are many ways to introduce bugs using locks and semaphores - forget to lock somewhere in the program - forget

More information

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

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References COSC 6374 Parallel Computation Shared memory programming with POSIX Threads Fall 2012 References Some of the slides in this lecture is based on the following references: http://www.cobweb.ecn.purdue.edu/~eigenman/ece563/h

More information

Condition Variables. Dongkun Shin, SKKU

Condition Variables. Dongkun Shin, SKKU Condition Variables 1 Why Condition? cases where a thread wishes to check whether a condition is true before continuing its execution 1 void *child(void *arg) { 2 printf("child\n"); 3 // XXX how to indicate

More information

CS533 Concepts of Operating Systems. Jonathan Walpole

CS533 Concepts of Operating Systems. Jonathan Walpole CS533 Concepts of Operating Systems Jonathan Walpole Introduction to Threads and Concurrency Why is Concurrency Important? Why study threads and concurrent programming in an OS class? What is a thread?

More information

Real Time Operating Systems and Middleware

Real Time Operating Systems and Middleware Real Time Operating Systems and Middleware POSIX Threads Synchronization Luca Abeni abeni@dit.unitn.it Real Time Operating Systems and Middleware p. 1 Threads Synchronisation All the threads running in

More information

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

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio Fall 2017 1 Outline Inter-Process Communication (20) Threads

More information

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

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage Semaphores CSE 451: Operating Systems Spring 2013 Module 8 Semaphores, Condition Variables, and Monitors Ed Lazowska lazowska@cs.washington.edu Allen Center 570 Semaphore = a synchronization primitive

More information

CS3733: Operating Systems

CS3733: Operating Systems Outline CS3733: Operating Systems Topics: Synchronization, Critical Sections and Semaphores (SGG Chapter 6) Instructor: Dr. Tongping Liu 1 Memory Model of Multithreaded Programs Synchronization for coordinated

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 6: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded Systems II Based heavily on slides by Dr. Roger Walker More Task Decomposition: Dependence

More information

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

CSE Traditional Operating Systems deal with typical system software designed to be: CSE 6431 Traditional Operating Systems deal with typical system software designed to be: general purpose running on single processor machines Advanced Operating Systems are designed for either a special

More information

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430 Implementing Mutual Exclusion Sarah Diesburg Operating Systems CS 3430 From the Previous Lecture The too much milk example shows that writing concurrent programs directly with load and store instructions

More information

ANSI/IEEE POSIX Standard Thread management

ANSI/IEEE POSIX Standard Thread management Pthread Prof. Jinkyu Jeong( jinkyu@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) TA Seokha Shin(seokha.shin@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Objectives Introduce Concept of Critical-Section Problem Hardware and Software Solutions of Critical-Section Problem Concept of Atomic Transaction Operating Systems CS

More information

February 23 rd, 2015 Prof. John Kubiatowicz

February 23 rd, 2015 Prof. John Kubiatowicz CS162 Operating Systems and Systems Programming Lecture 9 Synchronization Continued, Readers/Writers example, Scheduling February 23 rd, 2015 Prof. John Kubiatowicz http://cs162.eecs.berkeley.edu Acknowledgments:

More information

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

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify http://cs.lth.se/eda040 Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify Klas Nilsson 2016-09-20 http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 10: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded d Systems II Objectives: Lab 3: Windows Threads (win32 threading API) Convert serial

More information

Locks and semaphores. Johan Montelius KTH

Locks and semaphores. Johan Montelius KTH Locks and semaphores Johan Montelius KTH 2018 1 / 40 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;

More information

EEE3052: Introduction to Operating Systems. Fall Project #3

EEE3052: Introduction to Operating Systems. Fall Project #3 EEE3052: Introduction to Operating Systems Fall 2017 Project #3 Project Plan 4 projects 0) Install Xv6 1) Process management 2) Virtual memory 3) Synchronization - Thread (11/13 ~ 11/21) - Mutex & Condition

More information

CS 153 Design of Operating Systems Winter 2016

CS 153 Design of Operating Systems Winter 2016 CS 153 Design of Operating Systems Winter 2016 Lecture 9: Semaphores and Monitors Some slides from Matt Welsh Summarize Where We Are Goal: Use mutual exclusion to protect critical sections of code that

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

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

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives. CS 5523 Operating Systems: Concurrency and Synchronization Thank Dr. Dakai Zhu and Dr. Palden Lama for providing their slides. Lecture Outline Problems with concurrent access to shared data Ø Race condition

More information

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

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization Review: Implementation of Locks by Disabling Interrupts CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization Friday 11, 2010 Ion Stoica http://insteecsberkeleyedu/~cs162

More information

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

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala Synchronization II: EventBarrier, Monitor, and a Semaphore COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala Check point: Mission in progress Master synchronization techniques Develop best practices for

More information

Today: Synchronization. Recap: Synchronization

Today: Synchronization. Recap: Synchronization Today: Synchronization Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical section

More information

Synchronising Threads

Synchronising Threads Synchronising Threads David Chisnall March 1, 2011 First Rule for Maintainable Concurrent Code No data may be both mutable and aliased Harder Problems Data is shared and mutable Access to it must be protected

More information

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

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors CSE 451: Operating Systems Spring 2017 Module 10 Semaphores, Condition Variables, and Monitors John Zahorjan 2017 Gribble, Lazowska, Levy, Zahorjan, Zbikowski 1 Semaphores Semaphore = a synchronization

More information

Sharing Objects Ch. 3

Sharing Objects Ch. 3 Sharing Objects Ch. 3 Visibility What is the source of the issue? Volatile Dekker s algorithm Publication and Escape Thread Confinement Immutability Techniques of safe publication Assignment 1 Visibility

More information

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

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy Operating Systems Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : eaymanelshenawy@yahoo.com Reference

More information

POSIX Threads. HUJI Spring 2011

POSIX Threads. HUJI Spring 2011 POSIX Threads HUJI Spring 2011 Why Threads The primary motivation for using threads is to realize potential program performance gains and structuring. Overlapping CPU work with I/O. Priority/real-time

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

Condition Variables. Dongkun Shin, SKKU

Condition Variables. Dongkun Shin, SKKU Condition Variables 1 Why Condition? cases where a thread wishes to check whether a condition is true before continuing its execution 1 void *child(void *arg) { 2 printf("child\n"); 3 // XXX how to indicate

More information