Synchronization: Semaphores

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Synchronization: Semaphores"

Transcription

1 Illinois Institute of Technology Lecture 26 4/25 solved Synchronization: Semaphores CS 536: Science of Programming, Spring 2018 A. Why Avoiding interference, while good, isn t the same as coordinating desirable activities. It s common for one thread to wait for another thread to reach a desired state. B. Objectives At the end of this lecture you should know The semantics of binary semaphores and their P and V operations. How to implement binary semaphores using await statements. How to solve the mutual exclusion problem using binary semaphores. C. Binary Semaphores and the Mutual Exclusion Problem One use for await statements is for solving the mutual exclusion ( mutex ) problem where we want to ensure that only one process at a time is in some critical section of code (e.g., writing to a shared database). Say thread S₁ contains a section of code CS₁ ( Critical Section₁ ) and thread S₂ contains CS₂, so our program includes [ CS₁ CS₂ ]. In the mutual exclusion problem, we want to ban interleaving of critical section code: Only one of the CS i should be executing at any given time. Making the CS i atomic solves the problem automatically, but if the execution of CS i is going to take time (e.g., because it involves I/O), we might want to interleave critical code with noncritical code. E.g., if thread 1 is in CS₁ because it's writing to the database, it's fine for thread 2 to be doing some calculations in its noncritical code. We don't want both threads to be writing to the database simultaneously, so thread 1 being in CS₁ should prevent thread 2 from being in CS₂. The traditional solution to mutex problem uses binary semaphores. A binary semaphore is a boolean flag (initially T) with blocking properties: Semaphore = true indicates permission to continue. Semaphore = false indicates lack of permission. Operations on a binary semaphore s: Init(s, b): Initialize s to b (T or F). P(s): Get permission to continue (and deny it to everyone else). If s is true, we set it to false and continue. (The test and set are done atomically.) If s is false, we wait until it becomes true; then we set it to false and continue. V(s) means Give up permission (so someone else can get permission). If s is false, we set it to true, select one of the P(s) calls that are waiting and let that call continue. If s is true, leave it true. CS : Science of Programming 1 James Sasaki, 2018

2 Different implementations of semaphores can differ on how to select one of the waiting P(s) calls. The original scheme selected nondeterministically. For fairness, we might actually keep a priority queue of waiting calls. Semaphores were invented by Edsgar Dijkstra The names P and V come from Dutch V stands for verhoog = increase. P stands for ( prolaag, which is short for probeer te verlagen, or try-and-decrease. In practice, people often use synonyms for P and V: Synonyms for P: wait, acquire, lower. Synonyms for V: signal, release, raise. Using semaphores, the mutual exclusion problem is solved as follows: Init(s, T); // s true iff it's ok to enter the critical section [ ; P(s); CS₁; V(s); // thread 1 and its critical section ; P(s); CS₂; V(s); ] // thread 2 and its critical section We can encode semaphores using await statements: Init(s, b) s := b P(s) await s then s := F end Atomicity of await ensures no one can change s between the time that we notice s is T and then set s to F. V(s) s := T For the mutual exclusion problem, Let the semaphore be mu ( mutex ) Let NC₁ and NC₂ be noncritical section code. We'll model repeated attempts to enter critical section code by putting the critical code in a loop. The auxiliary variables u₁ and u₂ ( using critical section ) are true when threads 1 and 2 respectively are in their critical sections. Init(mu,T); u₁ := F; u₂ := F; [ while B₁ do NC₁; P(mu); u₁ := T; CS₁; u₁ := F; V(mu) od while B₂ do NC₂; P(mu); u₂ := T; CS₂; u₂ := F; V(mu) od ] If we translate P and V using await, we get the following program S i for thread i (where i = 1, 2). Note we ve pulled the set of u i := T into the await body to make it atomic with the reset of mu. while B i do NC i ; od await mu then mu := F; u i := T end; CS i ; // Get permission < mu := T; u i := F > // Release permission CS : Science of Programming 2 James Sasaki, 2018

3 D. Sequential Correctness of the Mutex Program Below is a full sequential annotation of S i. The loop invariant includes a global invariant p₀ (u ₀ u ₁) (mu u ₀ u ₁)) that is maintained across all threads. In addition, there s a local part to the loop invariant, u i, which means "we're not in a critical section". Since we're running the loop forever, the thread postcondition could have been false, but making it the global invariant seems more reasonable. Below, let j be the index used in S i to name the other thread. (Sets {i, j} and {1, 2} are equal) {inv u i p₀} // where p₀ (u i u j ) (mu u i u j ) while B i do {u i p₀} NC i {u i p₀}; await mu then mu := F; u i := T end; {m u u i p₀} CS i ; {m u u i p₀} {u j } { F ( F u j ) (T F u j )} < mu := T; u i := F > {u i (u i u j ) (mu u i u j )} {u i p₀} // add mu to invariant? [4/25] od {u i p₀ B i } // We're in our CS // from u i p₀ in line above // wp of atomic assignments // expanding p₀ Here's a standard annotation of S i : {u i p₀} // where p₀ (u i u j ) (mu u i u j ) while B i do {u i p₀ B i } NC i ; od {u i p₀ B i } {u i p₀} await mu then mu := F; u i := T end; {m u u i p₀} CS i ; {m u u i p₀} < mu := T; u i := F > // P(mu) // V(mu) E. Interference Freedom of the Mutex Program Let's verify that thread S i doesn't interfere with the other thread, S j. (By symmetry, we'll find that S j doesn't interfere with S i ). The predicates we don't want to interfere with are: u j p₀ where p₀ (u i u j ) (mu u i u j ) m u u j p₀ (Let's assume that we don't interfere with the loop test B j so that the variants of u j p₀ that add B j and B j aren't relevant.) The code in S i that we have to check is {u i p₀ B i } NC i { } CS : Science of Programming 3 James Sasaki, 2018

