#include <sys/sem.h> int semop(int semid, struct sembuf *sops, size_t nsops);
The semop() function performs operations on semaphores in a semaphore set. These operations are supplied in a user-defined array of operations.
Each semaphore operation specified by the sops array is performed on the semaphore set specified by semid. The entire array of operations is performed atomically; no other thread will operate on the semaphore set until all of the operations are done or it is determined that they cannot be done. If the entire set of operations cannot be performed, none of the operations are done, and the thread waits until all of the operations can be done.
The members of the sembuf structure are as follows:
unsigned short sem_num | The number of the semaphore in the semaphore set. |
short sem_op | The operation to perform on the semaphore. |
short sem_flg | The operation flags. |
The semop() function changes each semaphore specified by sem_num according to the value of sem_op as follows:
If IPC_NOWAIT is set in sem_flg and the operation cannot be completed, semop() returns with a return value of -1 and errno set to EAGAIN instead of causing the thread to wait.
If SEM_UNDO is set in sem_flg, semop() causes IPC to reverse the effect of this semaphore operation when the thread ends, effectively releasing the resources or request for resources controlled by the semaphore. This value is known as the semaphore adjustment value.
If the thread waits for the semaphore value to change, the calling thread suspends processing until one of the following occurs:
The system maintains status information about a semaphore set which can be retrieved with the semctl() function. When a semaphore operation is successfully completed, the system sets the members of the semid_ds structure as follows:
Authorization Required for semop()
Object Referred to | Authority Required | errno |
---|---|---|
Semaphore, sem_op is negative | Write | EACCES |
Semaphore, sem_op is positive | Write | EACCES |
Semaphore, sem_op is zero | Read | EACCES |
0 | semop() was successful. |
-1 | semop() was not successful. The errno variable is set to indicate the error. |
If semop() 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 sem_op value is negative or positive and the calling thread does not have write permisstion to the semaphore set.
The sem_op value is zero and the calling thread does not have read permisstion to the semaphore set.
Operation would have caused the process to be suspended.
The operation would result in the calling thread waiting and the IPC_NOWAIT flag is set in the sem_flg member.
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.
Object is too large.
The size of the object would exceed the system allowed maximum size.
The sem_num parameter is less than zero or greater than or equal to the number of semaphores in the set associated with semid.
ID has been removed.
The semaphore identifier semid has been removed from the system.
Interrupted function call.
The semop() function was interrupted by a signal.
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.
The semid parameter is not a valid semaphore identifier.
No space available.
The requested operations required additional space on the device and there is no space left. This could also be caused by exceeding the user profile storage limit when creating or transferring ownership of an object.
Insufficient space remains to hold the intended file, directory, or link.
The limit on the number of individual threads requesting a SEM_UNDO would be exceeded.
A range error occurred.
The value of an argument is too small, or a result too large.
An operation would cause a semval to overflow the system-imposed limit, or an operation would cause a semaphore adjustment value to overflow the system-imposed limit.
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 |