shm_open, shm_unlink − create/open or unlink POSIX shared memory objects


#include <sys/mman.h>
#include <sys/stat.h>
/* For mode constants */
#include <fcntl.h>
/* For O_* constants */

int shm_open(const char *name, int oflag, mode_t mode);

int shm_unlink(const char *name);

Link with −lrt.


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, a shared memory object should be identified by a name of the form /somename; that is, a null-terminated string of up to NAME_MAX (i.e., 255) characters consisting of an initial slash, followed by one or more characters, none of which are slashes.

oflag is a bit mask created by ORing together exactly one of O_RDONLY or O_RDWR and any of the other flags listed here:


Open the object for read access. A shared memory object opened in this way can be mmap(2)ed only for read (PROT_READ) access.


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 taken from the corresponding effective IDs of the calling process, 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 open(2). (Symbolic definitions of these constants can be obtained by including <sys/stat.h>.)

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 initialized to 0.


If O_CREAT was also specified, and a shared 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 zero bytes.

Definitions of these flag values can be obtained by including <fcntl.h>.

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 mapping.

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).


On success, shm_open() returns a nonnegative file descriptor. On failure, shm_open() returns −1. shm_unlink() returns 0 on success, or −1 on error.


On failure, errno is set to indicate the cause of the error. Values which may appear in errno include the following:


Permission to shm_unlink() the shared memory object was denied.


Permission was denied to shm_open() name in the specified mode, or O_TRUNC was specified and the caller does not have write permission on the object.


Both O_CREAT and O_EXCL were specified to shm_open() and the shared memory object specified by name already exists.


The name argument to shm_open() was invalid.


The process already has the maximum number of files open.


The length of name exceeds PATH_MAX.


The limit on the total number of files open on the system has been reached.


An attempt was made to shm_open() a name that did not exist, and O_CREAT was not specified.


An attempt was to made to shm_unlink() a name that does not exist.


These functions are provided in glibc 2.2 and later.



POSIX.1-2001 says that the group ownership of a newly created shared memory object is set to either the calling process’s effective group ID or "a system default group ID".


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 UNIX systems.

The POSIX shared memory object implementation on Linux 2.4 makes use of a dedicated filesystem, which is normally mounted under /dev/shm.


close(2), fchmod(2), fchown(2), fcntl(2), fstat(2), ftruncate(2), mmap(2), open(2), umask(2), shm_overview(7)


This page is part of release 3.69 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at−pages/.

More Linux Commands

lookup_dcookie(2) - return a directory entry's path.........
Look up the full path of the directory entry specified by the value cookie. The cookie is an opaque identifier uniquely identifying a particular directory entry

delch(3ncurses) - delete character under the cursor in a cur
delch.3ncurses - These routines delete the character under the cursor; all characters to the right of the cursor on the same line are moved to the left one posi

XML::DOM::DocumentFragment(3pm) - Facilitates cut & paste in
XML::DOM::DocumentFragment extends XML::DOM::Node DocumentFragment is a lightweight or minimal Document object. It is very common to want to be able to extract

ri(1) Ruby API reference front end - Linux manual page......
ri is a CLI front end for the Ruby API reference. You can search and read API reference for classes and methods with . ri is a part of Ruby. target can be one o

glutCreateMenu(3) - creates a new pop-up menu. (Man Page)...
glutCreateMenu creates a new pop-up menu and returns a unique small integer identifier. The range of allocated identifiers starts at one. The menu identifier ra

sched_getaffinity(2) - set and get a process's CPU affinity
A threads CPU affinity mask determines the set of CPUs on which it is eligible to run. On a multiprocessor system, setting the CPU affinity mask can be used to

glScale(3gl) - multiply the current matrix by a general scal
glScale produces a nonuniform scaling along the x, y, and z axes. The three parameters indicate the desired scale factor along each of the three axes. The curre

gnutls_x509_crq_sign2(3) - API function - Linux manual page
gnutls_x509_crq_sign2.3 - This function will sign the certificate request with a private key. This must be the same key as the one used in gnutls_x509_crt_set_k

zypp-refresh(8) Autorefresh metadata of all enabled reposito
zypp-refresh refreshes metadata of all enabled repositories which have autorefresh turned on (see zypper lr). For use e.g. in cron jobs or scripts. FILES /var/l

easyprof(8) AppArmor profile generation made easy...........
aa-easyprof provides an easy to use interface for AppArmor policy generation. aa-easyprof supports the use of templates and policy groups to quickly profile an

gnutls_x509_crt_get_issuer_dn_oid(3) - API function.........
This function will extract the OIDs of the name of the Certificate issuer specified by the given index. If oid is null then only the size will be filled. If the

unsq(1) - squeeze or unsqueeze a sorted word list (ManPage)
sq compresses a sorted list of words (a dictionary). For example: sort -u /usr/dict/words | sq | gzip -9 &gt; words.sq.Z will compress dict by about a factor of 5.

We can't live, work or learn in freedom unless the software we use is free.