Trains and Semaphores
8 July 2001
WORK IN PROGRESS!
are simple variables which take on a binary state (0 or a 1), and are used to
identify that an event has occurred, or to pass binary information. Semaphores
are positive integer values which can take on any range of values, but can also
be binary values (for mutual exclusion applications). An example of using a semaphore
is when a process uses a resource, and sets a semaphore flag to indicate that
it is currently accessing the resource. Any device, which then accesses the resource
while the semaphore flag was set, will know that the re-source is still being
used, and must thus wait until the flag is unset. Semaphores were initially developed
by Dijkstra and are implemented in IPC. Two common uses of semaphores are:
Memory. This allows processes to share a common area of memory.
File access. This allows processes to share access to files.
semaphore could simply be a memory location in which processes go to and test
the value. If the semaphore is set to a given value, a process may have to sleep
until the value is changed to a value which allows orderly access to the resource.
Semaphores can also be used to define critical code regions. These are parts of
a process which must wait for code in another process to complete. A practical
example of this is when we imagine two trains (two processes) approaching a region
of single-track rail. For safety considerations there must only be one track on
the single-track line, at a time. The trains must then have some way of signaling
to the other train that they are now on the track. A signal (the semaphore) could
be setup so that it is set either a red or a green light at the entrance of the
track. When a train enters the track the signal will change to red, and stop any
other trains from entering the track. When it has left the track the signal will
be set to green, and the other train can enter the track. In software terms, the
trains could be processes which require exclusive access to a resource. When one
of the processes gets access to it, all other processes must wait for the resource
to be released.
Chapter 6, Mastering Computing, W.Buchanan, Palgrave.
on this essay
If you've got any comments on this essay (no matter if you
agree or disagree with it), please send them to me using the form below.
Note your comments may be published
to a comments pages, but none of your details will be added to the comment, just
the date that it was sent. Thank you.