shm_open creates and opens a new, or opens an existing, POSIX shared
memory object. A POSIX shared memory object is in effect a handle which can be
used by unrelated processes to mmap(2) the same region of shared
memory. The shm_unlink function performs the converse operation,
removing an object previously created by shm_open.
The operation of shm_open is analogous to that of open(2).
name specifies the shared memory object to be created or opened. For
portable use, name should have an initial slash (/) and contain no
oflag is a bit mask created by ORing together exactly one of
O_RDONLY or O_RWDR and any of the other flags listed here:
Open the object for read access. A shared memory object
opened in this way can only be mmap(2)ed for read (
Open the object for read-write access.
Create the shared memory object if it does not exist. The
user and group ownership of the object are set as for open(2), and
the object's permission bits are set according to the low-order 9 bits of
mode, except that those bits set in the process file mode creation
mask (see umask(2)) are cleared for the new object. (A set of macro
constants which can be used to define mode is listed in
A new shared memory object initially has zero length - the size of the
object can be set using ftruncate(2). (The newly-allocated bytes of
a shared memory object are automatically initialised to 0.)
If O_CREAT was also specified, and a share memory
object with the given name already exists, return an error. The
check for the existence of the object, and its creation if it does not
exist, are performed atomically.
If the shared memory object already exists, truncate it to
On successful completion shm_open returns a new file descriptor referring
to the shared memory object. This file descriptor is guaranteed to be the
lowest-numbered file descriptor not previously opened within the process. The
FD_CLOEXEC flag (see fcntl(2)) is set for the file descriptor.
The file descriptor is normally used in subsequent calls to ftruncate(2)
(for a newly-created object) and mmap(2). After a call to
mmap(2) the file descriptor may be closed without affecting the memory
The operation of shm_unlink is analogous to unlink(2): it removes
a shared memory object name, and, once all processes have unmapped the object,
de-allocates and destroys the contents of the associated memory region. After
a successful shm_unlink, attempts to shm_open an object with the
same name will fail (unless O_CREAT was specified, in which case
a new, distinct object is created).
These functions are provided in glibc 2.2 and later. Programs using these
functions must specify the -lrt flag to cc in order to link
against the required ("realtime") library.
POSIX leaves the behavior of the combination of O_RDONLY and
O_TRUNC unspecified. On Linux, this will successfully truncate an
existing shared memory object - this may not be so on other Unices.
The POSIX shared memory object implementation on Linux 2.4 makes use of a
dedicated file system, which is normally mounted under /dev/shm.