Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

exec(2)

exit(2)

fork(2)

semctl(2)

semget(2)

semid_ds(4)

semop(2)  —  System Calls

OSF

NAME

semop − Performs semaphore operations

SYNOPSIS

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semop(
int semid,
struct sembuf ∗sops,
u_int nsops);

PARAMETERS

semidSpecifies the ID of the semaphore set. 

sopsPoints to the user-defined array of sembuf structures that contain the semaphore operations. 

nsopsThe number of sembuf structures in the array. 

DESCRIPTION

The semop() function performs operations on the semaphores in the specified semaphore set. The semaphore operations are defined in the sops array. The sops array contains nsops elements, each of which is represented by a sembuf structure. 

The sembuf structure (from sys/sem.h) is shown here: struct sembuf {
 u_shortsem_num;
 shortsem_op;
 shortsem_flg; };

The fields in the sembuf structure are defined as follows:

sem_numSpecifies an individual semaphore within the semaphore set. 

sem_opSpecifies the operation to perform on the sempahore. 

sem_flgSpecifies various flags for the operations. The possible values are:

SEM_UNDO
Instructs the kernel to adjust the process’s adjust-on-exit value for a modified semaphore. When the process exits, the kernel uses this value to restore the semaphore to the value it had before any modifications by the process. This flag is used to prevent semaphore locking by a process that no longer exists.

IPC_NOWAIT
Instructs the kernel to return an error condition if a requested operation would cause the process to sleep. If the kernel returns an error condition, none of the requested semaphore operations are performed.

The sem_op operation is specified as a negative integer, a positive integer, or 0 (zero). The effects of these three values are described below. 

If sem_op is a negative integer and the calling process has modify permission, the semop() function does one of the following:

       •If the semaphore’s current value (in semval) is equal to or greater than the absolute value of sem_op, the absolute value of sem_op is subtracted from semval.  If SEM_UNDO is set, the absolute value of sem_op is added to the calling process’ adjust-on-exit value for the semaphore. 

       •If semval is less than the absolute value of sem_op and IPC_NOWAIT is set, semop() returns immediately with an [EAGAIN] error. 

       •If semval is less than the absolute value of sem_op and IPC_NOWAIT is not set, semop() increments the semaphore’s semncnt value and suspends the calling process. 

If the process is suspended, it sleeps until one of the following occurs:

       •The semval value becomes equal to or greater than the absolute value of sem_op.  In this case, the semaphore’s semncnt value is decremented; the absolute value of sem_op is subtracted from semval; and, if SEM_UNDO is set, the absolute value of sem_op is added to the calling process’s adjust-on-exit value for the semaphore. 

       •The semaphore set (specified by semid) is removed from the system. In this case, errno is set equal to [EIDRM] and a value of -1 is returned to the calling process. 

       •The calling process catches a signal. In this case, the semaphore’s semncnt value is decremented, and the calling process resumes execution as directed by the signal() function. 

If sem_op is a positive integer and the calling process has modify permission, semop() adds the sem_op value to the semaphaore’s current semval value. If SEM_UNDO is set, the sem_op value is subtracted from the calling process’s adjust-on-exit value for the semaphore. 

If sem_op is 0 (zero) and the calling process has read permission, semop() does one of the following:

       •If semval is 0, semop() returns immediately. 

       •If semval is not equal to 0 and IPC_NOWAIT is set, semop() returns immediately. 

       •If semval is not equal to 0 and IPC_NOWAIT is not set, semop() increments the semaphore’s semzcnt value and suspends the calling process. 

If the process is suspended, it sleeps until one of the following occurs:

       •The semval value becomes 0 (zero). In this case, the semaphore’s semncnt value is decremented. 

       •The semaphore set (specified by semid) is removed from the system. In this case, errno is set equal to [EIDRM] and a value of -1 is returned to the calling process. 

       •The calling process catches a signal. In this case, the semaphore’s semncnt value is decremented, and the calling process resumes execution as directed by the signal() function. 

NOTES

Semaphore operations are performed atomically; that is, either all of the requested operations are performed, or none are. If the kernel goes to sleep while doing the operations, it restores all of the semaphores in the set to their previous values, at the start of the semop() function. 

RETURN VALUES

Upon successful completion, the semop() function returns a value of 0 (zero) and the sempid value for each semaphore that is operated upon is set to the process ID of the calling process. 

If the semop() function fails, a value of -1 is returned and errno is set to indicate the error. 

ERRORS

If the semop() function fails, errno may be set to one of the following values:

[EINVAL]The semid parameter is not a valid semaphore ID, or the number of semaphores for which SEM_UNDO is requested exceeds the system-defined limit. 

[EFBIG]The sem_num parameter is less than 0 (zero) or greater than or equal to the number of semaphores in semid. 

[E2BIG]The nsops parameter is greater than the system-defined maximum. 

[EACCES]The calling process does not have the required permission. 

[EAGAIN]Both sem_flg and IPC_NOWAIT are true, but the requested operation has caused the calling process to be suspended. 

[ENOSPC]The system-defined limit on the number of processes using SEM_UNDO was exceeded. 

[ERANGE]An operation caused a semval to overflow the system-defined limit, or an operation caused an adjust-on-exit value to exceed the system-defined limit. 

[EINTR]The semop() function was interrupted by a signal. 

[EIDRM]The semaphore ID specified by the semid parameter has been removed from the system. 

[ENOSYS]The requested operation is not supported by this implementation. 

RELATED INFORMATION

Functions: exec(2), exit(2), fork(2), semctl(2), semget(2)

Data Structures: semid_ds(4)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026