#include <sys/sem.h> int semctl(int semid, int semnum, int cmd, ...);
The semctl() function allows the caller to control the semaphore set specified by the semid parameter.
A semaphore set is controlled by setting the cmd parameter to one of the following values:
The IPC_RMID command can be run only by a thread with appropriate privileges or one that has an effective user ID equal to the user ID of the owner or the user ID of the creator of the semaphore set.
The IPC_SET command can be run only by a thread with appropriate privileges or one that has an effective user ID equal to the user ID of the owner or the user ID of the creator of the semaphore set.
The members of the semid_ds structure are as follows:
struct ipc_perm sem_perm | The members of the ipc_perm structure
are as follows:
|
||||||||||
unsigned short sem_nsems | The number of semaphores in the set. | ||||||||||
time_t sem_otime | The time the last thread operated on the semaphore set using semop(). | ||||||||||
time_t sem_ctime | The time the last thread changed the semaphore set using semctl(). |
Authorization Required for semctl()
Object Referred to | Authority Required | errno |
---|---|---|
Semaphore, get the value of (cmd = GETVAL) | Read | EACCES |
Semaphore, set the value of (cmd = SETVAL) | Write | EACCES |
Semaphore, get last process to operate on (cmd = GETPID) | Read | EACCES |
Semaphore, get number of threads waiting for value to increase (cmd = GETNCNT) | Read | EACCES |
Semaphore, get number of threads waiting for value to reach zero (cmd = GETZCNT) | Read | EACCES |
Semaphore set, get value of each semaphore (cmd = GETALL) | Read | EACCES |
Semaphore set, set value of each semaphore (cmd = SETALL) | Write | EACCES |
Semaphore set, retrieve state information (cmd = IPC_STAT) | Read | EACCES |
Semaphore set, set state information (cmd = IPC_SET) | See Note | EPERM |
Semaphore set, remove (cmd = IPC_RMID) | See Note | EPERM |
Note: To set semaphore set information or to remove a semaphore set, the thread must be the owner or creator of the semaphore set, or have appropriate privileges.
value | semctl() was successful.
Depending on the control operation specified in cmd,
semctl() returns the following values:
|
||||||||||
-1 | semctl() was not successful. The errno variable is set to indicate the error. |
If semctl() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Permission denied.
An attempt was made to access an object in a way forbidden by its object access permissions.
The thread does not have access to the specified file, directory, component, or path.
The cmd parameter is IPC_STAT, GETVAL, GETPID, GETNCNT, GETZCNT, or GETALL and the calling thread does not have read permission to the semaphore set.
The cmd parameter is SETVAL, or SETALL and the calling thread does not have write permission to the semaphore set.
A damaged object was encountered.
A referenced object is damaged. The object cannot be used.
The semaphore set has been damaged by a previous semaphore operation.
The address used for an argument is not correct.
In attempting to use an argument in a call, the system detected an address that is not valid.
While attempting to access a parameter passed to this function, the system detected an address that is not valid.
The value specified for the argument is not correct.
A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.
An argument value is not valid, out of range, or NULL.
One of the following has occurred:
Operation not permitted.
You must have appropriate privileges or be the owner of the object or other resource to do the requested operation.
The cmd parameter is IPC_RMID or IPC_SET and both of the following are true:
A range error occurred.
The value of an argument is too small, or a result too large.
The cmd parameter is SETVAL, and the value to which semval is to be set is greater than the system-imposed maximum.
Unknown system state.
The operation failed because of an unknown system state. See any messages in the job log and correct any errors that are indicated, then retry the operation.
None.
See Code disclaimer information for information pertaining to code examples.
For an example of using this function, see Using Semaphores and Shared Memory in Examples: APIs.
Top | UNIX-Type APIs | APIs by category |