Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.1 Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.

Similar presentations


Presentation on theme: "Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.1 Operating System Concepts Operating Systems Lecture 22 Semaphores Classic."— Presentation transcript:

1 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.1 Operating System Concepts Operating Systems Lecture 22 Semaphores Classic Synchronization Problems

2 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.2 Operating System Concepts 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); }

3 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.3 Operating System Concepts 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);

4 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.4 Operating System Concepts 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?

5 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.5 Operating System Concepts 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.

6 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.6 Operating System Concepts 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); } }

7 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.7 Operating System Concepts 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.

8 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.8 Operating System Concepts What is the Problem here?. Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(S);wait(Q); wait(Q);wait(S);  signal(S);signal(Q); signal(Q)signal(S);

9 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.9 Operating System Concepts 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.

10 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.10 Operating System Concepts 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

11 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.11 Operating System Concepts 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); }

12 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.12 Operating System Concepts Bounded-Buffer Problem Assume n buffer slots for data Shared data semaphore full, empty, mutex; Initially: full = 0, empty = n, mutex = 1

13 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.13 Operating System Concepts 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);

14 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.14 Operating System Concepts 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);

15 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.15 Operating System Concepts 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);

16 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.16 Operating System Concepts 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):

17 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.17 Operating System Concepts Dining-Philosophers Problem Shared data semaphore chopstick[5]; Initially all values are 1

18 Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.18 Operating System Concepts 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);


Download ppt "Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 7.1 Operating System Concepts Operating Systems Lecture 22 Semaphores Classic."

Similar presentations


Ads by Google