Synchronization: semaphores and some more stuff Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
1 Synchronization 2: semaphores and more… 1 Operating Systems, 2011, Danny Hendler & Amnon Meisels.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Synchronization: semaphores and some more stuff 1 Operating Systems, 2011, Danny Hendler & Amnon Meisels.
Synchronization Principles Gordon College Stephen Brinton.
Interprocess Communication
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
Chapter 2.3 : Interprocess Communication
What we will cover… Process Synchronization Basic Concepts
Jonathan Walpole Computer Science Portland State University
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization Solutions
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
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.
Monitors: An Operating System Structuring Concept
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CY2003 Computer Systems Lecture 06 Interprocess Communication Monitors.
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.
Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Synchronization: semaphores and some more stuff 1 Operating Systems, 2014, Meni Adler, Danny Hendler & Amnon Meisels.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Deadlock and Starvation
Interprocess Communication Race Conditions
Synchronization: semaphores and some more stuff
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization: Semaphores
Deadlock and Starvation
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Process Synchronization
Lecture 13: Producer-Consumer and Semaphores
Chapter 6: Synchronization Tools
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Critical section problem
Lecture 13: Producer-Consumer and Semaphores
Synchronization, Part 2 Semaphores
Presentation transcript:

Synchronization: semaphores and some more stuff Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

What's wrong with busy waiting?  Doesn't make sense for uni-processor o May cause priority inversion and deadlock  Wastes CPU time o But is efficient if waiting-time is short The mutual exclusion algorithms we saw used busy-waiting. What’s wrong with that? Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

What's wrong with busy waiting? Busy waiting may cause priority-inversion and deadlock  Process A's priority is higher than process B's  Process B enters the CS  Process A needs to enter the CS, busy-waits for B to exit the CS  Process B cannot execute as long as the higher-priority process A is executing/ready Priority inversion and deadlock result Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Outline  Semaphores and the producer/consumer problem  Counting semaphores from binary semaphores  Event counters and message passing synchronization Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Semaphores up(S) [the `v’ operation]  If there are blocked processes, wake-up one of them  Else S++ down(S) [the ‘p’ operation]  If S ≤ 0 the process is blocked. It will resume execution only after it is woken-up  Else S-- Two atomic operations are supported by a semaphore S:  S is non-negative  Supported by Windows, Unix, … Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Semaphores: is the following correct? up(S) [the `v’ operation]  S++  If there are blocked processes, wake-up one of them down(S) [the ‘p’ operation]  If S ≤ 0 the process is blocked. It will resume execution only after it is woken-up  S-- Two atomic operations are supported by a semaphore S: Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Consider the following bad scneario: down(S)  S=0 and process A performs down(S) – A is blocked up(S)S=1  Process B performs up(S) – S=1 A is ready down(S) S=0 & C proceeds  Process C performs down(S) – S=0 & C proceeds S=0  Process A gets a time-slice and proceeds – S=0 A single up() freed 2 down()s Operating Systems, 2011, Danny Hendler & Amnon Meisels Pseudo-code in previous slide is wrong

Implementing mutex with semaphores Shared data: semaphore lock; /* initially lock = 1 */ down(lock) Critical section up(lock) Does the algorithm satisfy mutex? Does it satisfy deadlock-freedom? Does it satisfy starvation-freedom? Yes Depends… Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

More on synchronization using semaphores Three processes p1; p2; p3 semaphoress1 = 1, s2 = 0; p1p2p3 s1s2s2 down(s1);down(s2);down(s2); A B C s2s2s1 up(s2);up(s2);up(s1); Which execution orders of A, B, C, are possible? (A B* C)* Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

P 0 P 1 down(S); down(Q); down(Q); down(S); move1 move2 up(S); up(Q); up(Q) up(S); 1 1  Example: move money between two accounts which are protected by semaphores S and Q No guarantee for correct synchronization Does this work? Deadlock! Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Negative-valued semaphores up(S)  S++  If there are blocked processes (i.e. S ≤ 0), wake-up one of them -3 down(S )  S--  If S<0 the process is blocked. It will resume execution only when S is non-negative Two atomic operations are supported by a semaphore S:  If S is negative, then there are –S blocked processes Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

type semaphore = record value: integer; L: list of process; end; Negative semaphore Implementation -3 atomic down(S): S.value--; if (S.value < 0) { add this process to S.L; sleep; } atomic up(S): S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(P); } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana L

