[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 2

semop Subroutine

Purpose

Performs semaphore operations.

Library

Standard C Library (libc.a)

Syntax

#include <sys/sem.h>


int semop (SemaphoreIDSemaphoreOperations, NumberOfSemaphoreOperations)
int  SemaphoreID;
struct sembuf * SemaphoreOperations;
size_t  NumberOfSemaphoreOperations;

Description

The semop subroutine performs operations on the set of semaphores associated with the semaphore identifier specified by the SemaphoreID parameter. 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:

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. The sem_op field specifies one of three semaphore operations.

  1. If the sem_op field is a negative integer and the calling process has permission to alter, one of the following conditions occurs:
  2. If the sem_op field is a positive integer and the calling process has alter permission, the value of the sem_op field is added to the semval variable. In addition, if the SEM_UNDO flag is set in the sem_flg field, the value of the sem_op field is subtracted from the calling process's semadj value for the specified semaphore.
  3. If the value of the sem_op field is 0 and the calling process has read permission, one of the following occurs:

The following limits apply to semaphores:

Parameters

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.

Return Values

Upon successful completion, the semop subroutine returns 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 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.

Error Codes

The semop 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.

EINVAL The SemaphoreID parameter is not a valid semaphore identifier.
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.

Related Information

The exec subroutine, exit subroutine, fork subroutine, semctl (semctl Subroutine) subroutine, semget (semget Subroutine) subroutine, sigaction (sigaction, sigvec, or signal Subroutine) subroutine.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]