shmctl (2) - Linux Man Pages
shmctl: System V shared memory control
Command to display shmctl
manual in Linux: $ man 2 shmctl
NAME
shmctl - System V shared memory control
SYNOPSIS
#include <sys/ipc.h>
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
DESCRIPTION
shmctl()
performs the control operation specified by
cmd
on the System V shared memory segment whose identifier is given in
shmid.
The
buf
argument is a pointer to a shmid_ds structure,
defined in <sys/shm.h> as follows:
struct shmid_ds {
struct ipc_perm shm_perm; /* Ownership and permissions */
size_t shm_segsz; /* Size of segment (bytes) */
time_t shm_atime; /* Last attach time */
time_t shm_dtime; /* Last detach time */
time_t shm_ctime; /* Last change time */
pid_t shm_cpid; /* PID of creator */
pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */
shmatt_t shm_nattch; /* No. of current attaches */
...
};
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 shmget(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 + SHM_DEST and
SHM_LOCKED flags */
unsigned short __seq; /* Sequence number */
};
Valid values for
cmd
are:
- IPC_STAT
-
Copy information from the kernel data structure associated with
shmid
into the
shmid_ds
structure pointed to by buf.
The caller must have read permission on the
shared memory segment.
- IPC_SET
-
Write the values of some members of the
shmid_ds
structure pointed to by
buf
to the kernel data structure associated with this shared memory segment,
updating also its
shm_ctime
member.
The following fields can be changed:
shm_perm.uid, shm_perm.gid,
and (the least significant 9 bits of) shm_perm.mode.
The effective UID of the calling process must match the owner
(shm_perm.uid)
or creator
(shm_perm.cuid)
of the shared memory segment, or the caller must be privileged.
- IPC_RMID
-
Mark the segment to be destroyed.
The segment will actually be destroyed
only after the last process detaches it (i.e., when the
shm_nattch
member of the associated structure
shmid_ds
is zero).
The caller must be the owner or creator of the segment, or be privileged.
The
buf
argument is ignored.
-
If a segment has been marked for destruction, then the (nonstandard)
SHM_DEST
flag of the
shm_perm.mode
field in the associated data structure retrieved by
IPC_STAT
will be set.
-
The caller must ensure that a segment is eventually destroyed;
otherwise its pages that were faulted in will remain in memory or swap.
-
See also the description of
/proc/sys/kernel/shm_rmid_forced
in
proc(5).
- IPC_INFO (Linux-specific)
-
Return information about system-wide shared memory limits and
parameters in the structure pointed to by
buf.
This structure is of type
shminfo
(thus, a cast is required),
defined in
<sys/shm.h>
if the
_GNU_SOURCE
feature test macro is defined:
-
struct shminfo {
unsigned long shmmax; /* Maximum segment size */
unsigned long shmmin; /* Minimum segment size;