Presentation is loading. Please wait.

Presentation is loading. Please wait.

CY2003 Computer Systems Lecture 05 Semaphores - Theory.

Similar presentations


Presentation on theme: "CY2003 Computer Systems Lecture 05 Semaphores - Theory."— Presentation transcript:

1 CY2003 Computer Systems Lecture 05 Semaphores - Theory

2 © JMU, 2004CY2003- Week 052 Overview Achieving mutual exclusion –Recall Peterson’s solution Sleep and wakeup –the producer-consumer problem Semaphores –solving the producer-consumer problem Dining philosopher problem –Deadlock –Starvation

3 Achieving Mutual Exclusion

4 © JMU, 2004CY2003- Week 054 Recall: A Real Solution (Peterson’s algorithm) By combining the idea of lock variables with the idea of taking turns, the problem of mutual exclusion can be solved The solution needs the following variables –each process has an integer id number –two shared variables are needed turn: a single flag to indicate whose turn it is interested: array of flags to indicate whether each process is currently interested in entering (or is running) its critical region –each process must know the other’s process id

5 © JMU, 2004CY2003- Week 055 Busy Waiting So far, all the software approaches have used loops of the form while ( some_condition ) do_nothing; to test for some condition to come true Such a loop is called busy waiting –continuously executing test & wasting processor time Busy waiting can also have unexpected and unwelcome effects in process scheduling –suppose the system is implementing a priority queue –if the busy process is high priority, it may never yield from its busy waiting loop

6 Sleep and Wakeup

7 © JMU, 2004CY2003- Week 057 Sleep and Wakeup In order to get around the busy waiting problem, two system calls can be implemented – sleep causes the caller to block until another process wakes it up – wakeup wakes up another sleeping process There needs to be a method for linking a process calling sleep with the process calling wakeup –either wakeup takes a single parameter: the process id of the process to be woken up, or sleep and wakeup both take a parameter to match up

8 © JMU, 2004CY2003- Week 058 The Producer-Consumer Problem A producer is adding data to a shared buffer A consumer is removing data from this buffer –how can we code this problem using sleep and wakeup so that the producer won’t overfill the buffer and the consumer won’t read from an empty buffer? int count; int N= 100; shared int item; while ( TRUE ) { produce_item(item); if ( count == N ) sleep(); enter_item(item); count= count + 1; if ( count == 1 ) wakeup(consumer); } producerconsumer int item; while ( TRUE ) { if ( count == 0 ) sleep(); remove_item(item); count= count - 1; if ( count == N - 1 ) wakeup(producer); consume_item(item); }

9 © JMU, 2004CY2003- Week 059 Another Difficulty But there is still a problem with this producer- consumer ‘solution’ using sleep and wakeup Suppose the count variable is zero –the consumer reads it and decides to go to sleep –but before the sleep starts, the consumer is pre-empted –the producer runs, enters an item into the buffer, increments count, and sends the consumer a wakeup –the wakeup arrives at the consumer before the consumer starts to sleep and so will be lost Eventually, the producer will fill the buffer & sleep –both producer & consumer will now sleep forever!

10 Semaphores

11 © JMU, 2004CY2003- Week 0511 The Semaphore Primitive A new primitive variable type, called a semaphore, was introduced by E.W. Dijkstra in 1965 –a semaphore has two operations defined down (a generalisation of sleep ) –checks to see if the value is greater than zero –if so, the value is decremented –if not, the process is put to sleep (blocks) up (a generalisation of wakeup ) –if there are processes waiting, then one is woken up –if not, the value is incremented Both these are guaranteed to be an atomic action –no other process can access the semaphore until the operation has completed or blocked

12 © JMU, 2004CY2003- Week 0512 Solving Producer-Consumer Semaphores can be used to solve many process synchronisation problems –for example, the producer-consumer problem semaphore mutex= 1;/* controls mutual exclusion */ semaphore empty= 100;/* counts the empty buffer slots */ semaphore full= 0;/* counts the full buffer slots */ shared int item; while ( TRUE ) { produce_item(item); down(empty); down(mutex); enter_item(item); up(mutex); up(full); } producer int item; while ( TRUE ) { down(full); down(mutex); remove_item(item); up(mutex); up(empty); consume_item(item); } consumer

13 © JMU, 2004CY2003- Week 0513 Semaphore Uses In the producer-consumer solution, semaphores have actually been used in two distinct manners –for mutual exclusion the mutex semaphore is used to prevent the two processes reading and writing to the shared buffer at the same time it is a called a binary semaphore because it is only used by two processes and can only have the value zero or one –for synchronisation the full and empty semaphores are used to guarantee that sequences of events do or do not occur in the right order in this case they ensure that the producer stops running when the buffer if full, and that the consumer stops running when the buffer is empty

14 The Dining Philosophers A Problem of Interprocess Communication (IPC)

15 © JMU, 2004CY2003- Week 0515 The Dining Philosophers Five philosophers are sat around a table to eat –there are only five spoons –two (left & right) are needed to eat A philosopher thinks & eats –and nothing else! After thinking for a while, a philosopher tries to pick up left & right spoon If successful, they eat for a while then put down the spoons How can this problem be solved?

16 © JMU, 2004CY2003- Week 0516 A Proposed Solution while ( TRUE ) { think(); take_spoon(i); take_spoon((i + 1) mod 5); eat(); put_spoon(i); put_spoon((i + 1) mod 5); } The functions perform the following actions –think : performs whatever action(s) thinking involves –take_spoon : waits until spoon is available, then takes it –eat : performs whatever action(s) eating involves –put_spoon : puts a spoon back on the table after use

17 © JMU, 2004CY2003- Week 0517 Deadlock Unfortunately this first proposed solution is wrong! The solution suffers from deadlock Suppose all five philosophers happen to run at the same time –each philosopher picks up the left-hand spoon –each philosopher will then enter take_spoon to obtain the right-hand and will wait for it to become available –all philosophers wait for each other there is no provision for giving up the wait

18 © JMU, 2004CY2003- Week 0518 Starvation Let’s try to improve the deadlock situation by not blocking if one spoon is not available –check left is free: if so, pick it up; check right & pick up –if not, release any spoons being held and try again Consider the following situation –each philosopher in turn picks up left hand spoon the right hand spoon is not available –each philosopher puts down the left hand spoon now loop around again! This situation is called starvation –processes continue to run, but one(+) never progress

19 © JMU, 2004CY2003- Week 0519 Another Proposal The starvation problem seems to occur if all philosophers keep picking up the left-hand spoon and releasing it in perfect synchronisation –so let’s solve the problem by adding a random amount of time after failing to acquire the right-hand spoon –this must work eventually This solution will work in practice –however, it is not completely satisfactory –particularly in time-critical or safety-critical applications

20 © JMU, 2004CY2003- Week 0520 Summary Achieving mutual exclusion –Recall Peterson’s solution Sleep and wakeup –the producer-consumer problem Semaphores –solving the producer-consumer problem Dining philosopher problem –Deadlock –Starvation


Download ppt "CY2003 Computer Systems Lecture 05 Semaphores - Theory."

Similar presentations


Ads by Google