Guardian Programmer's Guide

Table Of Contents
Guardian Programmer’s Guide 421922-014
26 - 1
26
Synchronizing Processes
One or more processes executing concurrently on a HP system might need to share a
particular resource. Most commonly, the shared resource is an area of memory, such
as an extended data segment. This sharing can result in conflicts and possible errors.
Consider, for example, two processes, A and B, running concurrently in the same CPU,
both of which are attempting to increment a variable in a shared memory area.
Process A fetches the variable, adds 1 to it, and returns it to memory. Process B then
fetches the variable, adds 1 to it, returns it to memory, and proceeds under the
assumption that the current value of the variable is 2 greater than the original value.
But suppose the processes are synchronized such that process B fetches the variable
just before A returns it to memory. The variable would not have the correct value.
One solution would be to simply run the processes sequentially to ensure that there is
no overlap in their execution and, consequently, no conflicts in accessing the shared
resource. However, this solution would result in very inefficient processing that would
not take advantage of the HP system’s ability to run multiple processes concurrently.
Clearly, a solution is needed that allows concurrent processes to access a shared
resource without conflicts while continuing to execute in parallel.
Binary semaphores provide such a solution. Binary semaphores are a tool to
synchronize processes. They provide a way for a process to hold exclusive access to
a resource while accessing the resource. Using binary semaphores, you can
synchronize processes so that only one process at a time can use a shared resource.
While a process is accessing the resource, other processes can execute concurrently
until they need to use the resource. They then enter a wait state until the resource
becomes available. When the original process is through with the resource, it releases
its “hold” on the resource, and another process in the waiting group acquires exclusive
access to the resource and resumes execution.
Note. Binary semaphores enable you to synchronize processes running in the same CPU; to
synchronize processes in different CPUs, you must use other techniques. However, when the
issue is serializing access to variables, as in the above example, the variables must be in
shared memory, which implies that the sharers are on the same CPU.