---
name: "shmget"
section: "2"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/shmget/2"
---

# shmget(2)

get shared memory area identifier

## NAME

**shmget** – get shared memory area identifier

## SYNOPSIS

**#include \<sys/shm.h\>**

*int*\

**shmget**(*key\_t key*, *size\_t size*, *int shmflg*);

## DESCRIPTION

**shmget**() returns the shared memory identifier associated with the key *key*.

A shared memory segment is created if either *key* is equal to IPC\_PRIVATE, or *key* does not have a shared memory segment identifier associated with it, and the IPC\_CREAT bit is set in *shmflg.*

If a new shared memory segment is created, the data structure associated with it (the *shmid\_ds* structure, see [shmctl(2)](/shmctl/2.md)) is initialized as follows:

- *shm\_perm.cuid* and *shm\_perm.uid* are set to the effective uid of the calling process.

- *shm\_perm.gid* and *shm\_perm.cgid* are set to the effective gid of the calling process.

- *shm\_perm.mode* is set to the lower 9 bits of *shmflg*.

- *shm\_lpid*, *shm\_nattch*, *shm\_atime*, and  *shm\_dtime* are set to 0

- *shm\_ctime* is set to the current time.

- *shm\_segsz* is set to the value of *size*.

- The [ftok(3)](/ftok/3.md) function may be used to generate a key from a pathname.

## RETURN VALUES

Upon successful completion a positive shared memory segment identifier is returned. Otherwise, -1 is returned and the global variable *errno* is set to indicate the error.

## ERRORS

The **shmget**() system call will fail if:

- \[EACCES\]

  A shared memory segment is already associated with *key* and the caller has no permission to access it.

- \[EEXIST\]

  Both IPC\_CREAT and IPC\_EXCL are set in *shmflg*, and a shared memory segment is already associated with *key*.

- \[EINVAL\]

  No shared memory segment is to be created, and a shared memory segment exists for *key*, but the size of the segment associated with it is less than *size*, which is non-zero.

- \[ENOENT\]

  IPC\_CREAT was not set in *shmflg* and no shared memory segment associated with *key* was found.

- \[ENOMEM\]

  There is not enough memory left to created a shared memory segment of the requested size.

- \[ENOSPC\]

  A new shared memory identifier could not be created because the system limit for the number of shared memory identifiers has been reached.

## LEGACY SYNOPSIS

**#include \<sys/types.h\>** **#include \<sys/ipc.h\>** **#include \<sys/shm.h\>**

*int*\
 **shmget**(*key\_t key*, *int size*, *int shmflg*);

All of these include files are necessary. The type of *size* has changed.

## SEE ALSO

[ftok(3)](/ftok/3.md), [shmat(2)](/shmat/2.md), [shmctl(2)](/shmctl/2.md), [shmdt(2)](/shmdt/2.md), [compat(5)](/compat/5.md)