4 {u i p₀} await mu then mu := F; u i := T end { } {m u u i p₀} CS i { } {m u u i p₀} < mu := T; u i := F > { } Only the P(mu) and V(mu) code actually need to be checked because NC i and CS i don't modify u j or mu, so they both maintain (u j p₀) and (m u u j p₀). So altogether we have four interference tests 1. {u i p₀} P(mu) { } with u j p₀ 2. {u i p₀} P(mu) { } with m u u j p₀ 3. {m u u i p₀} V(mu) { } with u j p₀ 4. {m u u i p₀} V(mu) { } with m u u j p₀ Test 1: {u i p₀} P(mu) doesn't interfere with u j p₀. Here's an annotation that shows interference freedom: {(u i p₀) (u j p₀)} // where p₀ (u i u j ) (mu u i u j ) await mu then end {(u i p₀) (u j p₀) mu} {u j (u j T) (F u j T)} mu := F; u i := T {u j (u j u i ) (mu u j u i )} {u j p₀} // precondition of await body // wp of postcondition // p₀ expanded Test 2: {u i p₀} P(mu) doesn't interfere with m u u j p₀. Note: {m u } await mu makes the contradiction m u mu the precondition of the await body; this lets us use anything for the postcondition of the await body. {(u i p₀) (m u u j p₀)} // where p₀ (u i u j ) (mu u i u j ) await mu then!!!!!!! // P(mu) in S i end; {m u u j p₀} {m u mu} {F} mu := F; u i := T {F}{m u u j p₀} Test 3: {m u u i p₀} V(mu) doesn't interfere with u j p₀. {(m u u i p₀) (u j p₀)} // where p₀ (u i u j ) (mu u i u j ) {u j (u j F) (T u j F)} < mu := T; u i := F > // V(mu) in S i {u j (u j u i ) (mu u j u i )} {u j p₀} // wp of the atomic assignments // p₀ expanded CS : Science of Programming 4 James Sasaki, 2018

