creatsem(2) — SYSTEM CALLS
NAME
creatsem − create an instance of a binary semaphore
SYNOPSIS
cc [flag . . .] file . . . -lx
int creatsem(char ∗sem_name, int mode);
DESCRIPTION
creatsem defines a binary semaphore named by sem_name to be used by waitsem and sigsem to manage mutually exclusive access to a resource, shared variable, or critical section of a program. creatsem returns a unique semaphore number, sem_num, which may then be used as the parameter in waitsem and sigsem calls. Semaphores are special files of 0 length. The filename space is used to provide unique identifiers for semaphores. mode sets the accessibility of the semaphore using the same format as file access bits. Access to a semaphore is granted only on the basis of the read access bit; the write and execute bits are ignored.
A semaphore can be operated on only by a synchronizing primitive, such as waitsem or sigsem, by creatsem which initializes it to some value, or by opensem which opens the semaphore for use by a process. Synchronizing primitives are guaranteed to be executed without interruption once started. These primitives are used by associating a semaphore with each resource (including critical code sections) to be protected.
The process controlling the semaphore should issue:
sem_num = creatsem("semaphore", mode);
to create, initialize, and open the semaphore for that process. All other processes using the semaphore should issue:
sem_num = opensem("semaphore");
to access the semaphore’s identification value. Note that a process cannot open and use a semaphore that has not been initialized by a call to creatsem, nor should a process open a semaphore more than once in one period of execution. Both the creating and opening processes use waitsem and sigsem to use the semaphore sem_num.
DIAGNOSTICS
creatsem returns the value −1 if an error occurs. If the semaphore named by sem_name is already open for use by other processes, errno is set to EEXIST. If the file specified exists but is not a semaphore type, errno is set to ENOTNAM. If the semaphore has not been initialized by a call to creatsem, errno is set to EINVAL.
SEE ALSO
opensem(2), sigsem(2), waitsem(2)
NOTES
After a creatsem, you must do a waitsem to gain control of a given resource.
— Application Compatibility Package