Controls semaphore operations.
Standard C Library (libc.a)
#include <sys/sem.h>
int semctl (SemaphoreID, SemaphoreNumber, Command, arg)
OR
int semctl (SemaphoreID, SemaphoreNumber, Command)
int SemaphoreID;
int SemaphoreNumber;
int Command;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
The semctl subroutine performs a variety of semaphore control operations as specified by the Command parameter.
The following limits apply to semaphores:
The following Command parameter values are executed with respect to the semaphore specified by the SemaphoreID and SemaphoreNumber parameters. These operations get and set the values of a sem structure, which is defined in the sys/sem.h file.
The following Command parameter values return and set every semval value in the set of semaphores. These operations get and set the values of a sem structure, which is defined in the sys/sem.h file.
The following Commands parameter values get and set the values of a semid_ds structure, defined in the sys/sem.h file. These operations get and set the values of a sem structure, which is defined in the sys/sem.h file.
Item | Description |
---|---|
sem_perm.uid | User ID of the owner |
sem_perm.gid | Group ID of the owner |
sem_perm.mode | Permission bits only |
sem_perm.cuid | Creator's user ID |
IPC_SET can only be executed by a process that has root user authority or an effective user ID equal to the value of the sem_perm.uid or sem_perm.cuid field in the data structure associated with the SemaphoreID parameter.
Upon successful completion, the value returned depends on the Command parameter as follows:
Command | Return Value |
---|---|
GETVAL | Returns the value of the semval field. |
GETPID | Returns the value of the sempid field. |
GETNCNT | Returns the value of the semncnt field. |
GETZCNT | Returns the value of the semzcnt field. |
All Others | Return a value of 0. |
If the semctl subroutine is unsuccessful, a value of -1 is returned and the global variable errno is set to indicate the error.
The semctl subroutine is unsuccessful if any of the following is true:
Item | Description |
---|---|
EINVAL | The SemaphoreID parameter is not a valid semaphore identifier. |
EINVAL | The SemaphoreNumber parameter is less than 0 or greater than or equal to the sem_nsems value. |
EINVAL | The Command parameter is not a valid command. |
EACCES | The calling process is denied permission for the specified operation. |
ERANGE | The Command parameter is equal to the SETVAL or SETALL value and the value to which semval value is to be set is greater than the system-imposed maximum. |
EPERM | The Command parameter is equal to the IPC_RMID or IPC_SET value and the calling process does not have root user authority or an effective user ID equal to the value of the sem_perm.uid or sem_perm.cuid field in the data structure associated with the SemaphoreID parameter. |
EFAULT | The arg.buf or arg.array parameter points outside of the allocated address space of the process. |
ENOMEM | The system does not have enough memory to complete the subroutine. |