sem_mksem(3) — Subroutines
Digital
NAME
sem_mksem − Create a new binary semaphore set (P1003.4/D11)
SYNOPSIS
#include <sys/stat.h>
#include <sys/types.h>
#include <binsem.h>
int sem_mksem (
const char ∗name,
size_t nsems,
int flags);
PARAMETERS
∗name A pointer to the pathname naming the binary semaphore object.
nsems The number of semaphores to be created.
flags A set of flags that control whether the binary semaphores created are locked or not. The flags are specified in the <binsem.h> header file.
If SEM_LOCKED is specified, each binary semaphore in the set is initialized to the locked state. Otherwise, each new binary semaphore is initialized to the unlocked (SEM_UNLOCKED) state.
DESCRIPTION
The sem_mksem function creates a new binary semaphore set of nsem binary semaphores. A binary semaphore set is used to limit access to a critical resource. When a process requires access to the resource without interference from other processes, it locks the associated semaphore. Then, other processes attempting to access the resource must wait for the semaphore to become unlocked.
Once the binary semaphore set has been created, the sem_open function establishes a connection between the set of binary semaphores and a binary semaphore set descriptor; the sem_wait and sem_ifwait functions lock the semaphore; and the sem_post function unlocks binary semaphores in the set. Use the sem_close function to deallocate the binary semaphore set descriptor and the sem_destroy function to remove the it. You can use the sem_getnsems function to get the number of binary semaphores in the binary semaphore set.
The name of the binary semaphore set created by a call to the sem_mksem function remains valid until the semaphore set is removed by a call to the sem_destroy function. The name and the state of the binary semaphore set is persistent across a system reboot or system crash. All the binary semaphore sets should be removed by a call to the sem_destroy function prior to calling the sem_mksem function after the system reboot.
RETURN VALUES
On successful completion, the sem_mksem function returns the value 0 and creates the binary semaphore; otherwise, the function returns the value −1 and sets errno to indicate the error.
ERRORS
The sem_mksem function fails under the following conditions:
[EAGAIN] nsems was greater than SEM_NSEMS_MAX.
[EEXIST] The binary semaphore set name already exists.
[EINVAL] Operation not supported for the given name.
[ENAMETOOLONG] The length of the null-terminated string name exceeds SEM_NAME_MAX.
[ENOSPC] There is not enough space for new binary semaphores.
[ENOSYS] The function sem_mksem is not supported by this implementation.
[EPERM] The process lacks the appropriate privilege to create the named binary semaphore set.
RELATED INFORMATION
Functions: fork(2), sem_close(3), sem_destroy(3), sem_open(3)