Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process Synchronization: Semaphores

Similar presentations


Presentation on theme: "Process Synchronization: Semaphores"— Presentation transcript:

1 Process Synchronization: Semaphores
CSSE 332 Operating Systems Rose-Hulman Institute of Technology Hand out Semaphore Quiz. Quiz due on Friday at start of class. 1

2 Critical-section problem solution
Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes A critical section is a section of code in each process that may be changing a common variable, updating a shared table, writing a file, and so on. The critical-section problem is to design a protocol that the process can use to cooperate. A solution to the critical-section problem must satisfy the following three requirements: 2

3 Structure of a typical process
do { entry section critical section exit section remainder section } while (TRUE); Protocol description: Each process must request permission to enter its critical section (code in which this request is made --- entry section) Critical section code may be followed by an exit section. The remaining code is called the remainder section. Entry section and remainder section are highlighted. 3

4 Four different approaches
Software-defined approaches Hardware support Support from the OS Support from the programming language Four different approaches for solving the critical-section problem. We are going to discuss support from the OS. 4

5 Semaphore Synchronization tool that does not require busy waiting
Semaphore S – integer variable Two standard operations modify S: wait() and signal() Less complicated than using hardware instructions Can only be accessed via two indivisible (atomic) operations wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; Notes Questions: What is busy waiting and why can it be a bad thing? Semaphore S in this case is a binary semaphore or a mutex because the values cannot be negative (should be 0 and 1). If s is incremented beyond 1, it is a counting semaphore. 5

6 Semaphore as General Synchronization Tool
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 Also known as mutex locks Provides mutual exclusion Semaphore mutex; // initialized to 1 do { wait (mutex); // Critical Section signal (mutex); // remainder section } while (TRUE); Can implement a counting semaphore as a binary semaphore. Binary semaphores are called mutex locks because they are locks that provide mutual exclusion. Notes Questions: Describe how this code follows the structure of a typical process in an effort to solve the critical-section problem. Notes Questions: How do we use wait() and signal() to control access to the critical section? 6

7 Semaphore implementation
Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section. Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Note that applications may spend lots of time in critical sections and therefore this is not a good solution. So how should the OS implement wait() and signal()? Third bullet is especially true for wait() since it contains a spin loop. 7

8 Semaphore Implementation with no busy wait
Define a semaphore as a struct typedef struct { int value; struct process *queue; } semaphore; Assume same simple operations: wait(S) may block the process that invokes it signal(S) resumes the execution of a blocked process P This is a much better implementation since busy waiting is ineffective and consumes CPU time. Its really 3 operations because you have to initialize it. 8

9 Implementation wait(S): S.value--; if (S.value < 0) {
add this process P to S.queue; block(P); } signal(S): S.value++; if (S.value <= 0) { remove a process P from S.queue; wakeup(P); Block() and wakeup() are two system calls. 9

10 Allowing multiple Critical Sections
Counting semaphores s.value >= 0 - # of processes that can execute wait(s) without blocking Assumption: no signal(s) executed Each can enter its critical section. s.value < 0 - # of processes blocked on s These blocked processes are in s.queue 10

11 Deadlock and Starvation
Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P P1 wait (S); wait (Q); wait (Q); wait (S); signal (S); signal (Q); signal (Q); signal (S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process Notes Questions: What events would processes P0 and P1 be waiting for? How does this wait cause a deadlock? 11

12 Classical Synchronization Problems
Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem Today we will explore using semaphores to solve a sample bounded buffer problem. 12

13 Bounded-Buffer Problem
N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N 13

14 Bounded Buffer Problem (Cont.)
The structure of the producer process do { // produce an item in nextp wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (TRUE); 14

15 Bounded Buffer Problem (Cont.)
The structure of the consumer process do { wait (full); wait (mutex); // remove an item from buffer in nextc signal (mutex); signal (empty); // consume the item in nextc } while (TRUE); Notes Questions: Briefly describe the bounded-buffer problem. 15


Download ppt "Process Synchronization: Semaphores"

Similar presentations


Ads by Google