Lecture 6 Semaphores
Lecture 6 Semaphores
shared resources
Session 7 Slides
Learning Objectives
By the end of this session, you should be
able to
binary semaphore
If we have several processes that are sharing a resource that should
only be accessed under mutual exclusion, they would be made to
share one semaphore, say sem.
The value of semaphore sem should be initialised to 1.
Each process typically executes code that consists of a combination
of critical and noncritical regions.
Before entering the critical region, the process uses the semWait
operation as the entry protocol and the semSignal operation as the
exit protocol
Semaphores
binary semaphore
Semaphores
binary semaphore
Semaphores
binary semaphore
Here the semaphore is represented as a divided box where the top
half displays the current integer value of the semaphore, and the
bottom half displays the queue part of the semaphore, listing the
IDs of the processes currently in this queue.
The semaphore s is initialised with the value 1, and initially no
processes are waiting on this semaphore’s queue.
All three processes start off executing their non-critical region
Semaphores
binary semaphore
Process A is the first to execute the entry protocol, semWait, for the
semaphore.
At that point the value of the semaphore is 1, so process A is able
to progress to its critical region having set the value of the
semaphore to 0.
Now process B comes to the point in its execution where it needs to
enter its critical region.
Process B executes the entry protocol, finds that the value is 0, and
is made to wait.
Now process C executes the entry protocol, and like B, it finds that
it is not allowed to enter the critical region but is made to wait for
the semaphore instead
Semaphores
binary semaphore
When process A has finished with its critical region, it executes the
exit protocol, semSignal.
This has the effect of signalling to process B (which is the process at
the front of the queue) that it can come out of the blocked state,
and hence enter its critical region.
Once process B has finished its critical region and executed
semSignal, process C is allowed to proceed with its critical region.
When process C completes its critical region, it executes the exit
protocol and, since there are now no processes waiting for the
semaphore, the value of the semaphore is increased to 1.
Semaphores
Counting semaphores
As already mentioned, semaphores can also be used where there are
multiple resources of the same type, all equally acceptable to a process
requiring one instance of the resource.
In that case the semaphore would initially be set to the number of resources
available.
This could be useful, for example, in the scenario where we have two
printers available for processes to use. This means that up to two processes
are allowed to use a printer simultaneously, but when a third process comes
along it will have to wait.
To manage the access to these printers, we would initialise a semaphore
with the value of the available number of resources, that is 2.
When a process requests an instance of the resource, the process is required
to call the semWait operation, and when it finishes using the instance, it calls
semSignal.
So if two processes attempt to use the printer, each would call the semWait
operation, and the semaphore’s value would decrease from 2 to 1 to 0. If, at
that point, a third process requests an instance of the resource, that process
Semaphores
Counting semaphores
There are four processes, and two instances of the resource.
At step t1, the semaphore is initialised to 2 and there are no
processes waiting.
Complete the diagram by indicating for each step what the value of
the semaphore is, and which, if any, processes are waiting for the
semaphore.
Semaphores
Counting semaphores
Semaphores
Counting semaphores
Semaphores