Guardian Programmer's Guide

Table Of Contents
Synchronizing Processes
Guardian Programmer’s Guide 421922-014
26 - 10
Binary Semaphore Example
Binary Semaphore Example
Following is a simple TAL example illustrating the use of the binary semaphore
procedure calls. This example can be used as a template for creating other programs
that use binary semaphores. The example assumes that two processes share a
segment containing a shared structure. One process executes the PRIMARY
procedure and the other process executes the SECONDARY procedure. The address
of the shared structure is passed to these procedures. The program consists of the
following components:
The shared memory segment. This segment contains both the shared structure
and the variables comprising the resource to which access is controlled.
The shared structure. This structure is used to pass the process handle of the
main process and the binary semaphore ID from the primary process to the
secondary process.
The external declarations that provide access to the binary semaphore procedures
and other Guardian procedures used by the program.
The USERESOURCE procedure. This procedure accesses the shared resource.
It is called by the main process and the secondary process.
The PRIMARY procedure. This procedure is executed by the main process. It
creates the binary semaphore, creates the secondary process, and uses the
shared resource.
The SECONDARY procedure. This procedure accesses the shared resource.
Shared Structure
The shared structure is used to pass the main process handle and the semaphore ID
from the main process, which creates the binary semaphore, to the secondary process,
which opens the binary semaphore. The structure is as follows:
STRUCT SHARED_TEMPLATE (*);
BEGIN
INT MAINPROCESSHANDLE[0:9];
INT(32) MAINSEMID;
END;
External Declarations
The following SOURCE directives provide access to the binary semaphore procedures
and other Guardian procedures used by the program:
?SOURCE $SYSTEM.SYSTEM.EXTDECS0(BINSEM_CREATE_, BINSEM_OPEN_)
?SOURCE $SYSTEM.SYSTEM.EXTDECS0(BINSEM_CLOSE_, BINSEM_LOCK_)
?SOURCE.$SYSTEM.SYSTEM.EXTDECS0(BINSEM_UNLOCK_, DEBUG)
?SOURCE.$SYSTEM.SYSTEM.EXTDECS0(PROCESS_GETINFO_)
?SOURCE.$SYSTEM.SYSTEM.EXTDECS0(PROCESSHANDLE_NULLIT_)