page 28 S e m a p h o r e s D e k k e r ’ s a l g o r i t h m s o l v e s t h e m u t u a l e x c l u s i o n p r o b l e m o n a s h a r e d m e m o r y m a c h i n e w i t h n o s u p p o r t f r o m t h e h a r d w a r e o r s o f t w a r e . S e m a p h o r e s a r e a h i g h e r l e v e l c o n c e p t t h a n a t o m i c i n s t r u c t i o n s . T h e y a r e a t o m i c a c t i o n s a n d u s u a l l y i m p l e m e n t e d w i t h i n t h e o p e r a t i n g s y s t e m . A s e m a p h o r e S i s a n o n - n e g a t i v e i n t e g e r v a r i a b l e t h a t h a s e x a c t l y t w o o p e r a t i o n s d e f i n e d f o r i t . P ( S ) I f S > 0 t h e n S = S - 1 , o t h e r w i s e s u s p e n d t h e p r o c e s s . V ( S ) I f t h e r e a r e p r o c e s s e s s u s p e n d e d o n t h i s s e m a p h o r e w a k e o n e o f t h e m , e l s e S = S + 1 . A n i m p o r t a n t p o i n t i s t h a t V ( S ) , a s i t i s c u r r e n t l y d e f i n e d , d o e s n o t s p e c i f y w h i c h o f t h e s u s p e n d e d p r o c e s s e s t o w a k e . S e m a p h o r e I n v a r i a n t s T h e f o l l o w i n g i n v a r i a n t s a r e t r u e f o r s e m a p h o r e s . S ≥ 0 S = S 0 + # V - # P w h e r e S 0 i s t h e i n i t i a l v a l u e o f s e m a p h o r e S .
26
Embed
Semaphores Dekker’s algorithm solves the mutual exclusion ...mcrane/CA463/Lecture2.pdf · Dekker’s algorithm solves the mutual exclusion problem on a shared memory machine with
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
page 28
Semaphores
Dekker’s algorithm solves the mutual exclusionproblem on a shared memory machine with no supportfrom the hardware or software. Semaphores are a higherlevel concept than atomic instructions. They are atomicactions and usually implemented within the operatingsystem.
A semaphore S is a non-negative integer variablethat has exactly two operations defined for it.
P(S) If S > 0 then S = S-1, otherwise suspend theprocess.
V(S) If there are processes suspended on thissemaphore wake one of them, else S = S + 1.
An important point is that V(S), as it is currentlydefined, does not specify which of the suspendedprocesses to wake.
Proof: Let #CS be the number of processes in theircritical sections. We need to prove that the following isan invariant.
#CS + mutex = 1
i) #CS = #P - #V ;from the program structure
ii) mutex = 1 + #V - #P ;semaphore invariant
iii) mutex = 1 - #CS ;from i) and ii)
iv) #CS + mutex = 1 ; from iii)
QED
page 30
Theorem The program cannot deadlock.
Proof: This would require all processes to besuspended in their P(mutex) operations. Then mutex = 0and #CS = 0 since no process is in its critical section.The critical section invariant just proven is :
#CS + mutex = 1 ⇒ 0 + 0 = 1
which is impossible.
Types of Semaphores
What we defined earlier is a general semaphore. Abinary semaphore is a semaphore that can only take thevalues 0 and 1.
The choice of suspended process to wake gives thefollowing definition.
Blocked-set semaphore Awakens any one of thesuspended processes.
Blocked-queue semaphore The suspended processesare kept in FIFO and are awakened in the same orderthey were suspended. This is the type of semaphoreimplemented in the SR language.
Busy-wait semaphore The value of the semaphoreis tested in a busy wait loop, with the test being atomic.There may be interleavings between cycles of the loop.
page 31
Theorem With busy-wait semaphores, starvation ispossible.
Proof: Consider the following execution sequence for2 processes.
i) P1 executes P(mutex) and enters its critical section.
ii) P2 executes P(mutex), finds mutex = 0 and loops.
iii) P1 finishes critical section, executes V(mutex) loopsback and execute P(mutex) and enters its criticalsection.
iv) P2 test mutex, finds mutex = 0, and loops.
Theorem With blocked-queue semaphores, starvation isimpossible.
Proof: If P1 is blocked on mutex there will be at mostN-1 processes ahead of P1 in the queue. Therefore afterN-1 V(mutex) P1 will enter its critical section.
Theorem With blocked-set semaphores, starvation ispossible for N ≥ 3.
Proof: If there are 3 processes it is possible toconstruct an execution sequence such that there arealways 2 processes blocked on a semaphore. V(mutex) is
page 32
required to only wake one of them, so it could alwaysignore one and leave that process starved.
The Producer-Consumer Problem
This type of problem has two types of processes:
Producers processes that, due to some internalactivity, produce data to be sent toconsumers.
Consumers processes that on receipt of a data elementconsume the data in some internalcomputation.
We could connect these processes in a synchronousmanner, such that data is only transmitted when aproducer is ready to send it and a consumer is ready toreceive it. A more flexible method is to connect theproducers and the consumers by a buffer which is aqueue.
If this was an infinite buffer (wow!) then thefollowing invariants should hold true for the buffer.
#elements ≥ 0
#elements = 0 + in_pointer - out_pointer
These invariants are exactly the same as thesemaphore invariants with a semaphore called elementsand an initial value 0.
page 33
var buffer [?]:intvar in_pointer:int := 0, out_pointer:int := 0sem elements := 0
process producerdo true ->
buffer [in_pointer] := produce ()in_pointer := in_pointer + 1V(elements)
This can be modified for real bounded circularbuffers by using another semaphore to count the emptyplaces in the buffer.
As an exercise prove the following:
(i) the program cannot deadlock, (ii) neither process isstarved and (iii) the program never removes data from anempty buffer or appends data to a full buffer.
page 34
const N := 100var buffer [N]:intvar in_pointer:int := 0, out_pointer:int := 0sem elements := 0sem spaces := N
process producervar i:int
do true ->i := produce ()P (spaces)buffer [in_pointer] := iin_pointer := (in_pointer + 1) mod NV (elements)
odend
process consumervar i:int
do true ->P (elements)i := buffer [out_pointer]out_pointer := (out_pointer + 1) mod NV (spaces)consume (i)
end
page 35
The Dining Philosophers Problem
The Problem
An institution hires five philosophers to solve a verydifficult problem. Each philosopher only engages in twoactivities - thinking and eating. Meals are taken in thediningroom which has a table set with five plates andfive forks. In the centre of the table is a bowl of spaghettithat is endlessly replenished. The philosophers, not beingthe most dextrous of individuals, requires two forks toeat; and may only pick up the forks immediately to hisleft and right.
For this system to operate correctly it is requiredthat:
a) A philosopher eats only if he has two forks.
b) No two philosophers can hold the same forksimultaneously.
page 36
c) No deadlock.
d) No individual starvation.
e) Efficient behaviour under the absence of contention.
This problem is a generalisation of multipleprocesses accessing a set of shared resources; e.g. anetwork of computers accessing a bank of printers.
First Attempted Solution
Model each fork as a semaphore. Then eachphilosopher must wait (execute a P operation) on boththe left and right forks before eating.
sem fork [5] := ([5] 1)process philosopher (i := 1 to 5)
do true ->Think ( )P(fork [i])P(fork [(i+1) mod 5]Eat ( )V(fork [i])V(fork [(i+1) mod 5]
odend
page 37
This is called a symmetric solution since each task isidentical. Symmetric solutions have many advantages,particularly when it comes to load-balancing.
We can prove that no fork is ever held by twophilosophers since Eat ( ) is the critical section of eachfork. If #Pi is the number of philosophers holding fork ithen we have:
( )Fork i Pi+ =# 1
Since a semaphore is non-negative then #Pi ≤ 1.
However, this system can deadlock under aninterleaving in which all five philosophers pick up theirleft forks together; i.e. all processes execute P(fork [i])before P(fork [(i+1) mod 5]. There are two solutions.One is to make one of the philosophers pick up the rightfork before the left fork (asymmetric solution); the otheris to only allow four philosophers into the room at anyone time.
A Symmetric Solution
sem Room := 4sem fork [5] := ([5] 1)
page 38
process philosopher (i := 1 to 5)do true ->
Think ( )P (Room)P(fork [i])P(fork [(i+1) mod 5]Eat ( )V(fork [i])V(fork [(i+1) mod 5]V (Room)
odend
This solves the deadlock problem.
Theorem Individual starvation cannot occur.
Proof For a process to starve it must be foreverblocked on one of the three semaphores, Room, fork [i]or fork [(i+1) mod 5].
Room semaphore
If the semaphore is a blocked-queue semaphore thenprocess i is blocked only if Room is 0 indefinitely. Thiswould require the other four philosophers to be blockedon their left forks, since if one of them can get two forkshe will finish, put down the forks and signal Room (V(Room) ). So this case will follow from the fork [i]case.
page 39
fork [ i]
If philosopher i is blocked on his left fork, thenphilosopher i-1 must be hold his right fork. Therefore heis either eating or signalling he is finished with his leftfork, and will eventually put down his right fork which isphilosopher i’ s left fork.
fork [(i+1) mod 5]
If philosopher i is block on his right fork, this meansthat philosopher (i+1) has taken his left fork and neverreleased it. Since eating and signalling cannot block,philosopher (i+1) must be waiting for his right fork, andmust all the other philosophers by induction: i+ j, 0≤j≤4.However the Room semaphore invariant only 4philosophers can be in the room, so philosopher i cannotbe blocked on his right fork.
Readers-Writers Problem
The Problem
Two kinds of processes, readers and writers, share adatabase. Readers execute transactions that examine thedatabase, writers execute transactions that examine andupdate the database. Given that the database is initiallyconsistent, then to ensure that the database remainsconsistent, a writer process must have exclusive access to
page 40
the database. Any number of readers may concurrentlyexamine the database.
Obviously, as far as a writer process is concerned,updating the database is a critical section that cannot beinterleaved with any other process.
P (mutexR)nr := nr + 1if nr = 1 -> P (rw) fiV (mutexR)Read_Database ( )P (mutexR)nr := nr - 1if nr = 0 -> V (rw) fiV (mutexR)
odend
page 41
process writer (i:= 1 to N)do true ->
P (rw)Update_Database ( )V (rw)
odend
This is called the readers’ preference solution sinceif some reader process is accessing the database and areader process and a writer process arrive at their entryprotocols then the reader process will always havepreference over the writer process.
This is not a fair solution since this solution alwaysgives readers precedence over writers, a continual streamof readers will block any writer process from updatingthe database.
To make the solution fair we need to use a splitbinary semaphore, that is several semaphores whotogether have the property that their sum is 0 or 1. Wealso need to count the number of suspended readerprocesses and suspended writer processes.
# readers and writerssem e := 1, r := 0, w := 0 # 0 ≤ (e+r+w) ≤ 1
process reader (i:= 1 to M)do true ->
P (e)if nw > 0 ->
sr:= sr + 1; V (e); P (r)finr := nr + 1if sr > 0 ->
sr := sr - 1; V (r)[] sr = 0 -> V(e)fi
Read_Database ( )
P (e)nr := nr - 1if nr = 0 and sw > 0 -> sw := sw - 1; V (w)[] nr >0 or sw = 0 -> V (e)fi
odend
page 43
process writer (i:= 1 to N)do true ->
P (e)if nr > 0 or nw > 0 ->
sw := sw + 1; V (e); P (w)finw := nw + 1V (e)
Update_Database ( )
P (e)nw := nw - 1if sr > 0 -> sr := sr -1; V (r)[] sw > 0 -> sw := sw - 1; V (w)[] sr = 0 and sw = 0 -> V(e)fi
odend
page 44
Monitors
The main disadvantage with semaphores is that theyare a low level programming construct. In a large project,with many programmers, if one programmer forgets to doV( ) operation on a semaphore after a critical section,then the whole system can deadlock.
What is required is a higher level construct thatgroups the responsibility for correctness into a fewmodules.
Monitors are such a construct. These are anextension of the monolithic found in operating systems.They encapsulate a set of procedures, and the data theyoperate on, into single modules, called monitors, andguarantee that only one process can execute a procedurein the monitor at any given time (mutual exclusion). Ofcourse different processes can execute procedures fromdifferent monitors at the same time.
Condition Variables
Synchronisation is achieved by using conditionvariables. These are data structures that have 3operations defined for them.
page 45
wait (C) The process that called the monitorcontaining this operation issuspended in a FIFO queueassociated with C. Mutual exclusionon the monitor is released.
signal (C) If the queue associated with C isnon-empty, then wake the process atthe head of the queue.
non-empty (C) Returns true if the queue associatedwith C is non-empty.
If a monitor guarantees mutual exclusion, and aprocess uses the signal operation and awakens anotherprocess suspended in the monitor, is there not twoprocesses in the same monitor at the same time? Yes.
To solve this problem, several signallingmechanisms can be implemented, the simplest of whichis the signal and continue mechanism. Under these rulesthe procedure in the monitor that signals a conditionvariable is allowed to continue to completion, so thesignal operation should be at the end of the procedure.The process which was suspended on the conditionvariable, but is now awoken, is scheduled for immediateresumption on the exiting of the procedure whichsignalled the condition variable.
Firstly we need blocked-queue semaphores (SR isOK) and secondly we need to implement the signal andcontinue mechanism. We do this with a variablec_count, one semaphore, s, to ensure mutual exclusionand another semaphore, c_semaphore, to act as thecondition variable. Then we translate _wait (s) to:
Using monitors yields a nice solution, since withsemaphores you cannot test two semaphoressimultaneously. The monitor solution maintains an arrayfork which counts the number of free forks available toeach philosopher.
Proof: Let #E be the number of philosophers who areeating, and have therefore taken both forks. Then thefollowing invariants are true from the program.
[ ]( ) [ ]Non empty i i− ⇒ <ok_to_eat fork 2
[ ] ( )fork Eii
= −=∑ 10 2
1
5
#
Deadlock implies #E=0 and all philosophers areenqueued on ok_to_eat. If they are all enqueued then
page 51
the first equation implies [ ]fork i∑ ≤ 5 and if no
philosopher is eating, then the second equation implies[ ]fork i∑ = 10. The contradiction implies that the
solution does not deadlock.
However, individual starvation can occur. How?What is the solution?
The Sleeping Barber Problem
This is a generalisation of rendezvoussynchronisation in client/server architectures.
Problem A small barber shop has two doors, anentrance and an exit. Inside is a barber who spends all hislife serving customers, one at a time. When there arenone in the shop, he sleeps in his chair. If a customerarrives and finds the barber asleep he awakens thebarber, sits in the customer’s chair and sleeps while hishair is being cut. If a customer arrives and the barber isbusy cutting hair, the customer goes asleep in one of thetwo waiting chairs. When the barber finishes cutting acustomer’s hair, he awakens the customer and holds theexit door open for him. If there are any waitingcustomers, he awakens one and waits for the customer tosit in the customer chair, otherwise he goes to sleep.
page 52
The barber and customers are interacting processes,and the barber shop is the monitor in which they react.
open := open + 1_signal (door_open)do open > 0 -> _wait (customer_left) od
_proc_end
_monitor_end
In general, when many different processes have torendezvous, the solution technique is to use a monitor tocreate an “environment” in which the processes canrendezvous.