Performs semaphore operations.
Standard C Library (libc.a)
#include <sys/sem.h>
int semop (SemaphoreID, SemaphoreOperations, NumberOfSemaphoreOperations)
int SemaphoreID;
struct sembuf * SemaphoreOperations;
size_t NumberOfSemaphoreOperations;
#include <sys/sem.h>
int semtimedop (SemaphoreID, SemaphoreOperations,
NumberOfSemaphoreOperations, Timeout)
int SemaphoreID;
struct sembuf * SemaphoreOperations;
size_t NumberOfSemaphoreOperations;
struct timespec * timeout;
The semop and semtimedop subroutines perform operations on the set of semaphores associated with the semaphore identifier specified by the SemaphoreID parameter.
The semtimedop subroutine limits the time the caller will sleep while waiting for the semaphore operation(s) to complete. The timespec structure is defined in the /usr/include/sys/time.h file and includes the following fields:
Item | Description |
---|---|
tv_sec | Seconds on timer |
tv_nsec | Nanoseconds on timer |
If the caller sleeps for the time allotted by the timespec structure before the operation(s) can be completed, the current operation is aborted and the semtimedop subroutine will return an error.
The sembuf structure is defined in the usr/include/sys/sem.h file. Each sembuf structure specified by the SemaphoreOperations parameter includes the following fields:
Item | Description |
---|---|
sem_num | Semaphore number |
sem_op | Semaphore operation |
sem_flg | Operation flags |
Each semaphore operation specified by the sem_op field is performed on the semaphore specified by the SemaphoreID parameter and the sem_num field. Semaphore operations are performed in the order they are received in the sembuf array. The sem_op field specifies one of three semaphore operations.
The following limits apply to semaphores:
Item | Description |
---|---|
SemaphoreID | Specifies the semaphore identifier. |
NumberOfSemaphoreOperations | Specifies the number of structures in the array. |
SemaphoreOperations | Points to an array of structures, each of which specifies a semaphore operation. |
Timeout | Points to a structure specifying an interval of time beyond which the operation should not sleep. |
Upon successful completion, the semop and semtimedop subroutines return a value of 0. Also, the SemaphoreID parameter value for each semaphore that is operated upon is set to the process ID of the calling process.
If the semop or semtimedop subroutine is unsuccessful, a value of -1 is returned and the errno global variable is set to indicate the error. If the SEM_ORDER flag was set in the sem_flg field for the first semaphore operation in the SemaphoreOperations array, the SEM_ERR value is set in the sem_flg field for the unsuccessful operation.
If the SemaphoreID parameter for which the calling process is awaiting action is removed from the system, the errno global variable is set to the EIDRM error code and a value of -1 is returned.
The semop or semtimedop subroutine is unsuccessful if one or more of the following are true for any of the semaphore operations specified by the SemaphoreOperations parameter. If the operations were performed individually, the discussion of the SEM_ORDER flag provides more information about error situations.
Item | Description |
---|---|
EINVAL | The SemaphoreID parameter is not a valid semaphore identifier. |
EINVAL | The number of individual semaphores for which the calling process requests a SEM_UNDO flag would exceed the limit. |
EINVAL | The Timeout parameter specified a tv_sec or tv_nsec value less than 0, or a tv_nsec value greater than 1000 million. |
EFBIG | The sem_num value is less than 0 or it is greater than or equal to the number of semaphores in the set associated with the SemaphoreID parameter. |
E2BIG | The NumberOfSemaphoreOperations parameter is greater than the system-imposed maximum. |
EACCES | The calling process is denied permission for the specified operation. |
EAGAIN | The operation would result in suspension of the calling process, but the IPC_NOWAIT value is set in the sem_flg field. |
ENOSPC | The limit on the number of individual processes requesting a SEM_UNDO flag would be exceeded. |
EINVAL | The number of individual semaphores for which the calling process requests a SEM_UNDO flag would exceed the limit. |
ERANGE | An operation would cause a semval value to overflow the system-imposed limit. |
ERANGE | An operation would cause a semadj value to overflow the system-imposed limit. |
EFAULT | The SemaphoreOperations parameter points outside of the address space of the process. |
EINTR | A signal interrupted the semop subroutine. |
EIDRM | The semaphore identifier SemaphoreID parameter has been removed from the system. |
EFAULT | The Timeout parameter points to an invalid address. |
ETIMEDOUT | The time specified by the Timeout parameter expired before the requested operations could be completed. |