Manpage of SHM_OPEN
SHM_OPENSection: Linux Programmer's Manual (3)
NAMEshm_open, shm_unlink - create/open or unlink POSIX shared memory objects
#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);
DESCRIPTIONshm_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 ly created by shm_open().
The operation of shm_open() is analogous to that of open(2). namespecifies 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.
oflagis a bit mask created by ORing together exactly one of O_RDONLYor O_RDWRand 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
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
modeis listed in
(Symbolic definitions of these constants can be obtained by including
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_CREATwas also specified, and a shared memory object with the given namealready 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 fcntl(2)) is set for the file descriptor.ly opened within the process. The FD_CLOEXECflag (see
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 of the associated memory region. After a successful shm_unlink(), attempts to shm_open() an object with the same namewill fail (unless O_CREATwas specified, in which case a new, distinct object is created).
RETURN VALUEOn success, shm_open() returns a nonnegative file descriptor. On failure, shm_open() returns -1. shm_unlink() returns 0 on success, or -1 on error.
ERRORSOn failure, errnois set to indicate the cause of the error. Values which may appear in errnoinclude the following:
- Permission to shm_unlink() the shared memory object was denied.
- Permission was denied to shm_open() namein the specified mode, or O_TRUNCwas specified and the caller does not have write permission on the object.
- Both O_CREATand O_EXCLwere specified to shm_open() and the shared memory object specified by namealready exists.
- The nameargument to shm_open() was invalid.
- The per-process limit on the number of open file descriptors has been reached.
- The length of nameexceeds PATH_MAX.
- The system-wide limit on the total number of open files has been reached.
- An attempt was made to shm_open() a namethat did not exist, and O_CREATwas not specified.
- An attempt was to made to shm_unlink() a namethat does not exist.
VERSIONSThese functions are provided in glibc 2.2 and later.
ATTRIBUTESFor an explanation of the terms used in this section, see attributes(7).
|shm_open(), shm_unlink()||Thread safety||MT-Safe locale|
CONFORMING TOPOSIX.1-2001, POSIX.1-2008.
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.1-2008 says that the group ownership may be set to either the calling process's effective group ID or, if the object is visible in the filesystem, the group ID of the parent directory.
POSIX leaves the behavior of the combination of O_RDONLYand O_TRUNCunspecified. On Linux, this will successfully truncate an existing shared memory object---this may not be so on other UNIX systems.
SEE ALSOclose(2), fchmod(2), fchown(2), fcntl(2), fstat(2), ftruncate(2), memfd_create(2), mmap(2), open(2), umask(2), shm_overview(7)
This document was created by man2html, using the manual pages.
Time: 22:27:46 GMT, June 20, 2016 Click Here!