Guardian Programmer's Guide

Table Of Contents
Synchronizing Processes
Guardian Programmer’s Guide 421922-014
26 - 2
How Binary Semaphores Work
How Binary Semaphores Work
Using binary semaphores, a programmer can maximize parallelism (the degree to
which processes are able to execute concurrently) among concurrent processes while
avoiding conflicts over shared resources. A binary semaphore consists of a global
entity called a lock and an associated group of waiting processes called a wait group.
Figure 26-1 illustrates the binary semaphore concept.
In the above example, the executing process follows these steps:
1. When the executing process reaches a critical section (a sequence of code that
accesses a shared resource), it requests a lock on the semaphore.
2. If the semaphore is unlocked, the process locks it and executes the critical code. If
the semaphore is locked by another process, the process requesting the lock is
placed in the wait group.
3. When the process holding the lock finishes executing its critical section, it
relinquishes the lock on the semaphore. The effect of relinquishing the lock is to
check the wait group and do one of the following:
If processes are waiting for the lock, one is removed from the group and made
available for execution; that process takes ownership of the lock.
If no processes are waiting, the semaphore is unlocked and the next process
to enter a critical section can lock the semaphore and access the shared
resource.
Table 26-1 lists four processes, indicated by A, B, C, and D, using binary semaphores
to synchronize their access to a shared memory area. The table shows the state of
each process at arbitrary points in time represented by t0 through t5.
Figure 26-1. Binary Semaphore
VST130.VSD