HPlogo MPE/iX Developer's Kit Reference Manual Volume 2: HP 3000 MPE/iX Computer Systems > Chapter 2 SVID IPC Library Function Descriptions

semop

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

Performs operations on a set of semaphores.

Syntax

   #include <sys/types.h>

   #include <sys/ipc.h>

   #include <sys/sem.h>



   int semop (int semid, struct sembuf *sops, int nsops);

Parameters

semid

Passes a semaphore identifier returned by a semget() call.

sops

Passes a pointer to an array of semaphore operation structures where each element is of type struct sembuf (defined in the <sys/sem.h> header). Semaphore operation structures define operations to perform on the semaphore set. For details on using semaphore operation structures, refer to the "Description" section below.

nsops

Passes the number of valid semaphore operation structures in the array pointed to by sops.

Return Values

0

Success.

-1

An error occurred, and errno is set to indicate the error condition.

Description

The semop() function performs operations on the set of semaphores associated with the semaphore identifier specified by semid.

The sops argument points to an array where each of nsops elements contains a semaphore operation structure. Each semaphore operation specified by the sem_op field is performed on the semaphore specified by sem_num. The operation is further defined by the sem_flg field. No semaphore operations are performed until blocking conditions on all of the semaphores specified in the array are removed.

If the value of sem_op is less than 0 and the calling process has write permission, one of the following operations occurs depending upon the current semaphore value and the value of sem_flag:

Table 2-1 Operations when sem_op <0

Semaphore

Value

sem_flag

Value

Operation
>= absolute value of sem_op0 or IPC_NOWAITA new semaphore value is calculated as the result of subtracting the absolute value of sem_op from the current semaphore value. The call to semop() returns successfully to the calling process.
>= absolute value of sem_opSEM_UNDOA new semaphore value is calculated as the result of subtracting the absolute value of sem_op from the current semaphore value. The absolute value of sem_op is added to the calling process's semaphore adjust value of the specified semaphore. The call to semop() returns successfully to the calling process.
< absolute value of sem_opIPC_NOWAITsemop() returns -1, sets errno to EAGAIN, and returns control to the calling process.
< absolute value of sem_opIPC_NOWAIT not specified

The semncnt field is incremented, indicating the number of processes waiting for the semaphore value of the specified semaphore to become greater than zero. Process execution is suspended until one of the following conditions occurs:

  • The semaphore value becomes greater than or equal to the absolute value of sem_op. When this occurs, the semncnt field is decremented by 1 and execution continues as described above when semaphore value >= absolute value of sem_op.

  • The semaphore identifier is removed from the system. semop() returns with a value of -1 and errno is set to EIRDM.

  • A signal is caught by the suspended process. When this occurs, the semncnt field is decremented by 1 and the calling process resumes execution in the manner defined by the signal facility.

 

If the value of sem_op is equal to 0 and the calling process has read permission, one of the following operations occurs depending upon the current semaphore value and the value of sem_flag:

Table 2-2 Operations when sem_op=0

Semaphore

Value

sem_flag

Value

Operation
0Any valuesemop() executes the next semaphore operation in the array pointed to by semops, or returns successfully to the calling process if there are no more valid semaphore operations.
<>0IPC_NOWAITsemop() returns -1, sets errno to EAGAIN, and returns control to the calling process.
<>00 or SEM_UNDO

The semzcnt field is incremented, indicating the number of processes waiting for the semaphore value of the specified semaphore to become zero. Process execution is suspended until one of the following conditions occurs:

  • The semaphore value becomes zero. When this occurs, the semzcnt field is decremented by 1 and execution continues as described above when semaphore value = 0.

  • The specified semaphore identifier is removed from the system. semop() returns with a value of -1 and errno is set to EIRDM.

  • A signal is caught by the suspended process. When this occurs, the semncnt field is decremented by 1 and the calling process resumes execution in the manner defined by the signal facility.

 

If the value of sem_op is greater than 0 and the calling process has write permission, one of the following operations occurs depending upon the current semaphore value and the value of sem_flag.

Table 2-3 Operations when sem_op>0

Semaphore

Value

sem_flag

Value

Operation
Any value SEM_UNDO not specified A new semaphore value is calculated as the result of adding the value of sem_op to the current semaphore value of the specified semaphore. The call to semop() returns successfully to the calling process.
Any value SEM_UNDOA new semaphore value is calculated as the result of adding the value of sem_op to the current semaphore value of the specified semaphore. The value of sem_op is subtracted from the calling process's semaphore adjust value of the specified semaphore. The call to semop() returns successfully to the calling process.

 

If semop() is successful, the value of sempid for each semaphore specified in the array pointed to by sops is set equal to the PID of the calling process. The value of sem_otime in the data structure associated with the semaphore identifier is set to the current time.

Implementation Considerations

If a process suspended during execution of semop() receives a signal, control returns to the user with errno set to EINTR. Disabled signals are ignored.

The maximum number of semaphore UNDO entries per process is 64. Semaphore adjust values can be maintained for up to 64 distinct semaphores (elements of semaphore sets).

An MPE/iX system manager can use the MPE/iX SVIPC utility to interactively configure:

  • the maximum semaphore value

  • the maximum semaphore adjust value

  • the maximum nsops value

  • The maximum sem_op value

Refer to the section "Managing SVID IPC Services" for more information.

Errors

If an error occurs, errno is set to one of the following values.

E2BIGCAUSEnsops specifies a value greater than the system-defined limit.
 ACTION Check the nsops value and make sure it is within the system-defined range.
EACCESCAUSEThe calling process does not have permission.
 ACTION Ensure the process has write permission (to modify a semaphore value) or read permission (to test a semaphore for 0).
EAGAINCAUSEsem_flg specifies IPC_NOWAIT and the calling process would suspend on the specified operation.
 ACTION None. Application dependent.
EFAULTCAUSE The system detected a NULL or bad address in attempting to use the sops argument.
 ACTION Check to see that the sops argument has been properly defined.
EFBIGCAUSEsem_num is either less than zero or greater than or equal to the number of semaphores in the semaphore set associated with semid.
 ACTION Check the sem_num value to make sure it specifies a valid semaphore in the semaphore set identified by semid.
EIDRMCAUSEThe semaphore set specified by semid was removed while semop() was suspended on a semaphore operation.
 ACTION None.
EINTRCAUSE semget() was interrupted by a signal.
 ACTION None. Application dependent.
EINVALCAUSEsemid is not a valid semaphore identifier, or the calling process requested a SEM_UNDO for a number of semaphores that would exceed the system-defined limit.
 ACTION Check that semid specifies a valid semaphore identifier and that it has not been removed from the system.
ENOSPCCAUSEThe number of maximum undo entries (64) for this process would exceed the system-defined limit.
 ACTION None. There were no undo table entries available to record the SEM_UNDO information. Examine the application to see if SEM_UNDO is required for that many semaphores.
ERANGECAUSE The resulting semaphore value or semaphore adjust value would exceed the system-defined limit.
 ACTIONNone. Application dependent.
ESYSERRCAUSE An operating system error occurred that does not map directly to any of the above errors.
 ACTION Examine the MPE/iX process error stack for the type of system error.

See Also

semctl(), semget(), SVID2 (Section 12)

Feedback to webmaster