Chapter 12, Slide 1 Concurrency Motivation 1: Mutual Exclusion  John and Mary share a bank acc't  withdraw x = copy balance to local machine subtract.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
6: Process Synchronization 1 1 PROCESS SYNCHRONIZATION I This is about getting processes to coordinate with each other. How do processes work with resources.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Modified from Sommerville’s originalsSoftware Engineering, 5th edition. Chapter 15 Slide 1 Function-oriented design Software Engineering Ian Sommerville.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Language Support for Concurrency. 2 Common programming errors Process i P(S) CS P(S) Process j V(S) CS V(S) Process k P(S) CS.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Concurrent computations n Concurrent units that executes in parallel n Physical parallelism n Logical parallelism n Parallel abstract machine n How does.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
University of Pennsylvania 9/28/00CSE 3801 Concurrent Programming (Critical Regions, Monitors, and Threads) CSE 380 Lecture Note 6 Insup Lee.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
Experience with Processes and Monitors in Mesa
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
©Ian Sommerville 1995 Software Engineering, 5th edition. Chapter 15Slide 1 Function-oriented design u Design with functional units which transform inputs.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12B - Synchronization and semaphores Programming Language.
Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Background on the need for Synchronization
Concurrency Motivation 1: Mutual Exclusion
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Process Synchronization
Module 7a: Classic Synchronization
Synchronization Hank Levy 1.
Subject : T0152 – Programming Language Concept
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization Hank Levy 1.
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization and semaphores
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Chapter 12, Slide 1 Concurrency Motivation 1: Mutual Exclusion  John and Mary share a bank acc't  withdraw x = copy balance to local machine subtract x give out $$ write back (balance - x)  Suppose John & Mary each withdraw $100 at the same time, from different machines: John copies balance Mary copies balance John gets $100 Mary gets $100 John writes back (balance - 100) Mary writes back (balance - 100)  New balance = balance - 100!

Chapter 12, Slide 2 Mutual Exclusion (continued) Use a variable to restrict access to the account: type gate = {open, closed}; var access: gate;  John/Mary: while access = open do; access := closed; withdraw $$; access := open  But what if John : test, access = open Mary : test, access = open -- before John has closed it John : set access = closed Mary : set access = closed John: withdraw $$ Mary: withdraw $$ etc. Problem: test/set of access is divisible

Chapter 12, Slide 3 The Producer/Consumer Model Motivation 2: Synchronization  Producer / Consumer: Producer makes items, places them in n-element buffer Consumer removes items from buffer  Important: don't put items in full buffer don't take items from empty buffer  Suppose a buffer counter t is incremented by producer and decremented by consumer: read t into private register update value of t locally write back to t

Chapter 12, Slide 4 Semaphores (Dijkstra 1965) A semaphore is a data object that can assume an integer value and can be operated on by primitives P and V.  P(s) = if s > 0 then s := s - 1 else suspend current process;  V(s) = if there is a process suspended on s then wake it up else s := s + 1;  Important: P and V are indivisible instructions. P: proberen (to try) or passeren (to pass) V: verhogen (to increase) or vrygeren (to release)

Chapter 12, Slide 5 Solving Bank Problem with Semaphores var mutex : semaphore := 1;  John: P(mutex); withdraw $$; V(mutex);  Mary: P(mutex); withdraw $$; V(mutex);

Chapter 12, Slide 6 General Producer/Consumer Model semaphore mutex := 1,-- availability control in := 0,-- # of things in buffer spaces := n;-- # of empty spaces in buffer process producer repeat produce thing; P(spaces); -- wait for buffer space P(mutex);-- wait for buffer availability put thing in buffer; V(mutex);-- free buffer V(in);-- increase # of items in buffer forever process consumer repeat P(in);-- wait until something in buffer P(mutex);-- wait for buffer availability take thing from buffer; V(mutex); -- free buffer V(spaces);-- increase # free spaces in buffer forever

Chapter 12, Slide 7 About Semaphores Each semaphore has  way to suspend processes (use process queue)  policy for selecting process to wake up. One semaphore per synchronization condition, not per resource. Low level, may be tricky and tedious to use. Deadlock quite possible.

Chapter 12, Slide 8 Monitors (Brinch Hansen & Hoare '73-'74) ADTs in a concurrent environment  used in Concurrent Pascal, Modula Instance of a monitor => shared resource Monitors are passive: data + proc defs + init code Active processes use monitors Mutual exclusion of access to monitor guaranteed by system

Chapter 12, Slide 9 Implementing a Producer/Consumer Buffer with Monitors main queue (calls to append and remove) sender... receiver buffer (type fifostorage)

Chapter 12, Slide 10 Monitor Implementation type fifostorage = monitor var contents: array [1..n] of integer; -- data tot: 0..n; -- count of items in buffer in, out: 1..n;-- “pointers” to buffer cells sender, receiver: queue; procedure entry append (item: integer); -- procedures begin if tot = n then delay (sender); contents[in] := item; in := (in mod n) + 1; tot := tot + 1; continue (receiver) end; procedure entry remove (var item: integer); begin if tot = 0 then delay (receiver); item := contents[out]; out := (out mod n) + 1; tot := tot - 1; continue (sender) end; begin -- initialization code tot := 0; in := 1; out := 1 end

Chapter 12, Slide 11 Monitor Implementation (continued) type producer = process (storage: fifostorage); var element: integer; begin cycle... storage.append (element); end end; type consumer = process (storage: fifostorage); var datum: integer; begin cycle... storage.remove (datum);... end end; var meproducer: producer; youconsumer: consumer; buffer: fifostorage; begin -- start everything init buffer, meproducer (buffer), youconsumer (buffer) end

