0% found this document useful (0 votes)
24 views

Lecture 6 Semaphores

Everything on semaphores

Uploaded by

iamtadzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 6 Semaphores

Everything on semaphores

Uploaded by

iamtadzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Process communication and

shared resources

Session 7 Slides
Learning Objectives
By the end of this session, you should be
able to

 Understand the how processes share resources


Semaphores

 In everyday life, semaphores provide a system of signals in order to


enable communication visually, usually with flags, lights, or some other
mechanism.
 In computing, semaphores are conceptually much simpler and more
analogous to the signals used on railway lines to indicate to drivers
whether it is safe to continue, or whether they should wait before
proceeding (into a tunnel for example).
 In software, a semaphore is a data structure that is useful for solving a
variety of synchronisation problems.
 Dijkstra (1968) introduced the idea of semaphores as part of the
operating system in order to synchronise processes with each other and
with hardware.
 Since then semaphores have become a very important mechanism for the
synchronisation of processes generally, not just for operating systems.
 One way of explaining semaphores is to think of them as a ‘permit’.
 Only one process at a time can hold the permit, and a process must get
hold of the permit before it can enter the critical region.
 Once it has finished the critical region, it gives back the permit
Semaphores

 A semaphore is a data structure, consisting of two pieces of data:


an integer value, say sem, and a queue.
 The sem data field has to be assigned an initial value, representing
the availability of some resource.
 Once the semaphore has been created, the only way the value can
be altered is through two operations, semWait and semSignal,
specified as follows:
 semWait: if sem >0 then sem = sem –1, else make the process that
called semWait wait by placing it in the queue.
 semSignal: if there is a process waiting on the queue for this
semaphore, then allow it to resume processing, else sem = sem+1.
Semaphores

 A semaphore is a data structure, consisting of two pieces of data:


an integer value, say sem, and a queue.
 The sem data field has to be assigned an initial value, representing
the availability of some resource.
 Once the semaphore has been created, the only way the value can
be altered is through two operations, semWait and semSignal,
specified as follows:
 semWait: if sem >0 then sem = sem –1,
 else make the process that called semWait wait by placing it in the
queue.
 semSignal: if there is a process waiting on the queue for this
semaphore, then allow it to resume processing, else sem = sem+1.

 Each of the operations semWait and semSignal is implemented as


an atomic action
Semaphores

 When a process is made to wait as result of calling semWait, it


becomes blocked, and gets added to the back of the semaphore’s
queue.
 The process in the queue now waits for an event to occur, i.e. a call
of the semSignal operation by another process using the same
semaphore.
 When semSignal is executed, the process that is at the front of the
queue is taken out of its blocked state and starts running
Semaphores

 semaphore provides a versatile mechanism and can be put to


different uses:
 for mutual exclusive access to a single shared resource, in which
case the semaphore is called a binary semaphore;
 to protect access to multiple instances of a resource (a counting
semaphore);
 to synchronise two processes (a blocking semaphore).
Semaphores

 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

 Using semaphores for process synchronisation


 we mentioned that in certain situations processes need to synchronise
with each other each, and that the synchronisation is governed by
whether a condition has been satisfiedor not.
 Such condition synchronisation can also be implemented using what
are termed blocking semaphores.
 As an example, consider a consumer process that needs to read some
data from a file that a producer process is supposed to write to.
 The point in the program code at which the consumer process needs to
read the input from the file is called the synchronisation point.
 If the consumer process reaches its synchronisation point before data
has been written to the file, it needs to know that it should wait.
 Obviously, if the producer has already written data to the file, then the
consumer need not wait and can pick up the data and continue
processing.
Semaphores

 Using semaphores for process synchronisation


Semaphores

 Using semaphores for process synchronisation


 (a) shows the scenario where the consumer reaches the
synchronisation point before the producer. By calling the semWait
operation, the consumer process is made to wait on the queue of
the semaphore. As soon as the producer reaches the point where it
has written data to the file, it calls the semSignal operation, which
has the effect of taking the consumer process out of the blocked
state.
 (b) shows the scenario where the producer reaches the
synchronisation point before the consumer. This works equally well
with the way the operations semWait and semSignal have been
defined. In this case, the value of the semaphore gets updated to 1
by the producer, with the effect that when the consumer checks the
semaphore with the semWait operation it finds it can simply go
ahead and retrieve data from the file. Since the semaphore’s value
was greater than 0, there was no need for the consumer to wait.

You might also like