Synchronization: Semaphores

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

Shared Variables and Interference

Shared Variables and Interference Solved Shared Variables and Interference CS 536: Science of Programming, Fall 2018 A. Why Parallel programs can coordinate their work using shared variables, but it s important for threads to not interfere

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

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

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

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 11 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel Feedback Queue: Q0, Q1,

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

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 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

Chapters 5 and 6 Concurrency

Chapters 5 and 6 Concurrency Operating Systems: Internals and Design Principles, 6/E William Stallings Chapters 5 and 6 Concurrency Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Concurrency When several processes/threads

More information

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

Concurrent Computing

Concurrent Computing Concurrent Computing Introduction SE205, P1, 2017 Administrivia Language: (fr)anglais? Lectures: Fridays (15.09-03.11), 13:30-16:45, Amphi Grenat Web page: https://se205.wp.imt.fr/ Exam: 03.11, 15:15-16:45

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

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

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

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems Concurrency 1 Concurrency Execution of multiple processes. Multi-programming: Management of multiple processes within a uni- processor system, every system has this support, whether big, small or complex.

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

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

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

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

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

CS Operating Systems

CS Operating Systems CS 4500 - Operating Systems Module 4: The Producer-Consumer Problem and Solution Methods Stanley Wileman Department of Computer Science University of Nebraska at Omaha Omaha, NE 68182-0500, USA June 3,

More information

CS Operating Systems

CS Operating Systems CS 4500 - Operating Systems Module 4: The Producer-Consumer Problem and Solution Methods Stanley Wileman Department of Computer Science University of Nebraska at Omaha Omaha, NE 68182-0500, USA June 3,

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

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write Goals for Today CS162 Operating Systems and Systems Programming Lecture 5 Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors and condition variables Semaphores,

More information

Synchronization: semaphores and some more stuff. Operating Systems, Spring 2018, I. Dinur, D. Hendler and R. Iakobashvili

Synchronization: semaphores and some more stuff. Operating Systems, Spring 2018, I. Dinur, D. Hendler and R. Iakobashvili Synchronization: semaphores and some more stuff 1 What's wrong with busy waiting? The mutual exclusion algorithms we saw used busy-waiting. What s wrong with that? Doesn't make sense for uni-processor

More information

Concurrent Processes Rab Nawaz Jadoon

Concurrent Processes Rab Nawaz Jadoon Concurrent Processes Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS Lahore Pakistan Operating System Concepts Concurrent Processes If more than one threads

More information

CS-537: Midterm Exam (Spring 2001)

CS-537: Midterm Exam (Spring 2001) CS-537: Midterm Exam (Spring 2001) Please Read All Questions Carefully! There are seven (7) total numbered pages Name: 1 Grading Page Points Total Possible Part I: Short Answers (12 5) 60 Part II: Long

More information

Process/Thread Synchronization

Process/Thread Synchronization CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov February 14, 2013 Reading Assignment 7 Chapter 7 Deadlocks, due 2/21 2/14/13 CSE325: Synchronization

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

Process Synchronization

Process Synchronization Process Synchronization Concurrent access to shared data may result in data inconsistency Multiple threads in a single process Maintaining data consistency requires mechanisms to ensure the orderly execution

More information

Interrupts on the 6812

