Section: Linux Programmer's Manual (3)
Return to Main Contents
shm_open, shm_unlink - Create/open or unlink POSIX shared memory objects
#include <fcntl.h> /* For O_* constants */
int shm_open(const char *name, int oflag, mode_t mode);
int shm_unlink(const char *name);
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
the same region of shared memory.
function performs the converse operation,
removing an object previously created by
The operation of
is analogous to that of
specifies the shared memory object to be created or opened.
For portable use,
should have an initial slash (/) and contain no embedded slashes.
is a bit mask created by ORing together exactly one of
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
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
except that those bits set in the process file mode
creation mask (see
are cleared for the new object.
A set of macro constants which can be used to define
is listed in
A new shared memory object initially has zero length --- the size of the
object can be set using
The newly allocated bytes of a shared memory
object are automatically initialised to 0.
was also specified, and a shared memory object with the given
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.
On successful completion
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.
is set for the file descriptor.
The file descriptor is normally used in subsequent calls
(for a newly created object) and
After a call to
the file descriptor may be closed without affecting the memory mapping.
is analogous to
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
an object with the same
will fail (unless
was specified, in which case a new, distinct object is created).
returns a non-negative file descriptor. On failure,
returns 0 on success, or -1 on error.
is set to indicate the cause of the error. Values which may appear in
include the following:
the shared memory object was denied.
Permission was denied to
in the specified
was specified and the caller does not have write permission on the object.
were specified to
and the shared memory object specified by
The process already has the maximum number of files open.
The length of
The limit on the total number of files open on the system has been
An attempt was made to
that did not exist, and
was not specified.
An attempt was to made to
that does not exist.
These functions are provided in glibc 2.2 and later. Programs using these
functions must specify the
in order to link against the required ("realtime") library.
POSIX leaves the behavior of the combination of
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
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"
- RETURN VALUE
- CONFORMING TO
- SEE ALSO
linux.jgfs.net manual pages