5 Test 4: {m u u i p₀} V(mu) doesn't interfere with m u u j p₀. Again, we find a contradiction: We assume u i u j hold when p₀ implies u j u i. {(m u u i p₀) (m u u j p₀)} // where p₀ (u i u j ) (mu u i u j ) {(m u u i u j ((u j u i )...)} {F} V(mu) {F} {m u u j p₀} // Expanding p₀ and rearranging // Contradiction // false implies anything F. Deadlock Freedom of the Mutex Program For deadlock freedom, we need to look at the potential deadlock conditions. For thread i, we have an await mu statement with precondition (u i p₀) and the thread s postcondition (u i p₀ B i ). (Recall p₀ (u ₀ u ₁) (mu u ₀ u ₁).) Let D₁ = {(u ₁ p₀ m u ), (u ₁ p₀ B i )} (await precondition test), (thread postcondition) Let D₂ = {(u ₂ p₀ m u ), (u ₂ p₀ B i )} The set D of deadlock conditions is { (u ₁ p₀ m u ) (u ₂ p₀ m u ) // Both threads blocked (u ₁ p₀ m u ) (u ₂ p₀ B j ) (u ₁ p₀ B i )) (u ₂ p₀ m u ) } // # 1 blocked, # 2 done // # 1 done, # 2 blocked The program is deadlock-free because all three of the deadlock conditions are contradictions: They each include u ₁ and u ₂ (so we must have mu), but we also have m u. So we can apply the rule for Parallelism with Deadlock Freedom and get our mutex program {T} mu := T; {mu} u₁ := F; u₂ := F; {mu u ₁ u ₂ p₀} {(u ₁ p₀) (u ₂ p₀)} [S₁ S₂] { } G. Counting Semaphores Boolean semaphores are good for situations with two states; for situations with many states, it's useful to have counting semaphores, which have natural number values. Init(s, n) initializes the semaphore to n (for natural number n 0). P(s) await s > 0 then s := s-1 end The P operation waits if decrementing the semaphore would make it negative. V(s) s := s+1 A boolean semaphore is equivalent to a counting semaphore where n 1. Recall the Producer/Consumer Problem: The producer adds items to a finite buffer (but needs to wait if it can't add anything to the buffer); the consumer removes items from the buffer (but needs to wait if the buffer contains nothing to consume). We can solve the Producer/Consumer Problem with two counting semaphore(s): CS : Science of Programming 5 James Sasaki, 2018

6 The nbr_unused semaphore will track the number of empty slots in the buffer (initially, the buffer capacity). The producer does a P on this semaphore before it adds an item to the buffer; the consumer does a V on it when it removes an item from the buffer. The nbr_used semaphore will track the number of used slots in the buffer (initially, zero). The consumer does a P on this semaphore before it removes an item from the buffer; the producer does a V on this semaphore when it stores an item into the buffer. The top-level code is: InitializeBuffer(b, N); Init(nbr_unused, N); Init(nbr_used, 0); [Producer Consumer] The producer is while done do # thing_p := Create(); # P(nbr_unused); BufferAdd(b, thing_p); # V(nbr_used); od The consumer is: while done do # P(nbr_used); # thing_c := BufferRemove(b); V(nbr_unused); # Consume(thing_c) od There's a critical section problem that comes up with the buffer operations: We almost certainly don't want buffer add and remove operations interleaving because that might leave the buffer in an inconsistent state, so the buffer operations are critical sections. If we have multiple producer or consumer threads running, then making the buffer operations critical sections also keeps (e.g.) two buffer adds interleaving. CS : Science of Programming 6 James Sasaki, 2018

7 Synchronization: Semaphores CS 536: Science of Programming A. Why It s common for one thread to wait for another thread to reach a desired state. B. Objectives At the end of this activity assignment you should be able to State the semantics of binary semaphores and their P and V operations. Implement binary semaphores using await statements. Solve the mutual exclusion problem using binary semaphores. C. Problems For the upcoming exam, it s sufficient to know the answers to the following questions. 1. For binary semaphores a. What is a (binary) semaphore? b. How can we implement P(s)? What does P(s) do? c. How can we implement V(s)? What does V(s) do? 2. Repeat the previous problem, but for counting semaphores. 3. How can we simulate a boolean semaphore using a counting semaphore? 4. How do we decide what to initialize a counting semaphore to? A binary semaphore? 5. How do we solve the critical section problem using a binary semaphore? When does it cause waiting? How is the semaphore initialized? 6. In the solution to the producer-consumer problem, what did our two counting semaphores count? How do we initialize them and use them? When do the producer and consumer wait, and for what? When are a waiting producer or consumer woken, and why? CS : Science of Programming 7 James Sasaki, 2018

8 Illinois Institute of Technology Lecture 26 Activity 26 Solution: Synchronization: Semaphores 1. (Binary semaphore) a. A binary semaphore is a permission flag. If the flag is true, we have permission to do some action associated with the semaphore. b. P(s) await s then s := F end; The P(s) operation waits until s is true, then sets s to false. c. V(s) s := T. The V(s) operation sets semaphore s to true. If one or more threads are waiting for their P(s) operation to complete, then one of them gets to continue. 2. (Counting semaphore) a. A counting semaphore s holds an integer 0; the value is taken as a number of available resources. b. P(s) await s > 0 then s := s-1 end. The P(s) operation waits until s > 0 then it decrements s. I.e., it models waiting until there exists at least one resource and then taking it. c. V(s): s := s +1. It models returning a resource and making it available. If this sets s to 1, then one of the waiting P(s) operation gets to continue. 3. We can simulate a boolean semaphore as a counting semaphore that has a maximum value of Viewing a counting semaphore as modeling the number of available resources, we should initialize it to the number of available resources at the point of initialization. A binary semaphore can be viewed as modeling having 0 or 1 permissions to do something, so we initialize it to F or T depending on whether permission exists initially or not. 5. To solve the critical section problem using a binary semaphore s, we surround each critical section with P(s) and V(s and initialize s to T. This ensures that only one thread ever gets permissions to enter its critical section. 6. To solve the producer-consumer problem One semaphore holds number of used buffer slots; the other holds the number of empty buffer slots. They get initialized to 0 and size of buffer respectively. The consumer does a P on the number of used buffer slots, so it waits if the buffer is empty. The producer does a P on the number of empty buffer slots, so it waits if the buffer is full. When the consumer removes something from the buffer, it does a V on the number of empty buffer slots (which may then awaken a waiting producer). When the producer adds something to the buffer, it does a V on the number of used buffer slots (which may awaken a waiting consumer). CS 536: Science of Programming 8 James Sasaki, 2018

Shared Variables and Interference

Shared Variables and Interference Illinois Institute of Technology Lecture 24 Shared Variables and Interference CS 536: Science of Programming, Spring 2018 A. Why Parallel programs can coordinate their work using shared variables, but

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

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

Last Class: Synchronization

Last Class: Synchronization Last Class: Synchronization Synchronization primitives are required to ensure that only one thread executes in a critical section at a time. Concurrent programs Low-level atomic operations (hardware) load/store

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

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

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

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9 CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9 Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce

More information

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 11: Semaphores I" Brian Logan School of Computer Science bsl@cs.nott.ac.uk Outline of this lecture" problems with Peterson s algorithm semaphores implementing semaphores

More information

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers Mutual exclusion & synchronization mutexes Unbounded buffer, 1 producer, N consumers out shared by all consumers mutex among consumers producer not concerned: can still add items to buffer at any time

More information

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

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO Semaphores A shared integer variable, s, initialized to init, and manipulated only by two operations: pass (proberen): P(s) df = await(s > 0)s = s 1 1 Fairness: V may release a waiting process Weakly fair,

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

SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores

SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores SWEN-220 Mathematical Models of Software Process Synchronization Critical Section & Semaphores 1 Topics The critical section Synchronization using busy-wait Semaphores 2 The Critical Section Processes

More information

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Process Synchronisation (contd.) Operating Systems. Autumn CS4023 Operating Systems Autumn 2017-2018 Outline Process Synchronisation (contd.) 1 Process Synchronisation (contd.) Synchronization Hardware 6.4 (SGG) Many systems provide hardware support for critical section

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

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

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 6: Algorithms for Mutual Natasha Alechina School of Computer Science nza@cs.nott.ac.uk Outline of this lecture mutual exclusion with standard instructions example:

More information

CS420: Operating Systems. Process Synchronization

CS420: Operating Systems. Process Synchronization Process Synchronization James Moscola Department of Engineering & Computer Science York College of Pennsylvania Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne Background

More information

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

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles Operating Systems: Internals and Design Principles Chapter 5 Concurrency: Mutual Exclusion and Synchronization Seventh Edition By William Stallings Designing correct routines for controlling concurrent

More information

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28) Lecture Topics Today: Concurrency (Stallings, chapter 5.1-5.4, 5.7) Next: Exam #1 1 Announcements Self-Study Exercise #5 Project #3 (due 9/28) Project #4 (due 10/12) 2 Exam #1 Tuesday, 10/3 during lecture

More information

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10 MULTITHREADING AND SYNCHRONIZATION CS124 Operating Systems Fall 2017-2018, Lecture 10 2 Critical Sections Race conditions can be avoided by preventing multiple control paths from accessing shared state

More information

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 Concurrent programming Assumptions:

More information

OS Process Synchronization!

OS Process Synchronization! OS Process Synchronization! Race Conditions! The Critical Section Problem! Synchronization Hardware! Semaphores! Classical Problems of Synchronization! Synchronization HW Assignment! 3.1! Concurrent Access

More information

Achieving Synchronization or How to Build a Semaphore

Achieving Synchronization or How to Build a Semaphore Achieving Synchronization or How to Build a Semaphore CS 241 March 12, 2012 Copyright University of Illinois CS 241 Staff 1 Announcements MP5 due tomorrow Jelly beans... Today Building a Semaphore If time:

More information

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for? Lightweight programming construct for concurrent activities How to implement? Kernel thread vs.

More information

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution Race Condition Synchronization CSCI 315 Operating Systems Design Department of Computer Science A race occurs when the correctness of a program depends on one thread reaching point x in its control flow

More information

Concurrency. Chapter 5

Concurrency. Chapter 5 Concurrency 1 Chapter 5 2 Concurrency Is a fundamental concept in operating system design Processes execute interleaved in time on a single processor Creates the illusion of simultaneous execution Benefits

More information

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7 Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce

More information

Process/Thread Synchronization

Process/Thread Synchronization CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov March 1, 2011 The image cannot be displayed. Your computer may not have enough memory to open the image,

More information

Concurrency: Mutual Exclusion and

Concurrency: Mutual Exclusion and Concurrency: Mutual Exclusion and Synchronization 1 Needs of Processes Allocation of processor time Allocation and sharing resources Communication among processes Synchronization of multiple processes

More information

Chapter 5 Asynchronous Concurrent Execution

Chapter 5 Asynchronous Concurrent Execution Chapter 5 Asynchronous Concurrent Execution Outline 5.1 Introduction 5.2 Mutual Exclusion 5.2.1 Java Multithreading Case Study 5.2.2 Critical Sections 5.2.3 Mutual Exclusion Primitives 5.3 Implementing

More information

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

Semaphores and Monitors: High-level Synchronization Constructs

Semaphores and Monitors: High-level Synchronization Constructs 1 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Semaphores and Monitors High-level Synchronization Constructs A Historical Perspective

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

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages The Dining Philosophers Problem CMSC 0: Organization of Programming Languages Threads Classic Concurrency Problems Philosophers either eat or think They must have two forks to eat Can only use forks on

More information

Process Synchronization

Process Synchronization Process Synchronization Concurrent access to shared data in the data section of a multi-thread process, in the shared memory of multiple processes, or in a shared file Although every example in this chapter

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

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

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Chapter 6: Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Synchronization, Silberschatz, Galvin and Gagne 2009 Outline Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Concurrency Issues. Past lectures: What about coordinated access across multiple objects? Today s lecture:

Concurrency Issues. Past lectures: What about coordinated access across multiple objects? Today s lecture: Deadlock 1 Concurrency Issues Past lectures: Ø Problem: Safely coordinate access to shared resource Ø Solutions: Use semaphores, monitors, locks, condition variables Coordinate access within shared objects

More information

UNIX Input/Output Buffering

UNIX Input/Output Buffering UNIX Input/Output Buffering When a C/C++ program begins execution, the operating system environment is responsible for opening three files and providing file pointers to them: stdout standard output stderr

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

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 12 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ 2 Mutex vs Semaphore Mutex is binary,

More information

Interprocess Communication By: Kaushik Vaghani

Interprocess Communication By: Kaushik Vaghani Interprocess Communication By: Kaushik Vaghani Background Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of execution depends on the

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

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0 How invariants help writing loops Author: Sander Kooijmans Document version: 1.0 Why this document? Did you ever feel frustrated because of a nasty bug in your code? Did you spend hours looking at the

More information

Lecture 5 Threads and Pthreads II

Lecture 5 Threads and Pthreads II CSCI-GA.3033-017 Special Topics: Multicore Programming Lecture 5 Threads and Pthreads II Christopher Mitchell, Ph.D. cmitchell@cs.nyu.edu http://z80.me Context We re exploring the layers of an application

More information

Last Class: Clock Synchronization. Today: More Canonical Problems

Last Class: Clock Synchronization. Today: More Canonical Problems Last Class: Clock Synchronization Logical clocks Vector clocks Global state Lecture 12, page 1 Today: More Canonical Problems Distributed snapshot and termination detection Election algorithms Bully algorithm

More information

Concurrent Programming. CS105 Programming Languages Supplement

Concurrent Programming. CS105 Programming Languages Supplement Concurrent Programming CS105 Programming Languages Supplement Outline Introduction Categories Concepts Semaphores Monitors Message Passing Statement level concurrency Introduction Definitions Process:

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

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches Synchronization CSCI 5103 Operating Systems Monitors Other Approaches Instructor: Abhishek Chandra 2 3 Semaphore Usage wait(s) Critical section signal(s) Each process must call wait and signal in correct

More information

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

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002 CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002 Lecture 6: Synchronization 6.0 Main points More concurrency examples Synchronization primitives 6.1 A Larger Concurrent

More information

Lecture 10: Introduction to Semaphores

Lecture 10: Introduction to Semaphores COMP 150-CCP Concurrent Programming Lecture 10: Introduction to Semaphores Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 19, 2008 Semaphores Semaphores (Dijkstra 1968) are widely

More information

CS193k, Stanford Handout #8. Threads 3

CS193k, Stanford Handout #8. Threads 3 CS193k, Stanford Handout #8 Spring, 2000-01 Nick Parlante Threads 3 t.join() Wait for finish We block until the receiver thread exits its run(). Use this to wait for another thread to finish. The current

More information

Chapter 13 Topics. Introduction. Introduction

Chapter 13 Topics. Introduction. Introduction Chapter 13 Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Java Threads C# Threads Statement-Level Concurrency Copyright 2006 Pearson Addison-Wesley. All rights reserved.

More information

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

COMP 300E Operating Systems Fall Semester 2011 Midterm Examination SAMPLE. Name: Student ID: COMP 300E Operating Systems Fall Semester 2011 Midterm Examination SAMPLE Time/Date: 5:30 6:30 pm Oct 19, 2011 (Wed) Name: Student ID: 1. Short Q&A 1.1 Explain the convoy effect with FCFS scheduling algorithm.

More information

Condition Variables & Semaphores

Condition Variables & Semaphores Condition Variables & Semaphores Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau) Review: Concurrency Objectives Mutual Exclusion A & B don t run at the same time Solved using locks Ordering