Chapter 12, Slide 12 Monitors (continued) for cooperation, use delay and continue:  delay -- takes name of queue and suspends executing process on that queue  continue -- takes name of queue and reactivates a suspended process on that queue. In both cases, active process releases lock on monitor.

Chapter 12, Slide 13 Rendezvous (Ada) Ada concurrent units: tasks  No active/passive distinction; shared resource is represented by a task.  Entry into task is via an accept statement, often inside a select, i.e., {when =>} accept ( ) do ; end;  To other process, task entry call is (and looks) just like any procedure except it's only carried out when the task owning the entry executes the corresponding accept.  Rendezvous: entry has been invoked, and task w/entry declaration has executed accept.  Suspension: caller invokes entry when task not in accept, or task executes accept when no other task has called entry

Chapter 12, Slide 14 Ada Rendezvous (continued) Accepts:  Alternatives with true when condition are marked open. (Those without conditions are always open.)  Open entries for which an entry call has already been issued are marked available. Any available alternative may be selected (nondeterminism).  Open alternatives but no available alternatives => task suspends until one becomes available.  No open alternatives => error.  Only one entry can be accepted at a time

Chapter 12, Slide 15 Either-Or Rendezvous Task task body Data_collector is begin select -- if data is available for processing, process it -- otherwise execute the else part of the select statement accept Put_data ( Sensor_in: SENSOR_VALUE) do Process_data (Sensor_in) ; end Put_data ; else -- execute Self_test rather than wait for data Self_test ; end select ; end Data_collector ;

Chapter 12, Slide 16 Implementing Producer/Consumer with Ada Rendezvous task buffer_handler is entry append (item: in integer); entry remove (item: out integer); end; task body buffer_handler is n: constant integer := 20;-- buffer size contents: array (1..n) of integer; in, out: integer range 1..n := 1;-- “pointers” into buffer tot: integer range 0..n := 0;-- # of items currently in buffer begin loop select when tot -- buffer not full accept append (item: in integer) do contents(in) := item; end; in := (in mod n) + 1; tot := tot + 1; or when tot > 0 =>-- buffer not empty accept remove (item: out integer) do item := contents (out); end; out := (out mod n) + 1; tot := tot - 1; end select; end loop; end buffer_handler; PRODUCERCONSUMER looploop produce new value V; buffer_handler.remove (V); buffer_handler.append (V);consume V; exit when V => end of stream;exit when V => end of stream; end loop;end loop;

Chapter 12, Slide 17 Ada Sequence Counter task Counter is entry Add (N: NATURAL) ; entry Subtract (N: NATURAL) ; entry Put_value (N: NATURAL) ; entry Get_value (N: out NATURAL) ; end Counter ; task body Counter is Value: NATURAL := 0 ; begin loop select accept Add (N: NATURAL) do Value := Value + N ; end Add ; or accept Subtract (N: NATURAL) do Value := Value - N ; end Subtract ; or accept Put_value (N: NATURAL) do Value := N ; end Put_value ; or accept Get_value (N: out NATURAL) do N := Value ; end Get_value ; end select ; end loop ; end Counter ;

Chapter 12, Slide 18 Ada Transponder task type Transponder is entry Give_position (Pos: POSITION ) ; end Transponder ; task body Transponder is Current_position: POSITION ; C1, C2: Satellite.COORDS ; loop select accept Give_position (Pos: out POSITION) do Pos:= Current_position ; end Give_position ; else C1 := Satellite1.Position ; C2 := Satellite2.Position ; Current_position := Navigator.Compute (C1, C2) ; end select ; end loop ; end Transponder ;

Chapter 12, Slide 19 Concurrent Office IR System procedure Office_system is task Get_command ; task Process_command is entry Command_menu ; entry Display_indexes ; entry Edit_qualifier ; -- Additional entries here. -- One for each command end Process_commands ; task Output_message is entry Message_available ; end Output_message ; task Workspace_editor is entry Enter ; entry Leave ; end Workspace_editor ; (to be continued)

Chapter 12, Slide 20 Concurrent Office IR System - II task body Get_command is begin loop Cursor_position := Get_cursor_position ; exit when cursor positioned in workspace or (cursor positioned over menu and button pressed) Display_cursor_position ; end loop ; if In_workspace (Cursor_position) then Workspace_editor.Enter ; elsif In_command_menu (Cursor_position) then Process_command.Command_menu ; elsif In_Known_indexes (Cursor_position) then Process_command.Display_indexes ; elsif In_Current_indexes (Cursor_position) then... Other commands here... end Get_command ;

Chapter 12, Slide 21 Concurrent Office IR System - III task body Process_command is Command: COMMAND.T ; Index: INDEX.T ; begin Workspace_editor.Leave ; loop accept Command_menu do Display_command_menu ; Get_menu_selection (Command) ; Execute_menu_command (Command) ; end Command_menu; accept Display_indexes do Display_current_indexes ; Get_index_selection (Index) ; end Display_indexes;... Other commands here... end Office_system ;

Chapter 12, Slide 22 Sequenced Rendezvous Actions For the case where actions are to be in a strict sequence task body Thermocouple is begin accept Get_temperature (T: in out TEMPERATURE) do -- code here to interrogate the hardware end Get_temperature ; accept Calibrate (T: TEMPERATURE) do -- code here to calibrate the thermocouple end Calibrate ; accept Disconnect do -- code to implement a hardware shutdown end Disconnect ; end Thermocouple ;