Implementing a spin-lock with TSL  In user space, one can use TSL (test-set-lock)mutex_lock: TSLREG, mutex CMPREG, #0 ok JZEok thread_yield CALLthread_yield JMP mutex_lockok: RETmutex_unlock: MOVmutex, #0 RET Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

type semaphore = record value, flag: integer; L: list of process; end; -3 down(S): repeat until test-and-set(S.flag) S.value--; if (S.value < 0) { add this process to S.L; S.flag=0 sleep; } else S.flag=0 Implementing a negative semaphore with TSL up(S): repeat until test-and-set(S.flag) S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(P); } S.flag=0 Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana L Any problem with this code? In down(), resetting flag and sleeping should be atomic.

More on semaphore implementation  On a uni-processor, disabling interrupts may be used  TSL implementation works for multi-processors  On a multi-processor, we can use spin-lock mutual exclusion to protect semaphore access Why is this better than busy-waiting in the 1 st place? Busy-waiting is now guaranteed to be very short Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Producer-Consumer Problem  Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process  Two versions unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size buffer in out Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

2 6 Out In item1 item4 item3 item2 consumer 0 buffer producer Bounded Buffer Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Implementation using semaphores  Two processes or more use a shared buffer in memory bounded  The buffer has finite size(i.e., it is bounded)  The producer writes to the buffer and the consumer reads from it full buffer  A full buffer stops the producer empty buffer  An empty buffer stops the consumer Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Producer-Consumer Producer-Consumer implementation with semaphores #defineN100/* Buffer size */ semaphore typedefintsemaphore; semaphore semaphoremutex = 1;/* access control to critical section */ semaphore semaphoreempty = N;/* counts empty buffer slots */ semaphore semaphorefull = 0;/* counts full slots */ void producer(void) { intitem; while(TRUE) { produce_item(&item);/* generate something... */ down down(&empty);/* decrement count of empty */ down down(&mutex);/* enter critical section */ enter_item(item);/* insert into buffer */ up up(&mutex);/* leave critical section */ up up(&full);/* increment count of full slots */} Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

consumer down down up up void consumer(void) { intitem; while(TRUE){ down(&full);/* decrement count of full */ down(&mutex); /* enter critical section */ remove_item(&item);/* take item from buffer) */ up(&mutex);/* leave critical section */ up(&empty);/* update count of empty */ consume_item(item);/* do something... */ } } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana Producer-Consumer Producer-Consumer implementation with semaphores

Outline  Semaphores and the producer/consumer problem  Counting semaphores from binary semaphores  Event counters and message passing synchronization Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

 Assumes only values 0 or 1  Wait blocks if semaphore=0  Signal (up operation) either wakes up a waiting process, if there is one, or sets value to 1 (if value is already 1, signal is “wasted”)  How can we implement a counting semaphore by using binary semaphores? Binary Semaphore

Implementing a counting semaphore with binary semaphores (user space): take 1 down(S): down(S1); S S.value--; S if(S.value < 0){ up(S1); down(S2); } else up(S1); binary-semaphore S1 initially 1, S2 initially 0, S.value initially 1 up(S): down(S1); S S.value++; S if(S.value ≤ 0) up(S2); up(S1) This code does not work. Why? Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana L1: L2:

Race condition for counting semaphore take 1 1.Processes Q1 – Q4 perform down(S), Q2 – Q4 are preempted between lines L1 and L2: the value of the counting semaphore is now -3 2.Processes Q5-Q7 now perform up(S): the value of the counting semaphore is now 0 3.Now, Q2-Q4 wake-up in turn and perform line L2 (down S2) 4.Q2 runs but Q3-Q4 block. Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana There is a discrepancy between the value of S and the number of processes waiting on it

