Download wait(S1)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Operating Systems
Lecture 22
Semaphores
Classic Synchronization Problems
Operating System Concepts
7.1
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Semaphores
 Synchronization tool that does not require busy waiting.
 Semaphore S – integer variable
 can only be accessed via two indivisible (atomic)
operations (Note: order of wait instructions corrected from
last set of slides).
wait (S) {
S--;
if (S < 0) then block(P);
}
signal (S) {
S++;
if (S <= 0) then wakeup(P);
}
Operating System Concepts
7.2
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Critical Section of n Processes
 Shared data:
semaphore mutex; //initially mutex = 1
 Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (1);
Operating System Concepts
7.3
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Questions about Semaphore
 Suppose mutex is initialized to 1.
 What does mutex == 1 mean?
 What does mutex == 0 mean?
 What does mutex < 0 mean?
 When should mutex be initialized to value > 1?
Operating System Concepts
7.4
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Semaphore Implementation
 Define a semaphore as a record
typedef struct {
int value;
struct process *L;
} semaphore;
 Assume two simple operations:
 block suspends the process that invokes it.
 wakeup(P) resumes the execution of a
blocked process P.
Operating System Concepts
7.5
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Implementation
 Semaphore operations now defined as
void wait(semaphore S) {
S.value--;
if (S.value < 0) {
add this process to S.L;
block( );
}
}
void signal(semaphore S) {
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
}
Operating System Concepts
7.6
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Notes on Implementation
 If the semaphore has a negative value, the magnitude
of the value indicates the number of processes waiting
on that semaphore.
 One can use a FIFO queue to ensure bounded
waiting. (A LIFO queue can lead to starvation).
 The wait and signal must be executed atomically.
 In a single processor environment, can disable
interrupts during the wait and signal function calls.
 In a multiprocessor environment, can use a solution
to the critical section problem discussed earlier.
Either a hardware solution (e.g. TestAndSet) if
available, or a software solution.
Operating System Concepts
7.7
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
What is the Problem here?
.
 Let S and Q be two semaphores initialized to 1
P0
wait(S);
wait(Q);

signal(S);
signal(Q)
Operating System Concepts
P1
wait(Q);
wait(S);

signal(Q);
signal(S);
7.8
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Two Types of Semaphores
 Counting semaphore – integer value can
range over an unrestricted domain.
 Binary semaphore – integer value can range
only between 0 and 1; can be simpler to
implement.
 Can implement a counting semaphore S as a
binary semaphore.
Operating System Concepts
7.9
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Implementing S as a Binary Semaphore
 Data structures:
binary-semaphore S1, S2;
int C:
 Initialization:
S1 = 1
S2 = 0
C = initial value of semaphore S
Operating System Concepts
7.10
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Implementing S
 wait operation
wait (S) {
wait(S1);
C--;
if (C < 0) {
signal(S1);
wait(S2);
}
signal(S1);
}
 signal operation
signal (S) {
wait(S1);
C ++;
if (C <= 0)
signal(S2);
else
signal(S1);
}
Operating System Concepts
7.11
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Bounded-Buffer Problem
 Assume n buffer slots for data
 Shared data
semaphore full, empty, mutex;
Initially:
full = 0, empty = n, mutex = 1
Operating System Concepts
7.12
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Bounded-Buffer Problem Producer Process
do {
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
} while (1);
Operating System Concepts
7.13
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Bounded-Buffer Problem Consumer Process
do {
wait(full)
wait(mutex);
…
remove an item from buffer to nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
} while (1);
Operating System Concepts
7.14
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Readers-Writers Problem
 Shared data
semaphore mutex, wrt;
int readcount
Initially
mutex = 1, wrt = 1, readcount = 0
Writer's code:
wait(wrt);
…
writing is performed
…
signal(wrt);
Operating System Concepts
7.15
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Readers-Writers Problem Reader Process
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):
Operating System Concepts
7.16
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Dining-Philosophers Problem
 Shared data
semaphore chopstick[5];
Initially all values are 1
Operating System Concepts
7.17
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Dining-Philosophers Problem
 Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])
…
eat
…
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
…
think
…
} while (1);
Operating System Concepts
7.18
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Related documents