The Graham Utilities for OS/2 - Version 2


[Warpspeed]

[Table of Contents] [Index] [Previous] [Next]


Appendix B - SEMMGR

Summary

SEMMGR is a program which manages multiple system semaphores on your behalf.

Icon

Description

SEMMGR is a detached program with which BE communicates to manage system semaphores. SEMMGR can only be run as a detached task. If you try to run it from the command line SEMMGR will report an error. If SEMMGR is not already loaded any one of the BE semaphore related commands will detach it. SEMMGR can be manually detached from the command line.

To do this, enter:

DETACH SEMMGR
from the command line.

SEMMGR creates, clears, sets, waits on and closes system semaphores. System semaphores are named objects which you may use for signalling or synchronisation between processes. The name always takes the form of:

\SEM\path\name.ext
where the path and extension (.ext) are optional. SEMMGR always uses semaphores of the form:
\SEM\SEMMGR\<name>
where name is the semaphore name which you supply to the BE semaphore function. This has been done to minimise the chance of interfering with other system semaphores.

The traditional use of semaphores is to control access to a resource which may not have more than one user at a time. The resource may not have more than one user because it could be damaged or behave unpredictably. For example, you would not want more than one process to access the same serial port at the same time. A semaphore can be created to represent the resource. Other processes should not access the resource unless they "own" the corresponding semaphore. Owning a semaphore is represented by setting the semaphore. When a semaphore is not owned by any one, it should be set to clear. A process should wait for a semaphore to become clear. It should then set the semaphore and continue processing.

The BE program supplies several sub functions which help you manage semaphores via SEMMGR.

SEMCLEAR NAME
Clears a system semaphore of the name: \SEM\SEMMGR\NAME.
SEMCLOSE NAME
Closes a system semaphore of the name: \SEM\SEMMGR\NAME. A system semaphore can only be closed when it is cleared. This function should be used carefully as another process may be about to set the semaphore. If it has been closed and you attempt to do anything other than create it again SEMMGR will report an error.
SEMKILL
This command unilaterally shuts down the SEMMGR process. This should ONLY be done when there are no semaphores in use. See also SEMLIST.
SEMLIST
The SEMLIST command lists all currently SEMMGR managed system semaphores and their state.
SEMSET NAME
Sets a system semaphore of the name: \SEM\SEMMGR\NAME. This command waits for the semaphore to be cleared before it is set again. See below for further details.
SEMWAIT NAME TIMEOUT
Waits for a system semaphore of the name: \SEM\SEMMGR\NAME to become clear. If it is already clear SEMWAIT returns immediately. If no TIMEOUT value is specified, SEMWAIT will wait indefinitely for the semaphore to clear. If TIMEOUT has been specified it will wait that many milliseconds for the semaphore to clear.

Consider the following situation:

Several processes are all waiting for the same semaphore to clear. The process which owns the semaphore finishes and clears the semaphore. All of the waiting tasks find that the semaphore is clear. They all then set it, thinking that they own it, and they all then proceed to run the same resource critical task simultaneously. There can be some time between when the process finds that the semaphore is clear and when it actually sets it. To overcome this problem, SEMSET waits until semaphore is clear before it sets it and returns control back to BE.

To illustrate the use of semaphore usage, the following files have been provided.

BE-TASKS.CMD
Starts several tasks which all require access to the same critical resource.
RESOURCE.CMD
Sample of a resource critical task.
BETASK01.CMD
CMD file to wait access to RESOURCE.CMD
BETASK01.BE
BE response file for BETASK01.CMD
BETASK02.CMD
CMD file to wait access to RESOURCE.CMD
BETASK02.BE
BE response file for BETASK02.CMD
BETASK03.CMD
CMD file to wait access to RESOURCE.CMD
BETASK03.BE
BE response file for BETASK03.CMD
BETASK04.CMD
CMD file to wait access to RESOURCE.CMD
BETASK04.BE
BE response file for BETASK04.CMD
BETASK05.CMD
CMD file to wait access to RESOURCE.CMD
BETASK05.BE
BE response file for BETASK05.CMD
BETASK06.CMD
CMD file to wait access to RESOURCE.CMD
BETASK06.BE
BE response file for BETASK06.CMD
BETASK07.CMD
CMD file to wait access to RESOURCE.CMD
BETASK07.BE
BE response file for BETASK07.CMD
BETASK08.CMD
CMD file to wait access to RESOURCE.CMD
BETASK08.BE
BE response file for BETASK08.CMD
BETASK09.CMD
CMD file to wait access to RESOURCE.CMD
BETASK09.BE
BE response file for BETASK09.CMD
BETASK10.CMD
CMD file to wait access to RESOURCE.CMD
BETASK10.BE
BE response file for BETASK10.CMD

Example

DETACH SEMMGR
This is how SEMMGR can be manually detached to run as a background process. If SEMMGR has not been detached and BE needs to process semaphore functions, BE will detach it for you.