180 likes | 325 Views
Operating Systems Lecture 22 Semaphores Classic Synchronization Problems. Semaphores. Synchronization tool that does not require busy waiting. Semaphore S – integer variable
E N D
Operating SystemsLecture 22 Semaphores Classic Synchronization Problems 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); } 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); 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? 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. 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); } } 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. Operating System Concepts
What is the Problem here? . • Let S and Q be two semaphores initialized to 1 P0P1 wait(S); wait(Q); wait(Q); wait(S); signal(S); signal(Q); signal(Q) signal(S); 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. 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 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); } Operating System Concepts
Bounded-Buffer Problem • Assume n buffer slots for data • Shared datasemaphore full, empty, mutex;Initially:full = 0, empty = n, mutex = 1 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); 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); Operating System Concepts
Readers-Writers Problem • Shared datasemaphore mutex, wrt; int readcountInitiallymutex = 1, wrt = 1, readcount = 0 Writer's code: wait(wrt); … writing is performed … signal(wrt); 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): Operating System Concepts
Dining-Philosophers Problem • Shared data semaphore chopstick[5]; Initially all values are 1 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); Operating System Concepts