Chapter 6 Semaphores.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Lecture 5: Concurrency: Mutual Exclusion and Synchronization.
Chapter 3 The Critical Section Problem
Stuff  Exam timetable now available  Class back in CS auditorium as of Wed, June 4 th  Assignment 2, Question 4 clarification.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.
Classic Synchronization Problems
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Classical Problems of Concurrency
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
Process Synchronization
Chapter 2.3 : Interprocess Communication
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Chapter 9 Spaces with LINDA. 2 Linda Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
CS3530 OPERATING SYSTEMS Summer 2014 Synchronization Chapter 6.
Concurrency, Mutual Exclusion and Synchronization.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Chapter 6. Semaphores are a simple, but successful and widely used, construct.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Interprocess Communication Race Conditions
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Semaphores Chapter 6.
Chapter 6: Synchronization Tools
CS 144 Advanced C++ Programming May 7 Class Meeting
CSE 542: Operating Systems
Presentation transcript:

Chapter 6 Semaphores

Semaphores Major advance incorporated into many modern operating systems (Unix, OS/2) A semaphore is a non-negative integer that has two indivisible, valid operations

Semaphore Operations Wait(s) (Dijkstra – P(s) – Proberen (to test) If s > 0 then s:= s - 1 else block this process Signal(s) (Dijkstra – V(s) – Verhogen (to increment) If there is a blocked process on this semaphore then wake it up else s:= s + 1

More on Semaphores The other valid operation is initialisation Two types of semaphores binary semaphores can only be 0 or 1 counting semaphores can be any non-negative integer Semaphores are an OS service implemented using one of the methods shown already usually by disabling interrupts for a very short time

The Critical Section Problem for Two Processes Note : S ← (1,ø) means semaphore S has a value of 1 and the set of blocked processes is null

State Diagram Mutual exclusion : Try to find a state (p2, q2) in which both processes are in their CS. Since there is no such state, mutual exclusion property holds No Deadlock : No states in which both processes are blocked No Starvation : If a process executes its wait statement, the next state is either CS or blocked. When blocked the only way is to a state in which the blocked process continues with a signal statement

The Critical Section Problem for N Processes Mutual exclusion and freedom from deadlock are still hold but there may be starvation

Scenario for Starvation (3 Processes) Processes p and r conspire to starve process q (see lines 3 and 7)

Order Of Execution Problems This mergesort algorithm uses two independent sort processes (may be more) and a merge process to merge the first and second halves when sorting is over The correct synchronization is achived by two binary semaphores as shown above

Producer - Consumer with a Single Buffer Program producerconsumer; Var s: semaphore; Procedure producer; begin repeat produce; wait(s); putinbuffer; signal(s); until false; End; Procedure consumer; Begin getfrombuffer; consume; Begin (* main program *) s:= 1; cobegin producer; consumer coend; End.

Producer - Consumer with Infinite Number of Buffers Program producerconsumer; Var s: semaphore; (* for mutual exclusion on access to buffer *) n: semaphore; (* to control empty and full buffer conditions *) Procedure producer; begin repeat produce; wait(s); putinbuffer; signal(s); signal(n); (* send a full buffer ready signal *) until false; End; Procedure consumer; Begin wait(n); (* wait for a full buffer *) wait(s); getfrombuffer; signal(s); consume; Begin (* main program *) s:= 1; n:= 0; cobegin producer; consumer coend; End.

Producer - Consumer with Infinite Number of Buffers (modification 1) Program producerconsumer; Var s: semaphore; n: semaphore; Procedure producer; begin repeat produce; wait(s); putinbuffer; signal(n); signal(s); until false; End; Procedure consumer; Begin wait(n); wait(s); getfrombuffer; signal(s); consume; until false; Begin (* main program *) s:= 1; n:= 0; cobegin producer; consumer coend; End. signal(s); signal(n); in producer is changed to signal(n); signal(s); Any change in solution? No change since producer can not be blocked in CS but CS is a bit long now.

Producer - Consumer with Infinite Number of Buffers (modification 2) Program producerconsumer; Var s: semaphore; n: semaphore; Procedure producer; begin repeat produce; wait(s); putinbuffer; signal(s); signal(n); until false; End; Procedure consumer; Begin wait(s); wait(n); getfrombuffer; signal(s); consume; Begin (* main program *) s:= 1; n:= 0; cobegin producer; consumer coend; End. wait(n); wait(s); in consumer is changed to wait(s); wait(n); Any change in solution? Suppose consumer enters CS with n = 0. It will be blocked on wait(n). Producer can not also enter CS. So, the system is DEADLOCKED!

Producer - Consumer with Finite Number of Buffers Program producerconsumer; Var s: semaphore; full, empty: semaphore; Procedure producer; begin repeat produce; wait(emtpy); wait(s); putinbuffer; signal(s); signal(full); until false; End; Procedure consumer; Begin wait(full); wait(s); getfrombuffer; signal(s); signal(empty); consume; Begin (* main program *) s:= 1; full:= 0; empty:= 10; (* 10 empty buffers initially *) cobegin producer; consumer coend; End.

Dining Philosophers 5 seating places, 5 plates of spagetti and 5 forks Figure is from Modern OS by Tanenbaum 5 seating places, 5 plates of spagetti and 5 forks A philosophers life cycle Repeat think; eat forever Eating can only be done with 2 forks Devise a ritual (protocol) that will allow the philosophers to eat. The protocol should satisfy mutual exclusion (no two philosophers try to use the same fork simultaneously) , free from deadlock and absense of starvation

Dining Philosophers Solution – First Attempt Program diningphilosophers; Var i : integer; fork : array[0..4] of semaphore; Procedure philosopher (i : integer); Begin repeat think; wait(fork[i]); (* get left fork *) wait(fork[(i+1) mod 5]; (* get right fork *) eat; signal(fork[i]); (* return left fork *) signal(fork[(i+1) mod 5]; (* return right fork *) until false; End; Begin (* main *) for i:= 0 to 4 do fork[i]:= 1; (* initially all forks are available *) cobegin philosopher(0); philosopher(1); philosopher(2); philosopher(3); philosopher(4); coend; End.

Comments on First Attempt Mutual exclusion is implemented by a binary semaphore fork Deadlock is possible if all 5 philosophers take the left forks simultaneously then all would wait for the right fork How to handle the deadlock and ensure liveliness? Let at the most 4 philosophers to sit and eat. Two of them can eat, one holds a fork and the other just sits One philosopher can eat and the other 3 can hold their left forks

Correct Solution Program diningphilosophers; Var fork : array[0..4] of semaphore; i : integer; table : semaphore; (* seating limit *) Procedure philosopher (i : integer); Begin repeat think; wait(table); wait(fork[i]); (* get left fork *) wait(fork[(i+1) mod 5]; (* get right fork *) eat; signal(fork[i]); (* return left fork *) signal(fork[(i+1) mod 5]; (* return right fork *) signal(table); until false; End; Begin (* main *) for i:= 0 to 4 do fork[i]:= 1; table:= 4; cobegin philosopher(0); philosopher(1); philosopher(2); philosopher(3); philosopher(4); coend; End.

Barz’s Simulation of General Semaphores K is the initial value of semaphore Semaphore gate is used to block and unblock processes Variable count holds the value of semaphore simulated Semaphore S is used as a mutex when accessing the count variable

Simulated Wait The first process to execute a simulated wait will pass through p1 since gate is initialized to 1, but others will be blocked The first process and each subsequent process (up to a total of k-1) will execute p5 so that other processes will pass through the gate The k’th process will be blocked (count is now 0) at p1

Courseware We will use the concurrent Pascal interpreter of Ben-Ari to solve our concurrent programs using semaphores. A new data type “semaphore” is available to declare semaphores. The initial value of a semaphore can be set in the main program section using an assignment (original concurent Pascal) or by an initialsem (BACI, jBACI) statement. Apart from this, you can only use semaphores in “wait” and “signal” primitives. You should not inspect or try to assign a value for a semaphore in the program. The interpreter has a new construct “cobegin ... coend” to start concurrent processes in the main section of your program The concurrent Pascal interpreter has two versions. Unfortunately both versions have slight differences

Ben-Ari’s Original Ben-Ari’s original is in the appendix of the book “Concurrent Programming By M. Ben-Ari” I have modified this SW so that it can accomodate more concurrent processes and a “for” loop can be used within the “cobegin..coend” block (which is not compatible with the version described on the next slide). You can find this version (source and executable) in my public directory

BACI and jBACI BACI stands for Ben-Ari’s Concurrent Interpreter BACI url: http://www.mines.edu/fs_home/tcamp/baci/ This software is available for Windows and Unix environments in C and Pascal. The other version jBACI is maintained by Ben-Ari himself jBACI url: http://stwww.weizmann.ac.il/g-cs/benari/jbaci/ I will use jBACI Windows and Pascal versions (You should do the same for this course). To use jBACI you should install and run the Sun java runtime environment which is available at http://java.sun.com/j2se/1.4.2/download.html

Example 1 - Tunnel A computer system is being used to control the flow of traffic through a road tunnel. For safety reasons, there must never be more than approximately N vehicles in the tunnel at one time. Traffic lights at the entrance control the entry of traffic and vehicle detectors at entrance and exit are used to measure the traffic flow. An entrance process records all vehicles entering the tunnel, and a separate exit process records all vehicles leaving. Each of these processes can, at any time, read its vehicle detector to determine how many vehicles have passed since the last reading was made. A traffic lights process controls the traffic lights at the entrance to the tunnel which is set to red whenever the number of vehicles in the tunnel equals or exceeds N, and green otherwise.

Tunnel Program program tunnel; const limit = 5; Var no_vehicles : integer; {in tunnel} red : boolean; {traffic light} screen : semaphore; {controls screen display screen} i : integer; max : integer; procedure entrance; .......... end; procedure exit; ................... end; procedure lights; ............... end; begin {main} no_vehicles := 0; max:= limit*2; red:= false; initialsem(screen,1); cobegin lights; entrance; exit coend; end.

Procedure Entrance procedure entrance; var entry_counter :integer; begin while true do if not red then begin entry_counter:= random(max); no_vehicles:= no_vehicles + entry_counter; wait(screen); write(' Entry reading .. ',entry_counter); end else begin write(' Light is now RED ... '); end; writeln(' No of vehicles in tunnel .. ',no_vehicles); signal(screen);

Procedure Exit procedure exit; var exit_counter :integer; begin while true do exit_counter:= random(no_vehicles); no_vehicles:= no_vehicles - exit_counter; wait(screen); writeln(' Vehicles passed .. ',exit_counter, ' No of vehicles in tunnel .. ',no_vehicles); signal(screen); end;

Procedure Lights procedure lights; begin while true do if no_vehicles >= limit then red:= true else red:= false; wait(screen); write(' Traffic light is .. '); if red then writeln('RED') else writeln('GREEN'); signal(screen); end;

Example 2 – The Bear and The Bees There are n honeybees and a hungry bear which share a pot of honey. The pot is initially empty; its capacity is H portions of honey. The bear sleeps until the pot is full, then eats all the honey and goes back to sleep. Each bee repeatedly gathers one portion of honey and puts it in the pot; the bee who fills the pot wakes up the bear.

Bear and Bees Program Program bear_and_bees; const n = 3; {three bees} h = 7; {portions for pot} var i : integer; pot : integer; screen : semaphore:=1; potmutex : semaphore:=1; (* access to pot is mutually exclusive *) bear_wakeup : semaphore:=0; (* bear is sleeping *) bee_fill_pot : semaphore:=h; procedure eat; var i : integer; begin for i:= 1 to random(50) do end; procedure gather_honey; var i : integer; begin for i:= 1 to random(15) do end; procedure bee(i:integer); .............. end; procedure bear; ............................. end; begin {main} pot:= 0; cobegin bee(1); bee(2); bee(3); bear coend; end.

Procedure Bee procedure bee(i:integer); begin repeat wait(bee_fill_pot); (* wait for a signal to start filling when bear finishes the pot *) wait(screen); writeln('bee : ',i,' is gathering honey '); signal(screen); gather_honey; wait(potmutex); if pot < h then pot:=pot+1; (* update pot *) wait(screen); writeln('bee : ',i,' is filling the pot which has ',pot,' portions now'); signal(screen); if pot = h then begin signal(bear_wakeup); (* wake up bear when pot is full *) wait(screen); writeln('bee : ',i,' is waking the bear up .. pot = ',pot); signal(screen) end; signal(potmutex); (* now other bees can fill the pot *) until false;

Procedure Bear procedure bear; var j : integer; begin repeat wait(screen); writeln('bear is now sleeping'); signal(screen); wait(bear_wakeup); (* bear wakes up *) wait(screen); writeln('bear is now awake and is eating ',pot,' portions'); signal(screen); pot:=0; eat; for j:= 1 to h do signal(bee_fill_pot); (* signal bees *) until false; end;

Comments on Solution Time taking actions can be simulated using a loop which executed in a random fashion as in “eat” and “gather_honey” A semaphore should be used for each write statements so that they are in order. The interpreter executes the pseudo code of a process for a number of instructions and the switches the process. If the process is interrupted while writing, the output may mixed with the output of other processes.

Example 3 – A Three Way Rendezvous { Consider three processes p1, p2, and p3. Implement a 3-way rendezvous to synchronize these processes } program synchronization; const n = 3; var s : array[1..n] of semaphore; {signal to send and wait at the rendezvous point } screen : semaphore; {for the screen} i : integer; procedure p(i :integer); ................... end; begin {main} for i:= 1 to n do initialsem(s[i],0); initialsem(screen,1); cobegin p(1); p(2); p(3) coend; end.

Procedure P procedure p(i :integer); var j,k,r :integer; begin while true do r:= random(100); wait(screen); writeln('Process ',i,' is executing for ',r,' units'); signal(screen); k:=0; for j:= 1 to r do k:= k+ 1; (* let time pass *) wait(screen); writeln('Process ',i,' is waiting for others ..'); signal(screen); for j:= 1 to n do if j <> i then signal(s[j]); (* send arrival messages *) for j:= 1 to n-1 do wait(s[i]); (* wait for reply from each process *) wait(screen); writeln('Process ',i,' has made the rendezvous ..'); signal(screen); end;

Comments on Solution When a process p reaches a rendezvous point it sends messages to the other processes to announce the fact that it had arrived. After that, it waits acknowlegements from the other processes before proceeding.