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

NAME

6       shmat, shmdt - 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()  attaches  the shared memory segment identified by shmid to the
18       address space of the calling process.  The attaching address is  speci‐
19       fied by shmaddr with one of the following criteria:
20
21       If  shmaddr  is NULL, the system chooses a suitable (unused) address at
22       which to attach the segment.
23
24       If shmaddr isn't NULL and SHM_RND is specified in  shmflg,  the  attach
25       occurs at the address equal to shmaddr rounded down to the nearest mul‐
26       tiple of SHMLBA.  Otherwise shmaddr must be a page-aligned  address  at
27       which the attach occurs.
28
29       If SHM_RDONLY is specified in shmflg, the segment is attached for read‐
30       ing and the process must have read permission for the segment.   Other‐
31       wise  the  segment  is attached for read and write and the process must
32       have read and write permission for the segment.  There is no notion  of
33       a write-only shared memory segment.
34
35       The (Linux-specific) SHM_REMAP flag may be specified in shmflg to indi‐
36       cate that the mapping of the segment should replace any  existing  map‐
37       ping  in  the  range starting at shmaddr and continuing for the size of
38       the segment.  (Normally an EINVAL  error  would  result  if  a  mapping
39       already  exists in this address range.)  In this case, shmaddr must not
40       be NULL.
41
42       The brk(2) value of the calling process is not altered by  the  attach.
43       The  segment  will automatically be detached at process exit.  The same
44       segment may be attached as a read and as a  read-write  one,  and  more
45       than once, in the process's address space.
46
47       A successful shmat() call updates the members of the shmid_ds structure
48       (see shmctl(2)) associated with the shared memory segment as follows:
49
50              shm_atime is set to the current time.
51
52              shm_lpid is set to the process-ID of the calling process.
53
54              shm_nattch is incremented by one.
55
56       shmdt() detaches the shared memory segment located at the address spec‐
57       ified  by  shmaddr  from the address space of the calling process.  The
58       to-be-detached segment must be currently attached with shmaddr equal to
59       the value returned by the attaching shmat() call.
60
61       On  a  successful  shmdt()  call  the system updates the members of the
62       shmid_ds structure associated with the shared memory  segment  as  fol‐
63       lows:
64
65              shm_dtime is set to the current time.
66
67              shm_lpid is set to the process-ID of the calling process.
68
69              shm_nattch  is decremented by one.  If it becomes 0 and the seg‐
70              ment is marked for deletion, the segment is deleted.
71
72       After a fork(2) the child inherits the attached shared memory segments.
73
74       After an execve(2) all attached shared  memory  segments  are  detached
75       from the process.
76
77       Upon _exit(2) all attached shared memory segments are detached from the
78       process.
79

RETURN VALUE

81       On success shmat() returns the address of the  attached  shared  memory
82       segment; on error (void *) -1 is returned, and errno is set to indicate
83       the cause of the error.
84
85       On success shmdt() returns 0; on error -1 is returned, and errno is set
86       to indicate the cause of the error.
87

ERRORS

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

CONFORMING TO

109       SVr4, POSIX.1-2001.
110
111       In SVID 3 (or perhaps earlier) the type of  the  shmaddr  argument  was
112       changed from char * into const void *, and the returned type of shmat()
113       from char * into void *.  (Linux libc4 and libc5 have the char * proto‐
114       types; glibc2 has void *.)
115

NOTES

117       Using shmat() with shmaddr equal to NULL is the preferred, portable way
118       of attaching a shared memory segment.  Be aware that the shared  memory
119       segment  attached in this way may be attached at different addresses in
120       different processes.  Therefore, any  pointers  maintained  within  the
121       shared  memory must be made relative (typically to the starting address
122       of the segment), rather than absolute.
123
124       On Linux, it is possible to attach a shared memory segment even  if  it
125       is  already marked to be deleted.  However, POSIX.1-2001 does not spec‐
126       ify this behavior and many other implementations do not support it.
127
128       The following system parameter affects shmat():
129
130       SHMLBA Segment low boundary address multiple.  Must  be  page  aligned.
131              For the current implementation the SHMLBA value is PAGE_SIZE.
132
133       The implementation places no intrinsic limit on the per-process maximum
134       number of shared memory segments (SHMSEG).
135

SEE ALSO

137       brk(2),  mmap(2),  shmctl(2),  shmget(2),  capabilities(7),   shm_over‐
138       view(7), svipc(7)
139

COLOPHON

141       This  page  is  part of release 3.25 of the Linux man-pages project.  A
142       description of the project, and information about reporting  bugs,  can
143       be found at http://www.kernel.org/doc/man-pages/.
144
145
146
147Linux                             2008-06-03                          SHMOP(2)
Impressum