shmctl (2) Linux Manual Page
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 {
The fields of the shmid_ds structure are as follows:
- shm_perm
- This is an ipc_perm structure (see below) that specifies the access permissions on the shared memory segment.
- shm_segsz
- Size in bytes of the shared memory segment.
- shm_atime
- Time of the last shmat(2) system call that attached this segment.
- shm_dtime
- Time of the last shmdt(2) system call that detached tgis segment.
- shm_ctime
- Time of creation of segment or time of the last shmctl() IPC_SET operation.
- shm_cpid
- ID of the process that created the shared memory segment.
- shm_lpid
- ID of the last process that executed a shmat(2) or shmdt(2) system call on this segment.
- shm_nattch
- Number of processes that have this segment attached.
The ipc_perm structure is defined as follows (the highlighted fields are settable using IPC_SET):
struct ipc_perm {
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 |
Bits 0100, 0010, and 0001 (the execute bits) are unused by the system. (It is not necessary to have execute permission on a segment in order to perform a shmat(2) call with the SHM_EXEC flag.)
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 are updated: 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.
- The following fields are updated: shm_perm.uid, shm_perm.gid, and (the least significant 9 bits of) shm_perm.mode.
- 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).
- 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.
- 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;
always 1 */
unsigned long shmmni; /* Maximum number of segments */
unsigned long shmseg; /* Maximum number of segments
that a process can attach;
unused within kernel */
unsigned long shmall; /* Maximum number of pages of
shared memory, system-wide */ };
- The shmmni, shmmax, and shmall settings can be changed via /proc files of the same name; see proc(5) for details.
- SHM_INFO (Linux-specific)
- Return a shm_info structure whose fields contain information about system resources consumed by shared memory. This structure is defined in <sys/shm.h> if the _GNU_SOURCE feature test macro is defined:
- struct shm_info {
int used_ids; /* # of currently existing
segments */
unsigned long shm_tot; /* Total number of shared
memory pages */
unsigned long shm_rss; /* # of resident shared
memory pages */
unsigned long shm_swp; - struct shminfo {