More information

Chapter 7: Process Synchronization. Background. Illustration

Chapter 7: Process Synchronization. Background. Illustration Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris

More information

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks Synchronization II Today Condition Variables Semaphores Monitors and some classical problems Next time Deadlocks Condition variables Many times a thread wants to check whether a condition is true before

More information

Timo Latvala. January 28, 2004

Timo Latvala. January 28, 2004 Reactive Systems: Kripke Structures and Automata Timo Latvala January 28, 2004 Reactive Systems: Kripke Structures and Automata 3-1 Properties of systems invariants: the system never reaches a bad state

More information

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

CS162 Operating Systems and Systems Programming Lecture 7. Mutual Exclusion, Semaphores, Monitors, and Condition Variables CS162 Operating Systems and Systems Programming Lecture 7 Mutual Exclusion, Semaphores, Monitors, and Condition Variables September 22, 2010 Prof John Kubiatowicz http://insteecsberkeleyedu/~cs162 Review:

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

Synchronization Spinlocks - Semaphores

Synchronization Spinlocks - Semaphores CS 4410 Operating Systems Synchronization Spinlocks - Semaphores Summer 2013 Cornell University 1 Today How can I synchronize the execution of multiple threads of the same process? Example Race condition

More information

Threads and concurrency

Threads and concurrency Threads and concurrency Motivation: operating systems getting really complex Multiple users, programs, I/O devices, etc. How to manage this complexity? Main techniques to manage complexity in programs?

