Guardian Programmer's Guide

Table Of Contents
Synchronizing Processes
Guardian Programmer’s Guide 421922-014
26 - 5
Using the Binary Semaphore Procedure Calls
Using the Binary Semaphore Procedure Calls
This subsection describes the steps involved in using binary semaphores. Following is
a summary of these steps:
1. First, the binary semaphore must be created (call BINSEM_CREATE_).
2. After the binary semaphore is created, all processes that will access the binary
semaphore must open it (call BINSEM_OPEN_). The process that creates the
binary semaphore does not need to call BINSEM_OPEN_, because
BINSEM_CREATE_ also opens the binary semaphore.
3. Before executing a critical section of code (that is, before accessing a shared
resource), a process locks the binary semaphore (call BINSEM_LOCK_). The
process that creates the binary semaphore does not need to call BINSEM_LOCK_,
the first time it attempts to lock the semaphore because BINSEM_CREATE_ also
locks the binary semaphore.
4. After finishing the critical section, a process unlocks the binary semaphore so that
another process can lock it and safely access the resource (call
BINSEM_UNLOCK_).
5. Once a process is finished using a binary semaphore, it should close the binary
semaphore to free any system resources used by the binary semaphore (call
BINSEM_CLOSE_).
An additional procedure, BINSEM_FORCELOCK_, is provided to enable a process to
take a lock away from the process currently holding the lock. This call should be used
only if absolutely necessary.
Creating a Binary Semaphore
The first step in synchronizing a group of processes is to create a binary semaphore.
In general, the process that creates the resource to be shared also creates the binary
semaphore, although this is not a requirement.
To create a binary semaphore, call the BINSEM_CREATE_ procedure. This procedure
creates, opens, and locks a binary semaphore, and it returns a semaphore ID that is
used to refer to the semaphore in subsequent calls. The process that creates the
binary semaphore must make this semaphore ID, along with its own process handle,
available to other processes that will access the semaphore. This can be done by
placing the values in a shared data area, as illustrated in the example at the end of this
section.
In the BINSEM_CREATE_ call, you must also specify a security level for the
semaphore. The security level determines which other processes are permitted to
open the semaphore. For example:
SECURITY := 0;
ERROR := BINSEM_CREATE (SEMID, SECURITY);