semctl (2) - Linux Manuals
semctl: System V semaphore control operations
Command to display semctl
manual in Linux: $ man 2 semctl
NAME
semctl - System V semaphore control operations
SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int semctl(int semid, int semnum, int cmd, ...);
DESCRIPTION
semctl()
performs the control operation specified by
cmd
on the System V semaphore set identified by
semid,
or on the
semnum-th
semaphore of that set.
(The semaphores in a set are numbered starting at 0.)
This function has three or four arguments, depending on
cmd.
When there are four, the fourth has the type
union semun.
The calling program must define this union as follows:
union semun {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO
(Linux-specific) */
};
The
semid_ds
data structure is defined in <sys/sem.h> as follows:
struct semid_ds {
struct ipc_perm sem_perm; /* Ownership and permissions */
time_t sem_otime; /* Last semop time */
time_t sem_ctime; /* Creation time/time of last
modification via semctl() */
unsigned long sem_nsems; /* No. of semaphores in set */
};
The fields of the
semid_ds
structure are as follows:
- sem_perm
-
This is an
ipc_perm
structure (see below) that specifies the access permissions on the semaphore
set.
- sem_otime
-
Time of last
semop(2)
system call.
- sem_ctime
-
Time of creation of semaphore set or time of last
semctl()
IPCSET,
SETVAL,
or
SETALL
operation.
- sem_nsems
-
Number of semaphores in the set.
Each semaphore of the set is referenced by a nonnegative integer
ranging from
0
to
sem_nsems-1.
The
ipc_perm
structure is defined as follows
(the highlighted fields are settable using
IPC_SET):
struct ipc_perm {
key_t __key; /* Key supplied to semget(2) */
uid_t uid; /* Effective UID of owner */
gid_t gid; /* Effective GID of owner */
uid_t cuid; /* Effective UID of creator */
gid_t cgid; /* Effective GID of creator */
unsigned short mode; /* Permissions */
unsigned short __seq; /* Sequence number */
};
The least significant 9 bits of the
mode
field of the
ipc_perm
structure define the access permissions for the shared memory segment.
The permission bits are as follows:
0400 | Read by user
|
0200 | Write by user
|
0040 | Read by group
|
0020 | Write by group
|
0004 | Read by others
|
0002 | Write by others
|
In effect, "write" means "alter" for a semaphore set.
Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
Valid values for
cmd
are:
- IPC_STAT
-
Copy information from the kernel data structure associated with
semid
into the
semid_ds
structure pointed to by
arg.buf.
The argument
semnum
is ignored.
The calling process must have read permission on the semaphore set.
- IPC_SET
-
Write the values of some members of the
semid_ds
structure pointed to by
arg.buf
to the kernel data structure associated with this semaphore set,
updating also its
sem_ctime
member.
-
The following members of the structure are updated:
sem_perm.uid,
sem_perm.gid,
and (the least significant 9 bits of)
sem_perm.mode.
-
The effective UID of the calling process must match the owner
(sem_perm.uid)
or creator
(sem_perm.cuid)
of the semaphore set, or the caller must be privileged.
The argument
semnum
is ignored.
- IPC_RMID
-
Immediately remove the semaphore set,
awakening all processes blocked in
semop(2)
calls on the set (with an error return and
errno
set to
EIDRM).
The effective user ID of the calling process must
match the creator or owner of the semaphore set,
or the caller must be privileged.
The argument
semnum
is ignored.
- IPC_INFO (Linux-specific)
-
Return information about system-wide semaphore limits and
parameters in the structure pointed to by
arg.__buf.
This structure is of type
seminfo,
defined in
<sys/sem.h>
if the
_GNU_SOURCE
feature test macro is defined:
-
struct seminfo {
int semmap; /* Number of entries in semaphore
map; unused within kernel */
int semmni; /* Maximum number of semaphore sets */
int semmns; /* Maximum number of semaphores in all