---
name: "shmctl"
section: "2"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/shmctl"
---

# shmctl(2)

System V shared memory control

## NAME

shmctl - System V shared memory control

## LIBRARY

Standard C library (*libc*, *-lc*)

## SYNOPSIS

```
#include <sys/shm.h>
```

**int shmctl(int** *shmid***, int** *op***, struct shmid\_ds \****buf***);**

## DESCRIPTION

**shmctl**() performs the control operation specified by *op* 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;   /* Creation time/time of last
                                    modification via shmctl() */
    pid_t           shm_cpid;    /* PID of creator */
    pid_t           shm_lpid;    /* PID of last shmat(2)/shmdt(2) */
    shmatt_t        shm_nattch;  /* # of current attaches */
    ...
};
```

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)](/shmat/2.md) system call that attached this segment.

- *shm\_dtime*

  Time of the last [**shmdt**(2)](/shmdt/2.md) 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)](/shmat/2.md) or [**shmdt**(2)](/shmdt/2.md) 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 {
    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 */
};
```

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)](/shmat/2.md) call with the **SHM\_EXEC** flag.)

Valid values for *op* 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.

- **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)](/proc/5.md).

- **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)](/proc/5.md) 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;  /* # of swapped shared
                                 memory pages */
      unsigned long swap_attempts;
                              /* Unused since Linux 2.4 */
      unsigned long swap_successes;
                              /* Unused since Linux 2.4 */
  };
  ```

- **SHM\_STAT** (Linux-specific)

  Return a *shmid\_ds* structure as for **IPC\_STAT**. However, the *shmid* argument is not a segment identifier, but instead an index into the kernel's internal array that maintains information about all shared memory segments on the system.

- **SHM\_STAT\_ANY** (Linux-specific, since Linux 4.17)

  Return a *shmid\_ds* structure as for **SHM\_STAT**. However, *shm\_perm.mode* is not checked for read access for *shmid*, meaning that any user can employ this operation (just as any user may read */proc/sysvipc/shm* to obtain the same information).

The caller can prevent or allow swapping of a shared memory segment with the following *op* values:

- **SHM\_LOCK** (Linux-specific)

  Prevent swapping of the shared memory segment. The caller must fault in any pages that are required to be present after locking is enabled. If a segment has been locked, then the (nonstandard) **SHM\_LOCKED** flag of the *shm\_perm.mode* field in the associated data structure retrieved by **IPC\_STAT** will be set.

- **SHM\_UNLOCK** (Linux-specific)

  Unlock the segment, allowing it to be swapped out.

Before Linux 2.6.10, only a privileged process could employ **SHM\_LOCK** and **SHM\_UNLOCK**. Since Linux 2.6.10, an unprivileged process can employ these operations if its effective UID matches the owner or creator UID of the segment, and (for **SHM\_LOCK**) the amount of memory to be locked falls within the **RLIMIT\_MEMLOCK** resource limit (see [**setrlimit**(2)](/setrlimit/2.md)).

## RETURN VALUE

A successful **IPC\_INFO** or **SHM\_INFO** operation returns the index of the highest used entry in the kernel's internal array recording information about all shared memory segments. (This information can be used with repeated **SHM\_STAT** or **SHM\_STAT\_ANY** operations to obtain information about all shared memory segments on the system.) A successful **SHM\_STAT** operation returns the identifier of the shared memory segment whose index was given in *shmid*. Other operations return 0 on success.

On error, -1 is returned, and *errno* is set to indicate the error.

## ERRORS

- **EACCES**

  **IPC\_STAT** or **SHM\_STAT** is requested and *shm\_perm.mode* does not allow read access for *shmid*, and the calling process does not have the **CAP\_IPC\_OWNER** capability in the user namespace that governs its IPC namespace.

- **EFAULT**

  The argument *op* has value **IPC\_SET** or **IPC\_STAT** but the address pointed to by *buf* isn't accessible.

- **EIDRM**

  *shmid* points to a removed identifier.

- **EINVAL**

  *shmid* is not a valid identifier, or *op* is not a valid operation. Or: for a **SHM\_STAT** or **SHM\_STAT\_ANY** operation, the index value specified in *shmid* referred to an array slot that is currently unused.

- **ENOMEM**

  (Since Linux 2.6.9), **SHM\_LOCK** was specified and the size of the to-be-locked segment would mean that the total bytes in locked shared memory segments would exceed the limit for the real user ID of the calling process. This limit is defined by the **RLIMIT\_MEMLOCK** soft resource limit (see [**setrlimit**(2)](/setrlimit/2.md)).

- **EOVERFLOW**

  **IPC\_STAT** is attempted, and the GID or UID value is too large to be stored in the structure pointed to by *buf*.

- **EPERM**

  **IPC\_SET** or **IPC\_RMID** is attempted, and the effective user ID of the calling process is not that of the creator (found in *shm\_perm.cuid*), or the owner (found in *shm\_perm.uid*), and the process was not privileged (Linux: did not have the **CAP\_SYS\_ADMIN** capability).

-

  Or (before Linux 2.6.9), **SHM\_LOCK** or **SHM\_UNLOCK** was specified, but the process was not privileged (Linux: did not have the **CAP\_IPC\_LOCK** capability). (Since Linux 2.6.9, this error can also occur if the **RLIMIT\_MEMLOCK** is 0 and the caller is not privileged.)

## VERSIONS

Linux permits a process to attach ([**shmat**(2)](/shmat/2.md)) a shared memory segment that has already been marked for deletion using *shmctl(IPC\_RMID)*. This feature is not available on other UNIX implementations; portable applications should avoid relying on it.

## STANDARDS

POSIX.1-2024.

## HISTORY

POSIX.1-2001, SVr4.

Various fields in a *struct shmid\_ds* were typed as *short* under Linux 2.2 and have become *long* under Linux 2.4. To take advantage of this, a recompilation under glibc-2.1.91 or later should suffice. (The kernel distinguishes old and new calls by an **IPC\_64** flag in *op*.)

## NOTES

The **IPC\_INFO**, **SHM\_STAT**, and **SHM\_INFO** operations are used by the [**ipcs**(1)](/ipcs/1.md) program to provide information on allocated resources. In the future, these may modified or moved to a */proc* filesystem interface.

## SEE ALSO

[**mlock**(2)](/mlock/2.md), [**setrlimit**(2)](/setrlimit/2.md), [**shmget**(2)](/shmget/2.md), [**shmop**(2)](/shmop/2.md), [**capabilities**(7)](/capabilities/7.md), [**sysvipc**(7)](/sysvipc/7.md)
