Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.

Similar presentations


Presentation on theme: "Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections."— Presentation transcript:

1 Semaphores

2 Readings r Silbershatz: Chapter 6

3 Mutual Exclusion in Critical Sections

4 RoadMap r Today there are libraries that provide application programmers with semaphores r Semaphores are used by programmers to m ensure mutual exclusion m send signals form one process to another r Let’s first talk about semaphores and then the OS support for them.

5 What is a semaphore? r A semaphore is an integer variable with the following three operations. 1. Initialize: You can initialize the semaphore to any non-negative value. 2. Decrement: A process can decrement the semaphore, if its value is positive. If value is 0, the process blocks (gets put into queue). It is said to be sleeping on the semaphore. This is the down operation. 3. Increment: If value is 0 and some processes are sleeping on the semaphore, one is unblocked. Otherwise, value is incremented. This is the up operation. r All semaphore operations are implemented as primitive actions (possibly using TSL ). r In Unix based system, unblocking is done using the signal system call.

6 What is a Semaphore? r Use down before entering a critical section r Use up after finishing with a critical section i.e., r Example: Assume S is initialized to 1. ……… down(S); //critical section up(S);

7 Using Semaphores Process P 0 ……………….. down(S); //critical section up(S); ……………….. Process P 1 ……………….. down(S); //critical section up(S); ………………..

8 Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Initialize the semaphore variable, S, to 1 m Why not zero?

9 Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Now what would happen if P 0 executes the down operation? m The semaphore S is currently 1. m It becomes 0 and P 0 enters the critical section

10 Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Now what would happen if P 1 executes the down operation? m The semaphore S is currently 0 m P 1 blocks

11 Using Semaphores Process P 0 Process P 1 down(S); critical section critical section up(S); r Assume now that P 0 is done with the critical section r It calls the up function m P 1 is unblocked m If there was no process waiting to enter the critical section the value of S would become one

12 Using Semaphores r What happens if there are three processes: P 0,P 1,P 2 r Assume P 0 enters its critical section r If P 1 and P 2 execute the down operation they will block r When P 0 leaves the critical section then P 1 is unblocked allowing P 1 to enter its critical section m P 2 is still blocked r What if P 0 wants to enter its critical section again when P 1 is in it?

13 Using Semaphores r What if we want to allow 10 processes to use a critical section? r How would we initialize a semaphore, S?

14 Semaphore r Binary Semaphore: Value is either 0 or 1 m Often referred to as a mutex r Counting Semaphore: Value ranges from 0 to N where N can be any integer number

15 Deadlock r Deadlock – Two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes r Something to watch for

16 Deadlock r Example: Let S and Q be two semaphores initialized to one Process P 0 Process P 1 down(S); down(Q); down(Q); down(S); …… …. up(S); up(Q); up(Q); up(S);

17 Implementation r With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: m value (of type integer) m pointer to next record in the list

18 Implementation r A semaphore can be defined as a C struct along these lines: typedef struct { int value; struct process *list; } semaphore

19 Implementation r down() operation can be defined as down(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); } r The block() operation suspends the process that invokes it.

20 Implementation r up() operation can be defined as up(semaphore *S) { S->value++; if (S->value ≤ 0) { remove process P from S->list; wakeup(P); } r The wakeup() operation sends a signal that represents an event that the invoking process is no longer in the critical section

21 Implementation r BTW, the implementation just described is how Linux implements semaphores r The up and down operations represent require access to a critical section which is the semaphore variable r Need hardware/OS support e.g., m Signals,TSL m Signals allow for a “message” to be sent to processes

22 Semaphore Implementation r When the up is executed a blocked process is woken up. This is done using signals r Semaphore operations are critical sections – use TSL.

23 Questions r How are multiple processes prevented from being in the critical section ? r Why different than disabling interrupts? r Which is better in a multicore system? m Disabling interrupts or TSL test?

24 Summary r Defined race condition r Examined different solutions


Download ppt "Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections."

Similar presentations


Ads by Google