1 / 18

Operating Systems Lecture 22 Semaphores Classic Synchronization Problems

Operating Systems Lecture 22 Semaphores Classic Synchronization Problems. Semaphores. Synchronization tool that does not require busy waiting. Semaphore S – integer variable

Download Presentation

Operating Systems Lecture 22 Semaphores Classic Synchronization Problems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating SystemsLecture 22 Semaphores Classic Synchronization Problems Operating System Concepts

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Bounded-Buffer Problem • Assume n buffer slots for data • Shared datasemaphore full, empty, mutex;Initially:full = 0, empty = n, mutex = 1 Operating System Concepts

  13. 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

  14. 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

  15. 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

  16. 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

  17. Dining-Philosophers Problem • Shared data semaphore chopstick[5]; Initially all values are 1 Operating System Concepts

  18. 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

More Related