Interrupts on the 6812 CS/ECE 6780/5780 Al Davis Today s topics: Threads restart code from last lecture move on to scheduling & semaphores Midterm (next Tues) covers Chaps & Labs 1-5 sample on the web 1 CS 5780 Interrupts on

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

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 7: Synchronization Administrivia Homework 1 Due today by the end of day Hopefully you have started on project 1 by now? Kernel-level threads (preemptable

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

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

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements HW #3 is coming, due Friday Feb. 25, a week+ from now PA #2 is coming, assigned about next Tuesday Midterm is tentatively

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

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XXVI April 27 th, 2006 1 Roadmap Shared Memory Synchronization Spin Locks Barriers Semaphores Monitors 2 1 Memory Architectures Distributed Memory Shared Memory

More information

Silberschatz and Galvin Chapter 6

Silberschatz and Galvin Chapter 6 Silberschatz and Galvin Chapter 6 Process Synchronization CPSC 410--Richard Furuta 2/26/99 1 Topics discussed Process synchronization Mutual exclusion--hardware Higher-level abstractions Ð Semaphores Ð

More information

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

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set? Last Class: Synchronization Review Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical

More information

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

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

COMP 3430 Robert Guderian

COMP 3430 Robert Guderian Operating Systems COMP 3430 Robert Guderian file:///users/robg/dropbox/teaching/3430-2018/slides/06_concurrency/index.html?print-pdf#/ 1/76 1 Concurrency file:///users/robg/dropbox/teaching/3430-2018/slides/06_concurrency/index.html?print-pdf#/

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

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

Process Synchronization

Process Synchronization CSC 4103 - Operating Systems Spring 2007 Lecture - VI Process Synchronization Tevfik Koşar Louisiana State University February 6 th, 2007 1 Roadmap Process Synchronization The Critical-Section Problem

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

Spring 2018 PhD Qualifying Exam in Languages

Spring 2018 PhD Qualifying Exam in Languages Spring 2018 PhD Qualifying Exam in Languages Illinois Institute of Technology Department of Computer Science Monday, January 29, 2018 Instructions This exam is closed book and closed notes. Read each problem

More information

CS 167 Final Exam Solutions

CS 167 Final Exam Solutions CS 167 Final Exam Solutions Spring 2018 Do all questions. 1. [20%] This question concerns a system employing a single (single-core) processor running a Unix-like operating system, in which interrupts are

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

T Reactive Systems: Kripke Structures and Automata

T Reactive Systems: Kripke Structures and Automata Tik-79.186 Reactive Systems 1 T-79.186 Reactive Systems: Kripke Structures and Automata Spring 2005, Lecture 3 January 31, 2005 Tik-79.186 Reactive Systems 2 Properties of systems invariants: the system

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

Forward Assignment; Strongest Postconditions

Forward Assignment; Strongest Postconditions 3/1 new version Forward Assignment; Strongest Postconditions CS 536: Science of Programming, Spring 2018 A. Why? At times, a forward version of the assignment rule is more appropriate than the backward

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

Multitasking / Multithreading system Supports multiple tasks

Multitasking / Multithreading system Supports multiple tasks Tasks and Intertask Communication Introduction Multitasking / Multithreading system Supports multiple tasks As we ve noted Important job in multitasking system Exchanging data between tasks Synchronizing

More information

Introduction to Operating Systems

Introduction to Operating Systems Introduction to Operating Systems Lecture 4: Process Synchronization MING GAO SE@ecnu (for course related communications) mgao@sei.ecnu.edu.cn Mar. 18, 2015 Outline 1 The synchronization problem 2 A roadmap

More information

Synchronization problems with semaphores

Synchronization problems with semaphores Synchronization problems with semaphores Lecture 4 of TDA384/DIT391 (Principles of Concurrent Programming) Carlo A. Furia Chalmers University of Technology University of Gothenburg SP3 2017/2018 Today

More information

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018 SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T 2. 3. 8 A N D 2. 3. 1 0 S P R I N G 2018 INTER-PROCESS COMMUNICATION 1. How a process pass information to another process

More information

Chapter 8. Basic Synchronization Principles

Chapter 8. Basic Synchronization Principles Chapter 8 Basic Synchronization Principles Need for Synchronization Multiprogramming Multiple concurrent, independent processes Those processes might want to coordinate activities Proc A { while (true)

More information

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems Synchronization CS 475, Spring 2018 Concurrent & Distributed Systems Review: Threads: Memory View code heap data files code heap data files stack stack stack stack m1 m1 a1 b1 m2 m2 a2 b2 m3 m3 a3 m4 m4

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

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

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

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L11 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel feedback queue:

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

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6. Part Three - Process Coordination Chapter 6: Synchronization 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure

More information

Concurrency: Mutual Exclusion and Synchronization

Concurrency: Mutual Exclusion and Synchronization 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

Synchronization for Concurrent Tasks

Synchronization for Concurrent Tasks Synchronization for Concurrent Tasks Minsoo Ryu Department of Computer Science and Engineering 2 1 Race Condition and Critical Section Page X 2 Algorithmic Approaches Page X 3 Hardware Support Page X 4

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson s Solution 6.4 Synchronization Hardware 6.5 Mutex Locks 6.6 Semaphores 6.7 Classic

More information

Semaphores. May 10, Mutual exclusion with shared variables is difficult (e.g. Dekker s solution).

Semaphores. May 10, Mutual exclusion with shared variables is difficult (e.g. Dekker s solution). Semaphores May 10, 2000 1 Introduction Mutual exclusion with shared variables is difficult (e.g. Dekker s solution). Generalising to an arbitrary number of processes is also nontrivial (e.g. Lamport s

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

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

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

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Chapter 5 Concurrency: Mutual Exclusion and Synchronization 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

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

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

The mutual-exclusion problem involves making certain that two things don t happen at once. A non-computer example arose in the fighter aircraft of

The mutual-exclusion problem involves making certain that two things don t happen at once. A non-computer example arose in the fighter aircraft of The mutual-exclusion problem involves making certain that two things don t happen at once. A non-computer example arose in the fighter aircraft of World War I (pictured is a Sopwith Camel). Due to a number

More information

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

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

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

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Midterm Exam. October 20th, Thursday NSC

Midterm Exam. October 20th, Thursday NSC CSE 421/521 - Operating Systems Fall 2011 Lecture - XIV Midterm Review Tevfik Koşar University at Buffalo October 18 th, 2011 1 Midterm Exam October 20th, Thursday 9:30am-10:50am @215 NSC Chapters included

More information

Concurrency. Lecture 14: Concurrency & exceptions. Why concurrent subprograms? Processes and threads. Design Issues for Concurrency.

Concurrency. Lecture 14: Concurrency & exceptions. Why concurrent subprograms? Processes and threads. Design Issues for Concurrency. Lecture 14: Concurrency & exceptions Concurrency Processes and threads Semaphores, monitors and message passing Exception handling Concurrency Is is often desirable or necessary to execute parts of programs

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

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

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

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras Week 06 Lecture 29 Semaphores Hello. In this video, we will

More information

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section ECE 462 Object-Oriented Programming g using C++ and Java Scheduling and Critical Section Yung-Hsiang Lu yunglu@purdue.edu d YHL Scheduling and Critical Section 1 Thread States born terminated ready running

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

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

Thread Synchronization: Too Much Milk

Thread Synchronization: Too Much Milk Thread Synchronization: Too Much Milk 1 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing mutual exclusion with memory reads and writes

More information

Types, Expressions, and States

Types, Expressions, and States 8/27: solved Types, Expressions, and States CS 536: Science of Programming, Fall 2018 A. Why? Expressions represent values in programming languages, relative to a state. Types describe common properties

More information

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code Old Producer Process Code Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Our

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

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

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L10 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Development project: You

More information

CSC501 Operating Systems Principles. Process Synchronization

CSC501 Operating Systems Principles. Process Synchronization CSC501 Operating Systems Principles Process Synchronization 1 Last Lecture q Process Scheduling Question I: Within one second, how many times the timer interrupt will occur? Question II: Within one second,

More information

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages CS4411 Intro. to Operating Systems Exam 1 Fall 2005 (October 6, 2005) 1 CS4411 Intro. to Operating Systems Exam 1 Fall 2005 150 points 10 pages Name: Most of the following questions only require very short

More information