1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) 2002 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" FIXME . Add an example to this page
27 .\" Japanese Version Copyright (c) 2002 Akihiro MOTOKI, all rights reserved.
28 .\" Translated Mon Feb 2 2003 by Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
29 .\" Updated 2009-02-23, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.19
31 .TH SHM_OPEN 3 2009-02-25 "Linux" "Linux Programmer's Manual"
34 .\"O shm_open, shm_unlink \- Create/open or unlink POSIX shared memory objects
35 shm_open, shm_unlink \- POSIX ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤ÎºîÀ®/¥ª¡¼¥×¥ó¡¢
39 .B #include <sys/mman.h>
41 .\"O .BR "#include <sys/stat.h>" " /* For mode constants */"
42 .BR "#include <sys/stat.h>" " /* mode Äê¿ôÍÑ */"
44 .\"O .BR "#include <fcntl.h>" " /* For O_* constants */"
45 .BR "#include <fcntl.h>" " /* O_* Äê¿ô¤ÎÄêµÁÍÑ */"
47 .BI "void * shm_open(const char *" name ", int " oflag ", mode_t " mode );
49 .BI "int shm_unlink(const char *" name );
51 .\"O Link with \fI\-lrt\fP.
52 \fI\-lrt\fP ¤Ç¥ê¥ó¥¯¤¹¤ë¡£
56 .\"O creates and opens a new, or opens an existing, POSIX shared memory object.
57 .\"O A POSIX shared memory object is in effect a handle which can
58 .\"O be used by unrelated processes to
60 .\"O the same region of shared memory.
62 ¤Ï¡¢POSIX ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤ò¿·µ¬¤ËºîÀ®/¥ª¡¼¥×¥ó¤·¤¿¤ê¡¢
63 ¤¹¤Ç¤Ë¸ºß¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤ò¥ª¡¼¥×¥ó¤·¤¿¤ê¤¹¤ë¡£
64 POSIX ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤Ï¡¢¼ÂºÝ¤Ë¤Ï¡¢´Ø·¸¤Î¤Ê¤¤¥×¥í¥»¥¹¤¬
65 ¶¦Í¥á¥â¥ê¤ÎƱ¤¸Îΰè¤ò
67 ¤¹¤ë¤¿¤á¤Ë»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤ë¼êÃʤǤ¢¤ë¡£
69 .\"O .BR shm_unlink ()
70 .\"O function performs the converse operation,
71 .\"O removing an object previously created by
74 ¤Ï¡¢µÕ¤ÎÁàºî¡¢¤Ä¤Þ¤ê°ÊÁ°¤Ë
76 ¤ÇºîÀ®¤µ¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤Îºï½ü¤ò¹Ô¤¦¡£
80 .\"O is analogous to that of
87 .\"O specifies the shared memory object to be created or opened.
88 .\"O For portable use,
89 .\"O a shared memory object should be identified by a name of the form
91 .\"O that is, a null-terminated string of up to
93 .\"O (i.e., 255) characters consisting of an initial slash,
95 ¤ÇºîÀ®¤·¤¿¤ê¥ª¡¼¥×¥ó¤·¤¿¤ê¤¹¤ë¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤ò»ØÄꤹ¤ë¡£
96 °Ü¿¢À¤ò»ý¤¿¤»¤ë¤¿¤á¤Ë¤Ï¡¢¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤Ï
98 ¤È¤¤¤¦·Á¼°¤Î̾Á°¤Ç¼±Ê̤·¡¢
101 (¤¹¤Ê¤ï¤Á 255) ʸ»ú¤Î NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ç¡¢
102 .\" glibc allows the initial slash to be omitted, and makes
103 .\" multiple initial slashes equivalent to a single slash.
104 .\" This differs from the implementation of POSIX message queues.
105 .\"O followed by one or more characters, none of which are slashes.
106 ¥¹¥é¥Ã¥·¥å¤Ç»Ï¤Þ¤ê¡¢¥¹¥é¥Ã¥·¥å°Ê³°¤Îʸ»ú¤¬ 1 ʸ»ú°Ê¾å³¤¯·Á¼°
108 .\" glibc allows subdirectory components in the name, in which
109 .\" case the subdirectory must exist under /dev/shm, and allow the
110 .\" required permissions if a user wants to create a shared memory
111 .\" object in that subdirectory.
114 .\"O is a bit mask created by ORing together exactly one of
118 .\"O and any of the other flags listed here:
124 ¤Î¤¤¤º¤ì¤«°ìÊý¤È¡¢°Ê²¼¤Ë½Ò¤Ù¤ë¾¤Î¥Õ¥é¥°¤ÎÏÀÍýϤò¤È¤Ã¤¿¤â¤Î
128 .\"O Open the object for read access.
129 .\"O A shared memory object opened in this way can only be
131 .\"O for read (\fBPROT_READ\fP) access.
132 Æɤ߽Ф·¥¢¥¯¥»¥¹ÍѤ˥ª¥Ö¥¸¥§¥¯¥È¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£
133 ¤³¤Î¥Õ¥é¥°¤ò»ØÄꤷ¤Æ¥ª¡¼¥×¥ó¤µ¤ì¤¿¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤Ï¡¢
134 Æɤ߽Ф· (\fBPROT_READ\fP) ¥¢¥¯¥»¥¹¤Ç¤Î¤ß
139 .\"O Open the object for read-write access.
140 Æɤ߽ñ¤¥¢¥¯¥»¥¹ÍѤ˥ª¥Ö¥¸¥§¥¯¥È¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£
143 .\"O Create the shared memory object if it does not exist.
144 ¸ºß¤·¤Ê¤¤¾ì¹ç¡¢¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤òºîÀ®¤¹¤ë¡£
145 .\"O The user and group ownership of the object are taken
146 .\"O from the corresponding effective IDs of the calling process,
147 .\"O .\" In truth it is actually the file system IDs on Linux, but these
148 .\"O .\" are nearly always the same as the effective IDs. (MTK, Jul 05)
149 .\"O and the object's
150 .\"O permission bits are set according to the low-order 9 bits of
152 .\"O except that those bits set in the process file mode
153 .\"O creation mask (see
155 .\"O are cleared for the new object.
156 ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥æ¡¼¥¶¤È¥°¥ë¡¼¥×¤Î½ê͸¢¤Ï¡¢
157 ¸Æ¤Ó½Ð¤·¸µ¥×¥í¥»¥¹¤ÎÂбþ¤¹¤ë¼Â¸ú ID ¤¬»È¤ï¤ì¡¢
158 .\" ËÜÅö¤Ï¡¢Linux ¤Ç¤Ï¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à ID ¤¬»È¤ï¤ì¤ë¤Î¤À¤¬¡¢
159 .\" ¤½¤ÎÃͤϤۤȤó¤É¤¤¤Ä¤â¼Â¸ú ID ¤ÈƱ¤¸¤Ç¤¢¤ë¡£ (MTK, Jul 05)
160 ¥ª¥Ö¥¸¥§¥¯¥È¤Îµö²Ä¥Ó¥Ã¥È¤Ï
162 ¤Î²¼°Ì 9 ¥Ó¥Ã¥È¤Ë´ð¤Å¤¤¤ÆÀßÄꤵ¤ì¤ë¡£¤¿¤À¤·¡¢
163 ¥Õ¥¡¥¤¥ë¥â¡¼¥ÉºîÀ®¥Þ¥¹¥¯
165 »²¾È) ¤ËÀßÄꤵ¤ì¤Æ¤¤¤ëÃͤϡ¢¿·µ¬¥ª¥Ö¥¸¥§¥¯¥È¤Ë´Ø¤·¤Æ¤Ï¥¯¥ê¥¢¤µ¤ì¤ë¡£
166 .\"O A set of macro constants which can be used to define
170 .\"O (Symbolic definitions of these constants can be obtained by including
171 .\"O .IR <sys/stat.h> .)
173 ¤òÄêµÁ¤¹¤ë¤¿¤á¤Ë»ÈÍѤǤ¤ë¥Þ¥¯¥íÄê¿ô(·²)¤Ï
176 (¤³¤ì¤é¤ÎÄê¿ô¤Î¥·¥ó¥Ü¥ëÄêµÁ¤Ï
178 ¤Î¥¤¥ó¥¯¥ë¡¼¥É¤Ë¤è¤êÆÀ¤é¤ì¤ë)¡£
180 .\"O A new shared memory object initially has zero length \(em the size of the
181 .\"O object can be set using
182 .\"O .BR ftruncate (2).
183 .\"O The newly-allocated bytes of a shared memory
184 .\"O object are automatically initialized to 0.
185 ¿·µ¬¤ËºîÀ®¤µ¤ì¤¿¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤ÏŤµ 0 ¤Ç½é´ü²½¤µ¤ì¤ë¡£
186 ¥ª¥Ö¥¸¥§¥¯¥È¤ÎÂ礤µ¤Ï
189 ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤È¤·¤Æ¿·µ¬¤Ë³ÎÊݤµ¤ì¤¿¥Ð¥¤¥È¤Ï¼«Æ°Åª¤Ë
195 .\"O was also specified, and a shared memory object with the given
197 .\"O already exists, return an error.
198 .\"O The check for the existence of the object, and its creation if it
199 .\"O does not exist, are performed atomically.
201 ¤¬°ì½ï¤Ë»ØÄꤵ¤ì¤Æ¤ª¤ê¡¢
203 ¤Ç»ØÄꤵ¤ì¤¿¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤¬´û¤Ë¸ºß¤·¤¿¾ì¹ç¡¢
205 ¥ª¥Ö¥¸¥§¥¯¥È¤Î¸ºß³Îǧ¤È¡¢Â¸ºß¤·¤Ê¤«¤Ã¤¿¾ì¹ç¤Î¥ª¥Ö¥¸¥§¥¯¥ÈºîÀ®¤Ï¡¢
206 ɬ¤º°ìÏ¢¤ÎÁàºî¤È¤·¤Æ¼Â¹Ô¤µ¤ì¤ë (performed atomically)¡£
209 .\"O If the shared memory object already exists, truncate it to zero bytes.
210 ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤¬¤¹¤Ç¤Ë¸ºß¤·¤¿¾ì¹ç¡¢
211 ¤½¤Î¥ª¥Ö¥¸¥§¥¯¥È¤ò 0 ¥Ð¥¤¥È¤ËÀÚ¤êµÍ¤á¤ë¡£
213 .\"O Definitions of these flag values can be obtained by including
215 ¤³¤ì¤é¤Î¥Õ¥é¥°ÃͤÎÄêµÁ¤Ï
217 ¤Î¥¤¥ó¥¯¥ë¡¼¥É¤Ë¤è¤êÆÀ¤é¤ì¤ë¡£
219 .\"O On successful completion
221 .\"O returns a new file descriptor referring to the shared memory object.
222 .\"O This file descriptor is guaranteed to be the lowest-numbered file descriptor
223 .\"O not previously opened within the process.
228 .\"O is set for the file descriptor.
229 À®¸ù¤·¤Æ´°Î»¤·¤¿¾ì¹ç¡¢
231 ¤Ï¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤ò»²¾È¤¹¤ë¿·¤·¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊÖ¤¹¡£
232 ¤³¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¡¢¤½¤Î¥×¥í¥»¥¹Æâ¤Ç²áµî¤Ë¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤Ê¤¤
233 ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÃæ¤ÇºÇ¤â¾®¤µ¤Ê¿ô¤Ë¤Ê¤ë¤³¤È¤¬Êݾڤµ¤ì¤ë¡£
237 ¤ò»²¾È) ¤¬¡¢¤³¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÀßÄꤵ¤ì¤ë¡£
239 .\"O The file descriptor is normally used in subsequent calls
241 .\"O .BR ftruncate (2)
242 .\"O (for a newly created object) and
246 .\"O the file descriptor may be closed without affecting the memory mapping.
247 Ä̾¤³¤ì¤é¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¡¢¤³¤Î¸å³¤±¤Æ¼Â¹Ô¤µ¤ì¤ë
249 (¿·µ¬¤ËºîÀ®¤µ¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤Î¾ì¹ç¤Î¤ß) ¤È
251 ¤Î¸Æ¤Ó½Ð¤·¤Ë»ÈÍѤµ¤ì¤ë¡£
253 ¤ò¸Æ¤Ó½Ð¤·¤¿¸å¤Ï¡¢¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¥¯¥í¡¼¥º¤·¤Æ¤â¤è¤¯¡¢
254 ¥¯¥í¡¼¥º¤·¤Æ¤â¥á¥â¥ê¡¦¥Þ¥Ã¥Ô¥ó¥°¤Ë±Æ¶Á¤òÍ¿¤¨¤ë¤³¤È¤Ï¤Ê¤¤¡£
258 .\"O .BR shm_unlink ()
261 .\"O it removes a shared memory object name, and, once all processes
262 .\"O have unmapped the object, de-allocates and
263 .\"O destroys the contents of the associated memory region.
268 ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È̾¤òºï½ü¤·¡¢¤¹¤Ù¤Æ¤Î¥×¥í¥»¥¹¤¬½èÍýÂоݤÎ
269 ¥ª¥Ö¥¸¥§¥¯¥È¤ò¥¢¥ó¥Þ¥Ã¥×¤·¤¿»þÅÀ¤Ç¥ª¥Ö¥¸¥§¥¯¥È¤Î³ä¤êÅö¤Æ¤ò²ò½ü¤·¡¢
270 Âбþ¤¹¤ë¥á¥â¥êÎΰè¤ÎÆâÍƤòÇË´þ¤¹¤ë¡£
271 .\"O After a successful
272 .\"O .BR shm_unlink (),
275 .\"O an object with the same
277 .\"O will fail (unless
279 .\"O was specified, in which case a new, distinct object is created).
283 ¤ò»ý¤Ä¥ª¥Ö¥¸¥§¥¯¥È¤ËÂФ·¤Æ
287 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç) ¼ºÇÔ¤¹¤ë¡£
289 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢¿·¤·¤¯Ê̤Υª¥Ö¥¸¥§¥¯¥È¤¬ºîÀ®¤µ¤ì¤ë)¡£
290 .\"O .SH "RETURN VALUE"
294 .\"O returns a nonnegative file descriptor.
298 .\"O .BR shm_unlink ()
299 .\"O returns 0 on success, or \-1 on error.
302 ¤ÏÈóÉé¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊÖ¤¹¡£
307 ¤Ï¡¢À®¸ù¤·¤¿¾ì¹ç 0 ¤ò¡¢¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿¾ì¹ç \-1 ¤òÊÖ¤¹¡£
312 .\"O is set to indicate the cause of the error.
313 .\"O Values which may appear in
315 .\"O include the following:
316 ¼ºÇÔ¤·¤¿¾ì¹ç¡¢¥¨¥é¡¼¤Î¸¶°ø¤ò¼¨¤¹¤¿¤á
320 ¤ËÀßÄꤵ¤ì¤ëÃͤϰʲ¼¤ÎÄ̤ê¤Ç¤¢¤ë:
324 .\"O .BR shm_unlink ()
325 .\"O the shared memory object was denied.
326 ¶¦Í¥á¥â¥ê¥ª¥Ö¥¸¥§¥¯¥È¤ò
331 .\"O Permission was denied to
334 .\"O in the specified
338 .\"O was specified and the caller does not have write permission on the object.
345 ¤¹¤ë¸¢¸Â¤¬¤Ê¤«¤Ã¤¿¡£¤â¤·¤¯¤Ï¡¢
347 ¤¬»ØÄꤵ¤ì¤¿¤¬¡¢¸Æ¤Ó½Ð¤·¸µ¤Ë¤Ï¤½¤Î¥ª¥Ö¥¸¥§¥¯¥È¤ËÂФ¹¤ë½ñ¤¹þ¤ß¸¢¸Â¤¬
355 .\"O were specified to
357 .\"O and the shared memory object specified by
367 ¤Ç»ØÄꤵ¤ì¤¿¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤¬´û¤Ë¸ºß¤·¤¿¡£
378 °ú¤¿ô¤¬ÉÔÀµ¤Ç¤¢¤Ã¤¿¡£
381 .\"O The process already has the maximum number of files open.
382 ¥×¥í¥»¥¹¤¬¥ª¡¼¥×¥ó²Äǽ¤Ê¥Õ¥¡¥¤¥ë¿ô¤Î¾å¸Â¤Ë¤¹¤Ç¤Ë㤷¤Æ¤¤¤¿¡£
395 .\"O The limit on the total number of files open on the system has been
397 ¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤Ç¥ª¡¼¥×¥ó¤Ç¤¤ë¥Õ¥¡¥¤¥ë¿ô¤Î¾å¸Â¤Ë㤷¤¿¡£
400 .\"O An attempt was made to
404 .\"O that did not exist, and
406 .\"O was not specified.
413 ¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤«¤Ã¤¿¡£
416 .\"O An attempt was to made to
417 .\"O .BR shm_unlink ()
420 .\"O that does not exist.
428 .\"O These functions are provided in glibc 2.2 and later.
429 ¤³¤ì¤é¤Î´Ø¿ô¤Ï glibc 2.2 °Ê¹ß¤ÇÄ󶡤µ¤ì¤Æ¤¤¤ë¡£
430 .\"O .SH "CONFORMING TO"
434 .\"O POSIX.1-2001 says that the group ownership of a newly created shared
435 .\"O memory object is set to either the calling process's effective group ID
436 .\"O or "a system default group ID".
437 POSIX.1-2001 ¤Ë¤Ï¡¢¿·µ¬¤ËºîÀ®¤µ¤ì¤ë¶¦Í¥á¥â¥ê¥ª¥Ö¥¸¥§¥¯¥È¤Î¥°¥ë¡¼¥×½ê͸¢¤Ï¡¢
438 ¸Æ¤Ó½Ð¤·¸µ¥×¥í¥»¥¹¤Î¼Â¸ú¥°¥ë¡¼¥× ID ¤« ¡Ö¥·¥¹¥Æ¥à¤Î¥Ç¥Õ¥©¥ë¥È¤Î¥°¥ë¡¼¥× ID¡×
439 ¤Î¤É¤Á¤é¤«¤ËÀßÄꤵ¤ì¤ë¡¢¤È½ñ¤«¤ì¤Æ¤¤¤ë¡£
443 .\"O POSIX leaves the behavior of the combination of
448 .\"O On Linux, this will successfully truncate an existing
449 .\"O shared memory object \(em this may not be so on other Unix systems.
454 ¤¬°ì½ï¤Ë»ØÄꤵ¤ì¤¿¾ì¹ç¤ÎÆ°ºî¤ò̤ÄêµÁ¤Ë¤·¤Æ¤¤¤ë¡£Linux ¤Ç¤Ï¡¢
455 ´û¸¤Î¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤ËÂФ¹¤ëÀÚ¤êµÍ¤á (truncate) ¤ÏÀ®¸ù¤¹¤ë¡£
456 ¤·¤«¤·¡¢Â¾¤Î Unix ¥·¥¹¥Æ¥à¤Ç¤âƱ¤¸¤Ç¤¢¤ë¤È¤Ï¸Â¤é¤Ê¤¤¡£
458 .\"O The POSIX shared memory object implementation on Linux 2.4 makes use
459 .\"O of a dedicated file system, which is normally
462 Linux 2.4 ¤Ë¤ª¤±¤ë POSIX ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È¤Î¼ÂÁõ¤Ï
463 ÀìÍѤΥե¡¥¤¥ë¥·¥¹¥Æ¥à¤ò»ÈÍѤ¹¤ë¡£¤½¤Î¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤ÏÄ̾ï