Download presentation
Presentation is loading. Please wait.
1
Enforcing Mutual Exclusion, Semaphores
2
Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches Support from the OS/compiler 2
3
OS support: Semaphores Two or more processes can cooperate with each other by means of signals Two (atomic) operations: semSignal(s) : send signal via semaphore s semWait(s) : receive signal via semaphore s ; process is suspended until signal is received Observation: this is an abstract data type (a set of values together with operations on those values) Any complex coordination requirement can be satisfied by appropriate signal structure 3
4
Mutual Exclusion Using Semaphores Shared data: semaphore mutex; /* Initial value determines … */ Process Pi: do { semWait(mutex); /* … effect of first call */ /* critical section */ semSignal(mutex); /* remainder section */ } while (1); 4
5
Semaphore Implementations Stallings: Semaphore initialized to nonnegative integer value semWait(s) : s = s – 1 if s < 0 then block process, otherwise continue executing semSignal(s) : s = s + 1 if s <= 0 then unblock one blocked process Example of a counting or general semaphore vs. a binary semaphore (later) If blocked processes are unblocked in FIFO order, the semaphore is strong; otherwise weak 5
6
Semaphore Implementations (cont.) semWait(S) : S.value--; if (S.value < 0) { add this process to S.L; block; } semSignal(S) : S.value++; if (S.value <= 0) { remove a process P from S.L; add P to ready queue } 6
7
Semaphore Implementations (cont.) POSIX int sem_init( sem_t *, int, unsigned ) Initializes the semaphore to a nonnegative integer value int sem_post( sem_t * ) If semaphore value is zero and at least one thread is blocked on the semaphore, Increments the semaphore Unblocks one of those threads Otherwise, just increments the semaphore int sem_wait( sem_t *, int, unsigned ) If semaphore value is zero, thread blocks If semaphore value is positive, decrements semaphore (and continues executing) Other operations for convenience and efficiency (see man semaphore.h ) 7
8
Classical Synchronization Problems Producer/Consumer and Bounded-Buffer Problems Readers and Writers Problem Dining-Philosophers Problem 8
9
Producer/Consumer Problem One or more producers are generating data and placing these in a buffer A single consumer is taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time 9
10
Producer/Consumer Problem 10
11
Producer/Consumer Problem Infinite buffer – no possibility of overflow Bounded buffer – possibility of overflow Empty buffer – consumer must be stopped Mutual exclusion and synchronization 11
12
Producer Process (Infinite Buffer) while (true) { /* produce item v */ b[in] = v; in++; } 12
13
Consumer Process (Infinite Buffer) while (true) { while (in <= out) /*do nothing */; w = b[out]; out++; /* consume item w */ } 13
14
14
15
15
16
Producer with Finite Circular Buffer while (true) { /* produce item v */ while ((in + 1) % n == out) /* do nothing */; b[in] = v; in = (in + 1) % n } 16
17
Consumer with Finite Circular Buffer while (true) { while (in == out) /* do nothing */; w = b[out]; out = (out + 1) % n; /* consume item w */ } 17
18
18
19
19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.