More information

7: Interprocess Communication

7: Interprocess Communication 7: Interprocess Communication Mark Handley Interprocess Communication Processes frequently need to communicate to perform tasks. Shared memory. Shared files. Message passing. Whenever processes communicate,

More information

Concurrency: Deadlock and Starvation. Chapter 6

Concurrency: Deadlock and Starvation. Chapter 6 Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs for resources

More information

Synchronization COMPSCI 386

Synchronization COMPSCI 386 Synchronization COMPSCI 386 Obvious? // push an item onto the stack while (top == SIZE) ; stack[top++] = item; // pop an item off the stack while (top == 0) ; item = stack[top--]; PRODUCER CONSUMER Suppose

More information

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011 Administrative Thursday s class Meet in WEB L130 to go over programming assignment Midterm on Thursday

More information

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

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M) re-exam Concurrent Programming tda383/dit390 Date: 2016-08-22 Time: 14:00 18:00 Place: Maskinhuset (M) Responsible Michał Pałka 031 772 10 79 Result Available no later than 2016-09-12 Aids Max 2 books

More information

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved. Processes Prof. James L. Frankel Harvard University Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved. Process Model Each process consists of a sequential program

More information

September 23 rd, 2015 Prof. John Kubiatowicz

September 23 rd, 2015 Prof. John Kubiatowicz CS162 Operating Systems and Systems Programming Lecture 8 Locks, Semaphores, Monitors, and Quick Intro to Scheduling September 23 rd, 2015 Prof. John Kubiatowicz http://cs162.eecs.berkeley.edu Acknowledgments:

