Guardian Programmer's Guide

Table Of Contents
Synchronizing Processes
Guardian Programmer’s Guide 421922-014
26 - 6
Opening a Binary Semaphore
In this example, a binary semaphore is created with security level 0, which permits all
processes in the same CPU to access the semaphore.
Opening a Binary Semaphore
All processes that will access a binary semaphore must first open the binary
semaphore. The BINSEM_CREATE_ call creates and opens a binary semaphore; all
other processes besides the process that calls BINSEM_CREATE_ must call
BINSEM_OPEN_. A process generally calls BINSEM_OPEN_ at the beginning of its
execution. To synchronize access to several shared resources, a process can have
multiple binary semaphores open at the same time. (The current binary semaphore
limits are 8,000 per process and 64,000 per processor.)
When calling BINSEM_OPEN, you must specify the following input parameters:
The process handle of either the process that created the semaphore or a process
that previously opened the semaphore.
The ID of the semaphore being opened (the ID returned by the above process).
The process that created or previously opened the semaphore must communicate
these values to all other processes that will open the semaphore.
BINSEM_OPEN_ returns an ID value that identifies the binary semaphore locally in
this process; it is different from the semaphore ID passed as input to BINSEM_OPEN_.
The local ID is used to designate the binary semaphore in subsequent calls within the
same process; it can also be conveyed to other processes for use as input to
BINSEM_OPEN_ or BINSEM_GETSTATS_, along with the process handle of the
process to which it belongs.
Locking a Binary Semaphore
Locking a binary semaphore enables a process to exercise exclusive access to a
shared resource; only one process at a time can lock a binary semaphore. To lock a
binary semaphore, call the BINSEM_LOCK_ procedure. A process typically calls
BINSEM_LOCK_ just before accessing the shared resource.
If the binary semaphore is unlocked when BINSEM_LOCK_ is called, the calling
process is granted a lock on the semaphore and can access the shared resource
safely.
If another process has the binary semaphore locked when BINSEM_LOCK_ is called,
execution of the calling process is suspended and the process is placed in a wait
group. The process remains in this state until either:
The process currently holding the lock unlocks the binary semaphore, and the
suspended process is selected from the wait group and granted the lock.
or
The value specified for the timeout parameter in the BINSEM_LOCK_ call is
reached. The timeout parameter provides a way to ensure that a process does