1.17k likes | 1.34k Views
Semaphores. Ref: William Stallings,Galvin, Naresh Chauhan G.Anuradha. Popular tool for process synchronization Used to protect any resource such as global shared memory that needs to be accessed and updated by many processes
E N D
Semaphores Ref: William Stallings,Galvin, Naresh Chauhan G.Anuradha
Popular tool for process synchronization Used to protect any resource such as global shared memory that needs to be accessed and updated by many processes Analogy- allotment of seminar room for various activites of college. What is a semaphore? 9/1/2014
Principle of a Semaphore For signaling semaphores are used • For sending a signal semSignal (s) is used • For receiving a signal semWait(s) is used • First defined by Dijkstra • Semaphore is a variable • A semaphore may be initialized to a nonnegative integer value. • The semWait operation decrements the semaphore value. • The semSignal operation increments the semaphore value.
wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } 9/1/2014
Types of Semaphores • Binary semaphore:-takes value 0 and 1-used for resource having a single instance • Counting Semphore:- when there are more than one process. • Mutex:-similar to binary semaphore but the key difference is that the mutex is locked and unlocked by the same process
Classical Synchronization problems • Producer consumer problem • Reader-writer problem • Barber shop • Dining philosopher
I. Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt 43 43
Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! 44
Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! 45
Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! Now imagine a customer picking items off the conveyor belt 46
Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! Now imagine many such customers picking items off the conveyor belt! 47
Producer-Consumer Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! Now imagine many such customers picking items off the conveyor belt! 48
Producer-Consumer Chef = Producer Customer = Consumer insertPtr removePtr 49
Producer-Consumer Chef = Producer Customer = Consumer insertPtr removePtr 50