Download presentation
Presentation is loading. Please wait.
1
Interprocess Communication (3)
Operating Systems Interprocess Communication (3)
2
Producer/Consumer Bounded Buffer
Producer puts items into a buffer Consumer removes items from buffer Producer issue: attempts to put an item in a full buffer Consumer issue: attempts to remove an item from an empty buffer
3
Producer/Consumer Model 1
prod_cons1() { Global int N = 100; int count = 0; } parbegin producer(); consumer(); parend producer() itemType item; while(1) if (count == N) sleep(); produce(); ++count; if (count == 1) wakeup(consumer); consumer() int item; if (count == 0) --count; if (count == (N – 1)) wakeup(producer) c
4
Access to Count is unconstrained
buffer is empty consumer tests count context-switch before sleep producer puts an item in the buffer increments count to 1 tries to wakeup consumer, but consumer is not asleep produces goes on filling the buffer then goes to sleep consumer runs and goes to sleep Could be solved with either peterson or tsl
5
Spinlock Semaphore ADT Proposed by Dijkstra
wait(int* S) //frequently called “down” { while( *S<= 0;) //item is in CS *S--; } signal(int* S) //frequently called “up” { *S++; } Key Ideas Apart from initialization, S is only modifiable by wait and signal All modifications to S in wait an signal are done without interruption 3. Testing S is done without interruption
6
N-Process Example N_Process() { Global int s = 1; } parbegin proc0();
parend //All N processes look like //this proc_0() while(1) wait(&s); cs(); signal(&s); proc0 runs and calls wait s == 1, so s is decremented to 0 Enter CS proc1, calls wait, fails test and sits in loop proc2, calls wait, fails test and sits in loop proc0 runs, finishes CS, calls signal, increments s proc2 runs, passes while test, decrements s, leaves down, enters CS
7
SempAhores can Synchronize processes. Proc0 before proc1
{ Global int s = 0; } parbegin proc0(); proc1(); parend Proc0() cs(); signal(&s); Proc1() wait(&s); cs()
8
PROC_0 PRODUCES X FOR PROC_1 TO CONSUME PROC_1 PRODUCES Y FOR PROC_0 TO CONSUME
synch() { Global int s1 = s2 = 0; itemType x, y; } parbegin proc0(); proc1(); parend }1 proc_0() while(1) x = produce(); signal(&s1); wait(&s2) consume(y); proc_1() { wait(s1); consume(x); y = produce(); signal(s2) Demonstrate with a table: s1, s2, x y
9
Problem with the Semaphore as defined?
Busy Wait SOLUTION: COUNTING SEMAPHORE
10
Counting Semaphore Requires Linked List of processes
typedef struct { int value; struct process *list; //a list of processes } semaphore
11
Wait and signal wait(semaphore* S) { S → value--; if (S → value < 0) { add (pid) S → list; block(pid); } } signal(semaphore* S) { S → value++; if (S → value <= 0) { pid = remove(S → list); wakeup(pid); } } “It is critical that semaphore operations be executed atomically. We must guarantee that no two process can execute wait() and signal() on the same semaphore at the same time.” (Silberschatz, p. 216). With single processors, we disable interrupts. With mulitiple processors, it becomes necessary to reintroduce busy-wait. Message passing is the answer.
12
N Process Semaphore Example
cnt_sem() { semaphore s1; s1.value = 1 parbegin { proc0(&s1) ... proc1(&s1); } parend procj(semaphor* s1) { while(1) { wait(s1); crit_sect(); signal(s1); }
13
Scenario 1. proc0() runs, calls down: value == 0, enters CS 2. proc1() runs, calls down: value == -1, fails test and blocks 3. proc2() runs, calls down: value == -2, fails test and blocks] 4. proc0() runs, finished CS, calls up: value == -1, removes p1 from sleep list, wakes up p1 lst p1 lst p1 p2 lst p1
14
Producer-Consumer With Counting Semaphores
Recall Producer/Consumer required 1 variables (count) and a buffer in which to store items produces or consumed. Both must be protected Solution with three semaphores empty: counts empty buffer slots, initially N full: counts full buffer slots, initially 0 mutex: controls access to buffer, initially 1
15
Producer-Consumer Model 2
prod_cons2() { Global sem mutex; sem empty; semfull: mutex.value = 1; empty.value = 100; full.value = 0; } parbegin { producer(); consumer(); parend producer() while(1) wait(&empty); wait(&mutex); produce(); signal(&mutex); signal(&full); consumer() wait(&full); consume(); signal(&empty);
16
Scenario 1 consumer runs value == -1, blocks on full producer runs
dec empty, dec mutex, enters cs, inc mutex, I inc full full.s == 0, wakes up consumer
17
Suppose full.s == 98, empty.s == 2, mutex.s == 1
producer runs: dec empty (so empty.s == 1), dec mutex (so mutex.s == 0), enters cs consumer runs: dec full (so full.s == 97), dec mutex (so mutex.s == -1) consumer blocks on mutex producer runs: inc mutex (so mutex.s == 0), wakes up consumer, inc full (so full.s == 98) consumer runs: enters cs, inc mutex (so mutex.s == 1), inc empty (so empty.s == 2) Semphore values: full.s == 98, empty.s == 2, mutex.s == 1 Both Producer and consumer have run to completion. Semaphore values are as they were in the beginning
18
A Trap Suppose in Producer Order of wait on mutx and empty switched
19
(more or less) Correct solutions
Peterson software two processes busy-wait TSL hardware N processes Spinlock Semaphore Hardware N processes busy-wait Counting Semaphore sleep/wakeup Limited to single processors without busy- wait.
20
Building a Model of Producer/Consumer
Primitives pipes: direct communication link between two linux processes semaphore system calls in linux
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.