Implementing a counting semaphore with binary semaphores (user space): take 2 down(S): down(S1); S S.value--; S if(S.value < 0){ up(S1); //L1 down(S2); } //L2 up(S1);up(S): down(S1); S S.value++; S if(S.value ≤ 0) up(S2); else up(S1) Does this code work? Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana binary-semaphore S1 initially 1, S2 initially 0, S.value initially 1

only if no process waits on S2  up(S1) is performed by up(S) only if no process waits on S2  Q5 leaves up(S) without releasing S1  Q6 cannot enter the critical section that protects the counter  It can only do so after one of Q2-Q4 releases S1  This generates a “lock-step” situation: an up(), a down(), an up()…  The critical section that protects the counter is entered alternately by a producer or a consumer The effect of the added ‘else’ Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Recall the bounded-buffer algorithm #defineN100 semaphore typedefintsemaphore; semaphore semaphoremutex = 1; semaphore semaphoreempty = N; semaphore semaphorefull = 0; producer void producer(void) { intitem; while(TRUE) { produce_item(&item); down down(&empty); down down(&mutex); enter_item(item); up up(&mutex); up up(&full);} consumer down down up up void consumer(void) { intitem; while(TRUE){ down(&full); down(&mutex); remove_item(&item); up(&mutex); up(&empty); consume_item(item); } } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario Consider a Bounded buffer of 5 slots. Assume there are 6 processes each filling five slots in turn. Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario five slots are filled by the first producer Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario The second producer is blocked Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario The third producer is blocked Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario 1 1. The fourth producer is blocked Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario The fifth producer is blocked Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario All blocked producers are waiting on S2 Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario The consumer consumes an item and is blocked on Empty.S1 until a producer adds an item. Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario The consumer consumes an item and is blocked on S1, one producer adds an item. Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario 4 4. Consumer must consume, only then another producer wakes up and produces an item Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario Same as in step 3. Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Problematic Scheduling Scenario And again… Empty.Value = Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Implementing a counting semaphore with binary semaphores (user space): take 4 (Hemmendinger, 1989) down(S) down(S1); S S.value--; S if(S.value < 0){ up(S1); down(S2); down(S1); S S.wake--; S if(S.wake > 0) then up(S2);} // L3 up(S1); up(S): down(S1); S S.value++; S if(S.value <= 0) { S S.wake++; if (S.wake == 1) up(S2); } up(S1); binary-semaphore S1=1, S2=0, integer wake=0 This works Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Implementing a counting semaphore with binary semaphores (user space): take 5 (Barz, 1983) down(S) down(S2); down(S1); S.value--; if (S.value>0) then up(S2); up(S1); up(S): down(S1); S.value++; if(S.value == 1) { up(S2); } up(S1); binary-semaphore S1=1, S2=min(1, init_value), value=init_value This works, is simpler, and was published earlier(!)… Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Correctness arguments… SSS  The critical section is guarded by S1 and each of the operations down(S) and up(S) uses it to correctly update the value of S.value  After updating (and inside the critical section) both operations release the S2 semaphore only if value is positive  S S  S.value is never negative, because any process performing down(S) is blocked at S2  Signals cannot be 'wasted' Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Fairness of semaphores  Order of releasing blocked processes: enters after o Weak – up() performing process enters after (one of the) blocked processes o Strong – An upper bound on the number of entries of process that performed up() if others are waiting Unfair:  Unfair: o No guarantee about the number of times the up() performing process enters before the blocked o Open competition each time the lock is free o Imitating the Java 'wait' 'notify' mechanism o Or the spin-lock of XV6… Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Outline  Semaphores and the producer/consumer problem  Counting semaphores from binary semaphores  Monitors – Language Level Critical Section Control  Event counters and message passing synchronization Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

An Abstract Monitor name : monitor … some local declarations … initialize local data procedure name(…arguments) … do some work … other procedures Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Monitor Rules  Any process can access any monitor procedure at any time  Only one process may enter a monitor procedure  No process may directly access a monitor’s local variables  A monitor may only access it’s local variables Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Things Needed to Enforce Monitor  “wait” operation o Forces running process to sleep  “signal” operation o Wakes up a sleeping process  A condition o Something to store who’s waiting for a particular reason o Implemented as a queue Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

A Running Example – Emerson’s Kitchen kitchen : monitor occupied : Boolean; occupied := false; nonOccupied : condition; procedure enterKitchen if occupied then nonOccupied.wait; occupied = true; procedure exitKitchen occupied = false; nonOccupied.signal; Monitor Declaration Declarations / Initialization Procedure Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Multiple Conditions  Sometimes desirable to be able to wait on multiple things  Can be implemented with multiple conditions  Example: Two reasons to enter kitchen - cook (remove clean dishes) or clean (add clean dishes)  Two reasons to wait: o Going to cook, but no clean dishes o Going to clean, no dirty dishes Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Emerson’s Kitchen kitchen : monitor cleanDishes, dirtyDishes : condition; dishes, sink : stack; dishes := stack of 10 dishes sink := stack of 0 dishes procedure cook if dishes.isEmpty then cleanDishes.wait sink.push ( dishes.pop ); dirtyDishes.signal; procedure cleanDish if sink.isEmpty then dirtyDishes.wait dishes.push (sink.pop) cleanDishes.signal Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El-Sana

Monitor Implementation Using Semaphores  Variables semaphore mutex; // (initially = 1) semaphore next; // (initially = 0) int next-count = 0;  Each procedure F will be replaced by wait(mutex); … body of F; … if (next_count > 0) signal(next) else signal(mutex);  Mutual exclusion within a monitor is ensured. Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Monitor Implementation  For each condition variable x, we have: semaphore x_sem; // (initially = 0) int x-count = 0;  The operation x.wait can be implemented as: x-count++; if (next_count > 0) signal(next); else signal(mutex); wait(x_sem); x-count--; Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Monitor Implementation  The operation x.signal can be implemented as: if (x-count > 0) { next_count++; signal(x_sem); wait(next); next_count--; } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Outline  Semaphores and the producer/consumer problem  Counting semaphores from binary semaphores  Monitors  Barrier  Event counters and message passing synchronization Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Use of a barrier. (a) Processes approaching a barrier. (b) All processes but one blocked at the barrier. (c) When the last process arrives at the barrier, all of them are let through. Barriers Example from Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

Outline  Semaphores and the producer/consumer problem  Counting semaphores from binary semaphores  Monitors  Barrier  Event counters and message passing synchronization Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Event Counters  Integer counters with three operations: o Advance(E): increment E by 1, wake up relevant sleepers Sleep if E < v o Await(E,v): wait until E ≥ v. Sleep if E < v o Read(E): return the current value of E Counter value is ever increasing  Counter value is ever increasing  The Read() operation is not required for the bounded-buffer implementation in the next slide Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

producer-consumer with Event Counters (for a single producer and a single consumer) #defineN100 typedefintevent_counter; event_counterin = 0;/* counts inserted items */ event_counter out = 0;/* items removed from buffer */ void producer(void) { int item, sequence = 0; while(TRUE) { produce_item(&item); sequence = sequence + 1; /* counts items produced */ await(out, sequence - N); /* wait for room in buffer */ enter_item(item); /* insert into buffer */ advance(&in); /* inform consumer */ } } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Event counters Event counters (producer-consumer) void consumer(void) { int item, sequence = 0; while(TRUE) { sequence = sequence + 1; /* count items consumed */ await(in, sequence); /* wait for item */ remove_item(&item); /* take item from buffer */ advance(&out); /* inform producer */ consume_item(item); } } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Message Passing – no shared memory  In a multi-processor system without shared memory, synchronization can be implemented by message passing  Implementation issues: o Acknowledgements may be required (messages may be lost) o Message sequence numbers required to avoid message duplication o Unique process addresses across CPUs (domains..) o Authentication (validate sender’s identity, a multi-machine environment…)  Two main functions: o send(destination, &message); o receive(source, &message) block while waiting... Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Message Passing Producer-consumer with Message Passing #defineN100 #define MSIZE4/* message size */ typedefintmessage(MSIZE); void producer(void) { int item; message m;/* message buffer */ while(TRUE) { produce_item(&item); receive(consumer, &m);/*wait for an empty */ construct_message(&m, item); send(consumer, &m);/* send item */ } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Message passing (cont.) empties void consumer(void) { int item, i; message m; for(i = 0; i < N; i++) send(producer, &m); /* send N empties */ while(TRUE) { receive(producer, &m);/* get message with item */ extract_item(&m, &item); send(producer, &m);/* send an empty reply */ consume_item(item); } } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana

Message passing variations  Messages can be addressed to a process address or to a mailbox o Mailboxes are generated with some capacity. When sending a message to a full mailbox, a process blocks o Buffer management done by mailbox  Unix pipes - a generalization of messages … no fixed size message (blocking receive)  If no buffer is maintained by the system, then send and receive must run in lock-step. Example: Unix rendezvous Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels& Jihad El- Sana