More information

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

Operating Systems (1DT020 & 1TT802) Lecture 6 Process synchronisation : Hardware support, Semaphores, Monitors, and Condition Variables Operating Systems (1DT020 & 1TT802) Lecture 6 Process synchronisation : Hardware support, Semaphores, Monitors, and Condition Variables April 22, 2008 Léon Mugwaneza http://www.it.uu.se/edu/course/homepage/os/vt08

More information

EECS 482 Introduction to Operating Systems

EECS 482 Introduction to Operating Systems EECS 482 Introduction to Operating Systems Fall 2017 Manos Kapritsos Slides by: Harsha V. Madhyastha Implementing reader-writer locks with monitors Shared data needed to implement readerstart, readerfinish,

More information

Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo}

Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo} Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo} The Wa (Wawa) at Princeton See the too much milk problem last week Wawa http://www.wawa.com/ http://www.absoluteastronomy.com/encyclopedia/w/wa/wawa_food_markets.htm

More information

Process Co-ordination OPERATING SYSTEMS

Process Co-ordination OPERATING SYSTEMS OPERATING SYSTEMS Prescribed Text Book Operating System Principles, Seventh Edition By Abraham Silberschatz, Peter Baer Galvin and Greg Gagne 1 PROCESS - CONCEPT Processes executing concurrently in the

