semctl

Performs a semaphore control operation.

Library:LibC
Classification:System-V
Service:Synchronization

Syntax

  #include <sys/sem.h>
   
  int semctl (
     int   semid,
     int   semnum,
     int   cmd,
     ... 
  

Parameters

semid

(IN) Specifies the semaphore identifier for the set (returned from the semget function).

semnum

(IN) Specifies the number of semaphores in the set.

cmd

(IN) Specifies a semaphore command with one of the following flags:

Flag

Value

Description

IPC_RMID

0

Removes the semaphore identifier and destroys the semaphore set.

IPC_SET

1

Sets the access permissions in the ipc_perm structure of the semid_ds structure.

IPC_STAT

2

Returns the access permissions in the ipc_perm structure of the semid_ds structure.

GETNCNT

3

Returns the number of threads waiting for the semaphore count to become greater than the current value.

GETPID

4

Returns the ID of the process from the last semop operation.

GETVAL

5

Returns the value of the semaphore. The semnum parameter specifies which semaphore.

GETALL

6

Returns the value of all the semaphores in the set. The optional parameter must point to an array for the values

GETZCNT

7

Returns the number of threads waiting for the semaphore count to become greater than 0.

SETVAL

8

Sets the value of the semaphore. The semnum parameter specifies which semaphore in the set. The optional parameter specifies the value.

SETALL

9

Sets the value of all the semaphores in the set. The optional parameter must point to an array of values.

...

(IN) Specifies an optional argument, if needed by the semaphore command. This should be an element in the semun union.

Return Values

If successful, the value returned depends upon the command.

  • GETNCNT returns the value of semncnt from the sem structure.

  • GETPID returns the value of sempid from the sem structure.

  • GETVAL returns the value of semva from the sem structurel.

  • GETZCNT returns the value of semzcnt from the sem structure.

  • All other commands return 0 on success.

If unsucessful, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

6

EACCES

The calling thread has insufficient rights to perform the command.

9

EINVAL

The semid parameter is not a valid semaphore identifier or the semnum parameter is invalid.

14

ERANGE

The operation would cause an overflow of system-imposed limits.

69

EPERM

The calling thread has insufficient rights to perform the command.

105

ENOCONTEXT

The calling thread has no context.

Remarks

When you specify the fourth parameter, you should make the explicit assignment prior to calling semctl.

See Also