Semaphores. A semaphore is an object that consists of a. methods (e.g., functions): signal and wait. semaphore. method signal. counter.

Similar documents
CS3502 OPERATING SYSTEMS

Chapter 6: Process Synchronization

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

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

Interprocess Communication By: Kaushik Vaghani

Silberschatz and Galvin Chapter 6

PROCESS SYNCHRONIZATION

Synchronization. Silvina Hanono Wachman Computer Science & Artificial Intelligence Lab M.I.T.

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Process Synchronization

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

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

Chapter 7: Process Synchronization!

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

CHAPTER 6: PROCESS SYNCHRONIZATION

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

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Process Management And Synchronization

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

Chapter 6: Process Synchronization

Process Synchronization

Process Synchronization

Classic Problems of Synchronization

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

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

Midterm Exam. October 20th, Thursday NSC

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Dealing with Issues for Interprocess Communication

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

Parallel Processing: Performance Limits & Synchronization

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

Process Co-ordination OPERATING SYSTEMS

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

OS Process Synchronization!

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

Synchronization Principles

Concurrency. Chapter 5

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Process Synchronization

Synchronization Principles II

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Lesson 6: Process Synchronization

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Introduction to Operating Systems

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

CS370 Operating Systems

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

CS370 Operating Systems

Synchronization Basic Problem:

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

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

Concept of a process

Synchronization problems with semaphores

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

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

CSC501 Operating Systems Principles. Process Synchronization

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Process Synchronization. studykorner.org

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

Lecture 3: Intro to Concurrent Processing using Semaphores

Process Synchronization

Chapter 7: Process Synchronization. Background

Chapter 6: Process Synchronization. Module 6: Process Synchronization

5 Classical IPC Problems

Semaphores (by Dijkstra)

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

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

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

Operating systems. Lecture 12

Module 6: Process Synchronization

CSE 4/521 Introduction to Operating Systems

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

Process Coordination

Concurrency and Synchronisation

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Concurrency and Synchronisation

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

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

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

Concurrency pros and cons. Concurrent Programming Problems. Linked list example. Linked list example. Mutual Exclusion. Concurrency is good for users

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

UNIX Input/Output Buffering

Chapter 2 Processes and Threads

Chapter 6: Process Synchronization

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1

EECS 482 Introduction to Operating Systems

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

9/29/2014. CS341: Operating System Mid Semester Model Solution Uploaded Semaphore ADT: wait(), signal()

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

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

Transcription:

Semaphores 1

Semaphores A semaphore is an object that consists of a counter, a waiting list of processes, and two methods (e.g., functions): signal and wait. method signal method wait counter waiting list semaphore 2

Semaphore Method: wait void wait(sem S) { S.count--; if (S.count < 0) { add the caller to the waiting list; block(); } } After decreasing the counter by 1, if the counter value becomes negative, then add the caller to the waiting list, and block the caller. 3

Semaphore Method: signal void signal(sem S) { S.count++; if (S.count <= 0) { remove a process P from the waiting list; resume(p); } } After increasing the counter by 1, if the new counter value is not positive, then remove a process P from the waiting list, resume the execution of process P, and return 4

Important Note: 1/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(p); } } If S.count < 0,, abs(s.count) is the number of waiting processes. This is because processes are added to (resp., removed from) the waiting list only if the counter value is < 0 (resp., <= 0). 5

Important Note: 2/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(p); } } The waiting list can be implemented with a queue if FIFO order is desired. However, the correctness of a program should not depend on a particular implementation of the waiting list. Your program should not make any assumption about the ordering of the waiting list. 6

Important t Note: 3/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(p); } } The caller may be blocked in the call to wait(). The caller never blocks in the call to signal(). If S.count > 0, signal() returns and the caller continues. Otherwise, a waiting process is released and the caller continues. In this case, two processes continue. 7

The Most Important t Note: 4/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(p); } } wait() and signal() must be executed atomically (i.e., e as one uninterruptible unit). Otherwise, race conditions may occur. Homework: use execution sequences to show race conditions if wait() and/or signal() is not executed atomically. 8

Three Typical Uses of Semaphores There are three typical uses of semaphores: mutual exclusion: Mutex (i.e., Mutual Exclusion) ) locks count-down lock: Keep in mind that a semaphore has an implicit counter. notification: Indicate an event has occurred. 9

