1SHMGET(2) Linux Programmer's Manual SHMGET(2)
2
3
4
6 shmget - allocates a shared memory segment
7
9 #include <sys/ipc.h>
10 #include <sys/shm.h>
11
12 int shmget(key_t key, size_t size, int shmflg);
13
15 shmget() returns the identifier of the shared memory segment associated
16 with the value of the argument key. A new shared memory segment, with
17 size equal to the value of size rounded up to a multiple of PAGE_SIZE,
18 is created if key has the value IPC_PRIVATE or key isn't IPC_PRIVATE,
19 no shared memory segment corresponding to key exists, and IPC_CREAT is
20 specified in shmflg.
21
22 If shmflg specifies both IPC_CREAT and IPC_EXCL and a shared memory
23 segment already exists for key, then shmget() fails with errno set to
24 EEXIST. (This is analogous to the effect of the combination O_CREAT |
25 O_EXCL for open(2).)
26
27 The value shmflg is composed of:
28
29 IPC_CREAT to create a new segment. If this flag is not used, then
30 shmget() will find the segment associated with key and
31 check to see if the user has permission to access the seg‐
32 ment.
33
34 IPC_EXCL used with IPC_CREAT to ensure failure if the segment
35 already exists.
36
37 mode_flags (least significant 9 bits) specifying the permissions
38 granted to the owner, group, and world. These bits have
39 the same format, and the same meaning, as the mode argument
40 of open(2). Presently, the execute permissions are not
41 used by the system.
42
43 SHM_HUGETLB (since Linux 2.6)
44 Allocate the segment using "huge pages." See the kernel
45 source file Documentation/vm/hugetlbpage.txt for further
46 information.
47
48 SHM_NORESERVE (since Linux 2.6.15)
49 This flag serves the same purpose as the mmap(2) MAP_NORE‐
50 SERVE flag. Do not reserve swap space for this segment.
51 When swap space is reserved, one has the guarantee that it
52 is possible to modify the segment. When swap space is not
53 reserved one might get SIGSEGV upon a write if no physical
54 memory is available. See also the discussion of the file
55 /proc/sys/vm/overcommit_memory in proc(5).
56
57 When a new shared memory segment is created, its contents are initial‐
58 ized to zero values, and its associated data structure, shmid_ds (see
59 shmctl(2)), is initialized as follows:
60
61 shm_perm.cuid and shm_perm.uid are set to the effective user ID
62 of the calling process.
63
64 shm_perm.cgid and shm_perm.gid are set to the effective group ID
65 of the calling process.
66
67 The least significant 9 bits of shm_perm.mode are set to the
68 least significant 9 bit of shmflg.
69
70 shm_segsz is set to the value of size.
71
72 shm_lpid, shm_nattch, shm_atime and shm_dtime are set to 0.
73
74 shm_ctime is set to the current time.
75
76 If the shared memory segment already exists, the permissions are veri‐
77 fied, and a check is made to see if it is marked for destruction.
78
80 A valid segment identifier, shmid, is returned on success, -1 on error.
81
83 On failure, errno is set to one of the following:
84
85 EACCES The user does not have permission to access the shared memory
86 segment, and does not have the CAP_IPC_OWNER capability.
87
88 EEXIST IPC_CREAT | IPC_EXCL was specified and the segment exists.
89
90 EINVAL A new segment was to be created and size < SHMMIN or size > SHM‐
91 MAX, or no new segment was to be created, a segment with given
92 key existed, but size is greater than the size of that segment.
93
94 ENFILE The system limit on the total number of open files has been
95 reached.
96
97 ENOENT No segment exists for the given key, and IPC_CREAT was not spec‐
98 ified.
99
100 ENOMEM No memory could be allocated for segment overhead.
101
102 ENOSPC All possible shared memory IDs have been taken (SHMMNI), or
103 allocating a segment of the requested size would cause the sys‐
104 tem to exceed the system-wide limit on shared memory (SHMALL).
105
106 EPERM The SHM_HUGETLB flag was specified, but the caller was not priv‐
107 ileged (did not have the CAP_IPC_LOCK capability).
108
110 SVr4, POSIX.1-2001.
111
112 SHM_HUGETLB is a nonportable Linux extension.
113
115 IPC_PRIVATE isn't a flag field but a key_t type. If this special value
116 is used for key, the system call ignores everything but the least sig‐
117 nificant 9 bits of shmflg and creates a new shared memory segment (on
118 success).
119
120 The following limits on shared memory segment resources affect the
121 shmget() call:
122
123 SHMALL System wide maximum of shared memory pages (on Linux, this limit
124 can be read and modified via /proc/sys/kernel/shmall).
125
126 SHMMAX Maximum size in bytes for a shared memory segment: policy depen‐
127 dent (on Linux, this limit can be read and modified via
128 /proc/sys/kernel/shmmax).
129
130 SHMMIN Minimum size in bytes for a shared memory segment: implementa‐
131 tion dependent (currently 1 byte, though PAGE_SIZE is the effec‐
132 tive minimum size).
133
134 SHMMNI System wide maximum number of shared memory segments: implemen‐
135 tation dependent (currently 4096, was 128 before Linux 2.3.99;
136 on Linux, this limit can be read and modified via /proc/sys/ker‐
137 nel/shmmni).
138
139 The implementation has no specific limits for the per-process maximum
140 number of shared memory segments (SHMSEG).
141
142 Linux Notes
143 Until version 2.3.30 Linux would return EIDRM for a shmget() on a
144 shared memory segment scheduled for deletion.
145
147 The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
148 clearly show its function.
149
151 shmat(2), shmctl(2), shmdt(2), ftok(3), capabilities(7), shm_over‐
152 view(7), svipc(7)
153
155 This page is part of release 3.25 of the Linux man-pages project. A
156 description of the project, and information about reporting bugs, can
157 be found at http://www.kernel.org/doc/man-pages/.
158
159
160
161Linux 2006-05-02 SHMGET(2)