More information

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ! Last Class: CPU Scheduling! Scheduling Algorithms: FCFS Round Robin SJF Multilevel Feedback Queues Lottery Scheduling Review questions: How does each work? Advantages? Disadvantages? Lecture 7, page 1

More information

Lecture 3: Synchronization & Deadlocks

Lecture 3: Synchronization & Deadlocks Lecture 3: Synchronization & Deadlocks Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating

More information

CMSC 433 Spring 2013 Exam 1

CMSC 433 Spring 2013 Exam 1 CMSC 433 Spring 2013 Exam 1 Name: _ EXAMPLE SOLUTIONS Directions: Test is closed book, closed notes, closed electronics. Answer every question; write your answers in the spaces provided. If you need extra

More information

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

Need for synchronization: If threads comprise parts of our software systems, then they must communicate. Thread communication and synchronization There are two main aspects to Outline for Lecture 19 multithreaded programming in Java: I. Thread synchronization. thread lifecycle, and thread synchronization.

More information

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors

Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors 4 Concurrency Concurrency Problems Signals & Synchronization Semaphore Mutual Exclusion Critical Section Monitors 2009 Universität Karlsruhe (TU), System Architecture Group 1 Roadmap for Today Concurrency

More information

Assignment-2 (TCS/TIT 503)