Use 1: Mutual Exclusion (Lock) initialization is important semaphore S = 1; int count = 0; Process 1 Process 2 while (1) { while (1) { // do something entry // do something S.wait(); S.wait(); count++; critical sections count--; S.signal(); S.signal(); // do something exit // do something } } What if the initial value of S is zero? S is a binary semaphore (count being 0 or 1). 10

Use 2: Count-Down Counter semaphore S = 3; Process 1 Process 2 while (1) { while (1) { // do something // do something S.wait(); S.wait(); at most 3 processes can be here!!! S.signal(); S.signal(); // do something // do something } } After three processes pass through wait(), this section is locked until a process calls signal(). 11

Use 3: Notification semaphore S1 = 1, S2 = 0; process 1 process 2 while (1) { while (1) { // do something // do something S1.wait(); notify S2.wait(); cout << 1 ; cout << 2 ; S2.signal(); notify S1.signal(); // do something // do something } } Process 1 uses S2.signal() to notify process 2, indicating I am done. Please go ahead. The output is 1 2 1 2 1 2 What if both S1 and S2 are both 0 sorboth1 s? s? What if S1 = 0 and S2 = 1? 12

Lock Example: Dining i Philosophers h Five philosophers are in a thinking - eating cycle. When a philosopher gets hungry, he sits down, picks up his left and right chopsticks, and eats. A philosopher p can eat only if he has both chopsticks. After eating, he puts down both chopsticks and thinks. This cycle continues. 13

Dining i Philosopher: h Ideas outer critical section Chopsticks are shared items (by two neighboring left chop locked philosophers) p and must be protected. Semaphore C[5] = 1; Each chopstick has a C[i].wait(); semaphore with initial C[(i+1)%5].wait(); value 1. A philosopher calls wait() has 2 chops and eats before picks up a chopstick and calls signal() to C[i].signal(); release it. C[(i+1)%5].signal(); inner critical section right chop locked 14

Dining i Philosophers: h Code semaphore C[5] = 1; philosopher i while (1) { // thinking C[i].wait(); C[(i+1)%5].wait(); // eating C[(i+1)%5].signal(); C[i].signal(); // finishes eating } wait for my left chop wait for my right chop release my right chop release my left chop Does this solution work? 15

Dining Philosophers: Deadlock! If all five philosophers sit down and pick up their left chopsticks at the same time, this program has a circular waiting and deadlocks. An easy way to remove this deadlock is to introduce a weirdo who picks up his right chopstick first! 16

Dining i Philosophers: h A Better Idea semaphore C[5] = 1; philosopher i (0, 1, 2, 3) Philosopher 4: the weirdo while (1) { while (1) { // thinking // thinking C[i].wait(); C[(i+1)%5].wait(); C[(i+1)%5].wait(); C[i].wait(); // eating // eating C[(i+1)%5].signal(); C[i].signal(); C[i].signal(); C[(i+1)%5].signal(); // finishes eating; // finishes eating } } lock left chop lock right chop 17

Dining Philosophers: Questions The following are some important questions for you to think about. We choose philosopher 4 to be the weirdo. Does this choice matter? Show that this solution does not cause circular waiting. Show that this solution does not cause circular waiting even if we have more than 1 and less than 5 weirdoes. These questions may appear as exam problems. 18

Count-Down Lock Example The naïve solution to the dining philosophers causes circular waiting. If only four philosophers are allowed to sit down, deadlock cannot occur. Why? If all four of them sit down at the same time, the right-most philosopher p can have both chopsticks! How about fewer than four? This is obvious. 19

Count-Down Lock Example semaphore C[5]= 1; semaphore Chair = 4; get a chair while (1) { // thinking Chair.wait(); C[i].wait(); C[(i+1)%5].wait(); // eating this is a count-down lock that only allows 4 to go! this is our old friend C[(i+1)%5].signal(); (); C[i].signal(); Chair.signal(); } release my chair 20

The Producer/Consumer Problem bounded-buffer Suppose we have a circular buffer of n slots. Pointer i t in (resp., out) ) points to the first empty (resp., filled) slot. Producer processes keep adding data into the buffer Consumer processes keep retrieving data from the buffer. 21

Problem Analysis buffer is implemented with an array Buf[ ] A producer deposits data into Buf[in] and a consumer retrieves info from Buf[out]. in and out must be advanced. in is shared among producers. out is shared among consumers. If Buf is full, producers should be blocked. If Buf is empty, consumers should be blocked. 22

A semaphore to protect tthe buffer. A semaphore to block producers if the buffer is full. A semaphore to block consumers if the buffer is empty. 23

no. of slots Solution semaphore NotFull=n, NotEmpty=0, Mutex=1; producer consumer while (1) { while (1) { NotFull.wait(); NotEmpty.wait(); Mutex.wait(); Mutex.wait(); Buf[in] = x; x = Buf[out]; in = (in+1)%n; out = (out+1)%n; Mutex.signal(); Mutex.signal(); NotEmpty.signal(); NotFull.signal(); } } notifications critical ii section 24

Question What if the producer code is modified as follows? Answer: a deadlock may occur. Why? while (1) { Mutex.wait(); NotFull.wait(); Buf[in] = x; order changed in = (in+1)%n; NotEmpty.signal(); Mutex.signal(); } 25

The Readers/Writers Problem Two groups of processes, readers and writers, access a shared resource by the following rules: Readers can read simultaneously. Only y one writer can write at any time. When a writer is writing, no reader can read. If there is any reader reading, all incoming writers must wait. Thus, readers have higher priority. 26

Problem Analysis We need a semaphore to block readers if a writer is writing. When a writer arrives, it must know if there are readers reading. A reader count is required which must be protected by a lock. This reader-priority version has a problem: bounded waiting condition may be violated if readers keep coming, causing the waiting writers no chance to write. 27

Readers When a reader comes in, it adds 1 to the counter. If it is the first reader, waits until no writer is writing. Reads data. Decreases the counter. If it is the last reader, notifies the waiting readers and writers that no reader is reading. 28

Writer When a writer comes in, it waits until no reader is reading and no writer is writing. Then, it writes data. Finally, notifies waiting readers and writers that no writer is writing. 29

Solution semaphore Mutex = 1, WrtMutex = 1; int RdrCount; reader writer while (1) { while (1) { Mutex.wait(); RdrCount++; if (RdrCount == 1) bl k b h d d i WrtMutex.wait(); WrtMutex.wait(); Mutex.signal(); // read data // write data Mutex.wait(); RdrCount--; if (RdrCount == 0) WrtMutex.signal(); WrtMutex.signal(); Mutex.signal(); } } blocks both readers and writers 30