1SHM_OPEN(3)                Linux Programmer's Manual               SHM_OPEN(3)
2
3
4

NAME

6       shm_open,  shm_unlink  -  create/open  or  unlink  POSIX  shared memory
7       objects
8

SYNOPSIS

10       #include <sys/mman.h>
11       #include <sys/stat.h>        /* For mode constants */
12       #include <fcntl.h>           /* For O_* constants */
13
14       int shm_open(const char *name, int oflag, mode_t mode);
15
16       int shm_unlink(const char *name);
17
18       Link with -lrt.
19

DESCRIPTION

21       shm_open() creates and opens a new, or opens an existing, POSIX  shared
22       memory  object.   A  POSIX  shared  memory object is in effect a handle
23       which can be used by unrelated processes to mmap(2) the same region  of
24       shared  memory.  The shm_unlink() function performs the converse opera‐
25       tion, removing an object previously created by shm_open().
26
27       The operation of shm_open() is analogous  to  that  of  open(2).   name
28       specifies the shared memory object to be created or opened.  For porta‐
29       ble use, a shared memory object should be identified by a name  of  the
30       form  /somename;  that  is,  a null-terminated string of up to NAME_MAX
31       (i.e., 255) characters consisting of an initial slash, followed by  one
32       or more characters, none of which are slashes.
33
34       oflag  is  a bit mask created by ORing together exactly one of O_RDONLY
35       or O_RDWR and any of the other flags listed here:
36
37       O_RDONLY   Open the object for read access.   A  shared  memory  object
38                  opened   in   this  way  can  be  mmap(2)ed  only  for  read
39                  (PROT_READ) access.
40
41       O_RDWR     Open the object for read-write access.
42
43       O_CREAT    Create the shared memory object if it does not  exist.   The
44                  user  and  group  ownership of the object are taken from the
45                  corresponding effective IDs of the calling process, and  the
46                  object's  permission bits are set according to the low-order
47                  9 bits of mode, except that those bits set  in  the  process
48                  file  mode  creation mask (see umask(2)) are cleared for the
49                  new object.  A set of macro constants which can be  used  to
50                  define  mode is listed in open(2).  (Symbolic definitions of
51                  these constants can be obtained by including <sys/stat.h>.)
52
53                  A new shared memory object  initially  has  zero  length—the
54                  size of the object can be set using ftruncate(2).  The newly
55                  allocated bytes of a shared memory object are  automatically
56                  initialized to 0.
57
58       O_EXCL     If  O_CREAT  was  also specified, and a shared memory object
59                  with the given name already exists, return  an  error.   The
60                  check  for  the existence of the object, and its creation if
61                  it does not exist, are performed atomically.
62
63       O_TRUNC    If the shared memory object already exists, truncate  it  to
64                  zero bytes.
65
66       Definitions   of  these  flag  values  can  be  obtained  by  including
67       <fcntl.h>.
68
69       On successful completion  shm_open()  returns  a  new  file  descriptor
70       referring to the shared memory object.  This file descriptor is guaran‐
71       teed to be the lowest-numbered file descriptor  not  previously  opened
72       within  the process.  The FD_CLOEXEC flag (see fcntl(2)) is set for the
73       file descriptor.
74
75       The file descriptor is normally used  in  subsequent  calls  to  ftrun‐
76       cate(2)  (for  a  newly  created  object) and mmap(2).  After a call to
77       mmap(2) the file descriptor may be closed without affecting the  memory
78       mapping.
79
80       The  operation  of shm_unlink() is analogous to unlink(2): it removes a
81       shared memory object name, and, once all processes  have  unmapped  the
82       object, de-allocates and destroys the contents of the associated memory
83       region.  After a successful shm_unlink(),  attempts  to  shm_open()  an
84       object  with  the same name will fail (unless O_CREAT was specified, in
85       which case a new, distinct object is created).
86

RETURN VALUE

88       On success, shm_open() returns a nonnegative file descriptor.  On fail‐
89       ure,  shm_open()  returns -1.  shm_unlink() returns 0 on success, or -1
90       on error.
91

ERRORS

93       On failure, errno is set to indicate the cause of  the  error.   Values
94       which may appear in errno include the following:
95
96       EACCES Permission to shm_unlink() the shared memory object was denied.
97
98       EACCES Permission  was denied to shm_open() name in the specified mode,
99              or O_TRUNC was specified and the caller does not have write per‐
100              mission on the object.
101
102       EEXIST Both  O_CREAT  and  O_EXCL  were specified to shm_open() and the
103              shared memory object specified by name already exists.
104
105       EINVAL The name argument to shm_open() was invalid.
106
107       EMFILE The process already has the maximum number of files open.
108
109       ENAMETOOLONG
110              The length of name exceeds PATH_MAX.
111
112       ENFILE The limit on the total number of files open on  the  system  has
113              been reached.
114
115       ENOENT An attempt was made to shm_open() a name that did not exist, and
116              O_CREAT was not specified.
117
118       ENOENT An attempt was to made to shm_unlink()  a  name  that  does  not
119              exist.
120

VERSIONS

122       These functions are provided in glibc 2.2 and later.
123

CONFORMING TO

125       POSIX.1-2001.
126
127       POSIX.1-2001  says  that  the group ownership of a newly created shared
128       memory object is set to either the calling process's effective group ID
129       or "a system default group ID".
130

NOTES

132       POSIX  leaves  the  behavior of the combination of O_RDONLY and O_TRUNC
133       unspecified.  On Linux, this will  successfully  truncate  an  existing
134       shared memory object—this may not be so on other UNIX systems.
135
136       The POSIX shared memory object implementation on Linux 2.4 makes use of
137       a dedicated file system, which is normally mounted under /dev/shm.
138

SEE ALSO

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

COLOPHON

144       This  page  is  part of release 3.53 of the Linux man-pages project.  A
145       description of the project, and information about reporting  bugs,  can
146       be found at http://www.kernel.org/doc/man-pages/.
147
148
149
150Linux                             2009-02-25                       SHM_OPEN(3)
Impressum