semctl(2) CLIX semctl(2)
NAME
semctl - Semaphore control operations
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semctl(
int semid ,
int semnum ,
int cmd ,
union semnum {
int val;
struct semid_ds *buf;
ushort *array;
} arg );
PARAMETERS
semid Represents the identifier of an already existing semaphore
(created by the semget() function).
semnum Selects a semaphore by its number.
cmd Represents a control command.
arg Passes the appropriate union member for the requested control
command.
DESCRIPTION
The semctl() function provides a variety of semaphore control operations
as specified by the value of cmd.
The following cmds are executed with respect to the semaphore specified by
semid and semnum:
GETVAL Returns the value of semval. (See intro(2).)
SETVAL Sets the value of semval to arg.val. When this cmd is
successfully executed, the semadj value corresponding to the
specified semaphore in all processes is cleared.
2/94 - Intergraph Corporation 1
semctl(2) CLIX semctl(2)
GETPID Returns the value of sempid. (See the semget() function.)
GETNCNT Returns the value of semncnt. (See the semget() function.)
GETZCNT Returns the value of semzcnt. (See the semget() function.)
The following cmds return and set, every semval in the set of semaphores,
respectively:
GETALL Places semvals in the array pointed to by arg.array.
SETALL Sets semvals according to the array pointed to by arg.array.
When this cmd is successfully executed the semadj values
corresponding to each specified semaphore in all processes are
cleared.
The following cmds are also available:
IPC_STAT Places the current value of each member of the data structure
associated with semid into the structure pointed to by arg.buf.
The contents of this structure are defined in intro(2).
IPC_SET Sets the value of the following members of the data structure
associated with semid to the corresponding value found in the
structure pointed to by arg.buf:
sem_perm.uid
sem_perm.gid
sem_perm.mode /* Only low 9 bits */
This cmd can only be executed by a process that has an
effective user ID equal to either that of superuser, or to the
value of sem_perm.cuid or sem_perm.uid in the data structure
associated with semid.
IPC_RMID Removes the semaphore identifier specified by semid from the
system and destroy the set of semaphores and the data structure
associated with it. This cmd can only be executed by a process
that has an effective user ID equal to either that of
superuser, or to the value of sem_perm.cuid or sem_perm.uid in
the data structure associated with semid.
EXAMPLES
This example gets three semaphores, sets them all to 1, prints out the
value of each semaphore, and then removes the semaphore.
#define NUM_SEMS 3
typedef union {
2 Intergraph Corporation - 2/94
semctl(2) CLIX semctl(2)
int val;
struct semid_ds *buf;
ushort array[NUM_SEMS];
} Sem_union;
int semid;
int i;
struct semid_ds buf;
Sem_union sem_union;
if ((semid = semget(75, NUM_SEMS, 0600 | IPC_CREAT | IPC_EXCL))
< 0) {
perror("Could not get semaphores");
exit(1);
}
for (i = 0; i < NUM_SEMS; i++)
sem_union.array[i] = 1;
if (semctl(semid, 0, SETALL, sem_union.array) < 0)
perror("Setting semaphores to 1");
if (semctl(semid, 0, GETALL, sem_union.array) < 0)
perror("Could not GETALL on semaphore");
for (i = 0; i < NUM_SEMS; i++)
printf("semaphore[%d] = %d\n", i, sem_union.array[i]);
if (semctl(semid, 0, IPC_RMID, (Sem_union *)0) < 0) {
perror("Could not remove semaphores");
exit(1);
}
RETURN VALUES
Upon successful completion, the value returned depends on the value of cmd
as follows:
GETVAL The value of semval.
GETPID The value of sempid.
GETNCNT The value of semncnt.
GETZCNT The value of semcnt.
All others
A value of 0.
Otherwise, a value of -1 is returned and errno is set to indicate the
2/94 - Intergraph Corporation 3
semctl(2) CLIX semctl(2)
error.
ERRORS
The semctl() function fails if one or more of the following are true:
[EINVAL] The value of semid is not a valid semaphore identifier.
[EINVAL] The value of semnum is less than 0 or greater than sem_nsems.
[EINVAL] The value of cmd does no represent a possible command.
[EACCES] Operation permission is denied to the calling process. (See
intro(2).)
[ERANGE] The value of cmd is SETVAL or SETALL and the value to which
semval is to be set is greater than the system-imposed maximum.
[EPERM] The value of cmd is equal to IPC_RMID or IPC_SET and the
effective user ID of the calling process is not equal to that
of superuser, or to the value of sem_perm.cuid or sem_perm.uid
in the data structure associated with semid.
[EFAULT] The arg.buf variable points to an illegal address.
RELATED INFORMATION
Functions: intro(2), semget(2), semop(2)
4 Intergraph Corporation - 2/94