Guardian Programmer's Guide

Table Of Contents
Synchronizing Processes
Guardian Programmer’s Guide 421922-014
26 - 11
Procedure USERESOURCE
Procedure USERESOURCE
Procedure USERESOURCE locks the binary semaphore, uses the shared resource,
and unlocks the binary semaphore. The procedure is called by the primary process
and the secondary process. Procedure USERESOURCE is as follows:
PROC USERESOURCE (SEMID);
INT(32) SEMID;
BEGIN
INT ERROR;
ERROR := BINSEM_LOCK_ (SEMID, -1D);
IF (ERROR <> 0) THEN CALL DEBUG;
... Use the shared resource...
ERROR := BINSEM_UNLOCK_ (SEMID);
IF (ERROR <> 0) THEN CALL DEBUG;
END;
Procedure PRIMARY
Procedure PRIMARY is executed by the main process. That process allocates a
memory segment to contain both the shared structure and the shared resource. It
creates a pointer to the structure and passes that pointer to PRIMARY. This procedure
creates the binary semaphore and makes the main process handle and semaphore ID
available to other processes by placing them in the shared structure. The PRIMARY
procedure then creates the secondary process, uses the shared resource, and closes
the binary semaphore. The PRIMARY procedure is as follows:
PROC PRIMARY (SHARED);
INT .EXT SHARED (SHARED_TEMPLATE);
BEGIN
INT ERROR;
ERROR := PROCESSHANDLE_NULLIT_ (SHARED.MAINPROCESSHANDLE);
IF (ERROR <> 0) THEN CALL DEBUG;
ERROR := PROCESS_GETINFO_ (SHARED.MAINPROCESSHANDLE);
IF (ERROR <> 0) THEN CALL DEBUG;
ERROR := BINSEM_CREATE_ (SHARED.MAINSEMID, 2);
IF (ERROR <> 0) THEN CALL DEBUG;
...Create secondary process ...
CALL USERESOURCE (SHARED.MAINSEMID);
ERROR := BINSEM_CLOSE_ (SHARED.MAINSEMID);
IF (ERROR <> 0) THEN CALL DEBUG;
END