Assignment-2 (TCS/TIT 503) Assignment-2 (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores Submission dates: 1)CS A:Monday,26/10/2015 (Time: between 12 pm to 1 pm) 2)CS B:Monday,26/10/2015(in the lecture

More information

Lecture 3: Semaphores (chap. 6) K. V. S. Prasad Dept of Computer Science Chalmer University 6 Sep 2013

Lecture 3: Semaphores (chap. 6) K. V. S. Prasad Dept of Computer Science Chalmer University 6 Sep 2013 Lecture 3: Semaphores (chap. 6) K. V. S. Prasad Dept of Computer Science Chalmer University 6 Sep 2013 QuesFons? Anything you did not get Was I too fast/slow? Have you joined the google group? Found a

More information

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning Synchronization Peter J. Denning CS471/CS571 Copyright 2001, by Peter Denning What is synchronization? Requirement that one process stop to wait to pass a point until another process sends a signal. The

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

Concurrent & Distributed 2Systems Mutual Exclusion. Uwe R. Zimmer - The Australian National University

Concurrent & Distributed 2Systems Mutual Exclusion. Uwe R. Zimmer - The Australian National University Concurrent & Distributed 2Systems 2017 Mutual Exclusion Uwe R. Zimmer - The Australian National University References for this chapter [Ben-Ari06] M. Ben-Ari Principles of Concurrent and Distributed Programming

More information

CPS 110 Midterm. Spring 2011

CPS 110 Midterm. Spring 2011 CPS 110 Midterm Spring 2011 Ola! Greetings from Puerto Rico, where the air is warm and salty and the mojitos are cold and sweet. Please answer all questions for a total of 200 points. Keep it clear and

More information

EE458 - Embedded Systems Lecture 8 Semaphores

EE458 - Embedded Systems Lecture 8 Semaphores EE458 - Embedded Systems Lecture 8 Semaphores Outline Introduction to Semaphores Binary and Counting Semaphores Mutexes Typical Applications RTEMS Semaphores References RTC: Chapter 6 CUG: Chapter 9 1

More information

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University Review: Motivation for Monitors and Condition Variables Semaphores are a huge step up, but: They are confusing because

More information

CS 152 Computer Architecture and Engineering. Lecture 19: Synchronization and Sequential Consistency

CS 152 Computer Architecture and Engineering. Lecture 19: Synchronization and Sequential Consistency CS 152 Computer Architecture and Engineering Lecture 19: Synchronization and Sequential Consistency Krste Asanovic Electrical Engineering and Computer Sciences University of California, Berkeley http://www.eecs.berkeley.edu/~krste

More information

Interprocess Communication and Synchronization

Interprocess Communication and Synchronization Chapter 2 (Second Part) Interprocess Communication and Synchronization Slide Credits: Jonathan Walpole Andrew Tanenbaum 1 Outline Race Conditions Mutual Exclusion and Critical Regions Mutex s Test-And-Set

More information

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 I. Logic 101 In logic, a statement or proposition is a sentence that can either be true or false. A predicate is a sentence in

More information

Multiprocessors II: CC-NUMA DSM. CC-NUMA for Large Systems

Multiprocessors II: CC-NUMA DSM. CC-NUMA for Large Systems Multiprocessors II: CC-NUMA DSM DSM cache coherence the hardware stuff Today s topics: what happens when we lose snooping new issues: global vs. local cache line state enter the directory issues of increasing

More information

Threads and Parallelism in Java

Threads and Parallelism in Java Threads and Parallelism in Java Java is one of the few main stream programming languages to explicitly provide for user-programmed parallelism in the form of threads. A Java programmer may organize a program

More information

CS 470 Spring Mike Lam, Professor. Advanced OpenMP

CS 470 Spring Mike Lam, Professor. Advanced OpenMP CS 470 Spring 2017 Mike Lam, Professor Advanced OpenMP Atomics OpenMP provides access to highly-efficient hardware synchronization mechanisms Use the atomic pragma to annotate a single statement Statement

More information

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17 Concurrency & Parallelism So far, our programs have been sequential: they do one thing after another, one thing at a. Let s start writing programs that do more than one thing at at a. Threads, Concurrency,

More information

Hoare Logic: Proving Programs Correct

Hoare Logic: Proving Programs Correct Hoare Logic: Proving Programs Correct 17-654/17-765 Analysis of Software Artifacts Jonathan Aldrich Reading: C.A.R. Hoare, An Axiomatic Basis for Computer Programming Some presentation ideas from a lecture

More information

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

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory Controlling access by concurrent processes to shared writeable data has been studied as

More information

Synchronization 1. Synchronization

Synchronization 1. Synchronization Synchronization 1 Synchronization key concepts critical sections, mutual exclusion, test-and-set, spinlocks, blocking and blocking locks, semaphores, condition variables, deadlocks reading Three Easy Pieces:

More information

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

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1 Synchronization Disclaimer: some slides are adopted from the book authors slides with permission 1 What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for?

More information

The concept of concurrency is fundamental to all these areas.

The concept of concurrency is fundamental to all these areas. Chapter 5 Concurrency(I) The central themes of OS are all concerned with the management of processes and threads: such as multiprogramming, multiprocessing, and distributed processing. The concept of concurrency

More information

CS/ECE 6780/5780. Al Davis

CS/ECE 6780/5780. Al Davis CS/ECE 6780/5780 Al Davis Today s topics: Threads basic control block scheduling semaphores Midterm (next Tues) covers Chaps & Labs 1-5 sample on the web 1 CS 5780 Lab 5 Logistics Problem not enough interrupt

More information