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

NAME

6       shmat, shmdt - System V shared memory operations
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/shm.h>
11
12       void *shmat(int shmid, const void *shmaddr, int shmflg);
13
14       int shmdt(const void *shmaddr);
15

DESCRIPTION

17   shmat()
18       shmat() attaches the System V shared memory segment identified by shmid
19       to the address space of the calling process.  The attaching address  is
20       specified by shmaddr with one of the following criteria:
21
22       *  If  shmaddr  is  NULL,  the system chooses a suitable (unused) page-
23          aligned address to attach the segment.
24
25       *  If shmaddr isn't NULL and SHM_RND is specified in shmflg, the attach
26          occurs  at  the address equal to shmaddr rounded down to the nearest
27          multiple of SHMLBA.
28
29       *  Otherwise, shmaddr must be  a  page-aligned  address  at  which  the
30          attach occurs.
31
32       In  addition  to  SHM_RND,  the following flags may be specified in the
33       shmflg bit-mask argument:
34
35       SHM_EXEC (Linux-specific; since Linux 2.6.9)
36              Allow the contents of the segment to be  executed.   The  caller
37              must have execute permission on the segment.
38
39       SHM_RDONLY
40              Attach  the segment for read-only access.  The process must have
41              read permission for the segment.  If this flag is not specified,
42              the  segment  is  attached  for  read  and write access, and the
43              process must have read and write  permission  for  the  segment.
44              There is no notion of a write-only shared memory segment.
45
46       SHM_REMAP (Linux-specific)
47              This  flag  specifies  that  the  mapping  of the segment should
48              replace any existing mapping in the range  starting  at  shmaddr
49              and  continuing for the size of the segment.  (Normally, an EIN‐
50              VAL error would result if  a  mapping  already  exists  in  this
51              address range.)  In this case, shmaddr must not be NULL.
52
53       The  brk(2)  value of the calling process is not altered by the attach.
54       The segment will automatically be detached at process exit.   The  same
55       segment  may  be  attached  as a read and as a read-write one, and more
56       than once, in the process's address space.
57
58       A successful shmat() call updates the members of the shmid_ds structure
59       (see shmctl(2)) associated with the shared memory segment as follows:
60
61              shm_atime is set to the current time.
62
63              shm_lpid is set to the process-ID of the calling process.
64
65              shm_nattch is incremented by one.
66
67   shmdt()
68       shmdt() detaches the shared memory segment located at the address spec‐
69       ified by shmaddr from the address space of the  calling  process.   The
70       to-be-detached segment must be currently attached with shmaddr equal to
71       the value returned by the attaching shmat() call.
72
73       On a successful shmdt() call, the system updates  the  members  of  the
74       shmid_ds  structure  associated  with the shared memory segment as fol‐
75       lows:
76
77              shm_dtime is set to the current time.
78
79              shm_lpid is set to the process-ID of the calling process.
80
81              shm_nattch is decremented by one.  If it becomes 0 and the  seg‐
82              ment is marked for deletion, the segment is deleted.
83

RETURN VALUE

85       On  success,  shmat() returns the address of the attached shared memory
86       segment; on error, (void *) -1 is returned, and errno is set  to  indi‐
87       cate the cause of the error.
88
89       On  success,  shmdt()  returns 0; on error -1 is returned, and errno is
90       set to indicate the cause of the error.
91

ERRORS

93       When shmat() fails, errno is set to one of the following:
94
95       EACCES The calling process does not have the required  permissions  for
96              the  requested  attach type, and does not have the CAP_IPC_OWNER
97              capability in the user namespace that governs its IPC namespace.
98
99       EIDRM  shmid points to a removed identifier.
100
101       EINVAL Invalid shmid  value,  unaligned  (i.e.,  not  page-aligned  and
102              SHM_RND  was  not  specified) or invalid shmaddr value, or can't
103              attach segment  at  shmaddr,  or  SHM_REMAP  was  specified  and
104              shmaddr was NULL.
105
106       ENOMEM Could  not  allocate  memory  for the descriptor or for the page
107              tables.
108
109       When shmdt() fails, errno is set as follows:
110
111       EINVAL There is no shared  memory  segment  attached  at  shmaddr;  or,
112              shmaddr is not aligned on a page boundary.
113

CONFORMING TO

115       POSIX.1-2001, POSIX.1-2008, SVr4.
116
117       In  SVID  3  (or perhaps earlier), the type of the shmaddr argument was
118       changed from char * into const void *, and the returned type of shmat()
119       from char * into void *.
120

NOTES

122       After  a  fork(2),  the  child inherits the attached shared memory seg‐
123       ments.
124
125       After an execve(2), all attached shared memory  segments  are  detached
126       from the process.
127
128       Upon  _exit(2),  all  attached shared memory segments are detached from
129       the process.
130
131       Using shmat() with shmaddr equal to NULL is the preferred, portable way
132       of  attaching a shared memory segment.  Be aware that the shared memory
133       segment attached in this way may be attached at different addresses  in
134       different  processes.   Therefore,  any  pointers maintained within the
135       shared memory must be made relative (typically to the starting  address
136       of the segment), rather than absolute.
137
138       On  Linux,  it is possible to attach a shared memory segment even if it
139       is already marked to be deleted.  However,  POSIX.1  does  not  specify
140       this behavior and many other implementations do not support it.
141
142       The following system parameter affects shmat():
143
144       SHMLBA Segment low boundary address multiple.  When explicitly specify‐
145              ing an attach address in a call to shmat(),  the  caller  should
146              ensure  that  the  address is a multiple of this value.  This is
147              necessary on some architectures, in order either to ensure  good
148              CPU  cache  performance  or to ensure that different attaches of
149              the same segment have consistent views  within  the  CPU  cache.
150              SHMLBA  is  normally some multiple of the system page size.  (On
151              many Linux architectures, SHMLBA is the same as the system  page
152              size.)
153
154       The  implementation places no intrinsic per-process limit on the number
155       of shared memory segments (SHMSEG).
156

SEE ALSO

158       brk(2),  mmap(2),  shmctl(2),  shmget(2),  capabilities(7),   shm_over‐
159       view(7), svipc(7)
160

COLOPHON

162       This  page  is  part of release 4.16 of the Linux man-pages project.  A
163       description of the project, information about reporting bugs,  and  the
164       latest     version     of     this    page,    can    be    found    at
165       https://www.kernel.org/doc/man-pages/.
166
167
168
169Linux                             2017-09-15                          SHMOP(2)
Impressum