1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl), 1 Nov 1999
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" 1999-11-10: Merged text taken from the page contributed by
24 .\" Reed H. Petty (rhp@draper.net)
26 .\" Japanese Version Copyright (c) 1999 HANATAKA Shinya
27 .\" all rights reserved.
28 .\" Translated 1999-12-04, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
29 .\" Updated 2007-01-09, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.43
30 .\" Updated 2008-11-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.13
32 .TH VFORK 2 2010-09-20 "Linux" "Linux Programmer's Manual"
35 .\"O vfork \- create a child process and block parent
36 vfork \- »Ò¥×¥í¥»¥¹¤òÀ¸À®¤·¿Æ¥×¥í¥»¥¹¤òÄä»ß¤µ¤»¤ë
39 .B #include <sys/types.h>
41 .B #include <unistd.h>
46 .\"O Feature Test Macro Requirements for glibc (see
47 .\"O .BR feature_test_macros (7)):
48 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
49 .RB ( feature_test_macros (7)
58 .\"O Since glibc 2.12:
62 (_XOPEN_SOURCE\ >=\ 500 ||
63 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
64 !(_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700)
67 .\"O Before glibc 2.12:
69 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
70 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
76 .\"O .SS "Standard Description"
82 .\"O function has the same effect as
84 .\"O except that the behavior is undefined if the process created by
86 .\"O either modifies any data other than a variable of type
88 .\"O used to store the return value from
90 .\"O or returns from the function in which
92 .\"O was called, or calls any other function before successfully calling
96 .\"O family of functions.
102 ¤ÇºîÀ®¤µ¤ì¤¿¥×¥í¥»¥¹¤¬
104 ¤«¤é¤ÎÊÖ¤êÃͤò³ÊǼ¤·¤Æ¤¤¤ë
106 ·¿¤ÎÊÑ¿ô°Ê³°¤òÊѹ¹¤·¤¿¤ê¡¢
108 ¤ò¸Æ¤Ó½Ð¤·¤Æ¤¤¤ë´Ø¿ô¤«¤é return ¤·¤¿¤ê¡¢
112 ²¤Î´Ø¿ô¤ò¥³¡¼¥ë¤¹¤ëÁ°¤Ë¾¤Î´Ø¿ô¤ò¥³¡¼¥ë¤·¤¿¾ì¹ç¤ÎÆ°ºî¤¬
113 ̤ÄêµÁ¤Ç¤¢¤ë¤È¤¤¤¦ÅÀ¤¬°Û¤Ê¤ë¡£
114 .\"O .SS "Linux Description"
119 .\"O creates a child process of the calling process.
120 .\"O For details and return value and errors, see
125 ¤ÈÁ´¤¯Æ±¤¸¤è¤¦¤Ë¸Æ¤Ó½Ð¤·¤¿¥×¥í¥»¥¹¤Î»Ò¥×¥í¥»¥¹¤òÀ¸À®¤¹¤ë¡£
126 ¾Ü¤·¤¤ÀâÌÀ¤ÈÊÖ¤êÃÍ¡¢¥¨¥é¡¼¤Ë¤Ä¤¤¤Æ¤Ï
131 .\"O is a special case of
133 .\"O It is used to create new processes without copying the page tables of
134 .\"O the parent process.
135 .\"O It may be useful in performance-sensitive applications
136 .\"O where a child is created which then immediately issues an
142 ¿Æ¥×¥í¥»¥¹¤Î¥Ú¡¼¥¸¥Æ¡¼¥Ö¥ë¤Î¥³¥Ô¡¼¤ò¹Ô¤ï¤º¤Ë¿·¤·¤¤¥×¥í¥»¥¹¤ò
143 ºîÀ®¤¹¤ë¤¿¤á¤Ë»ÈÍѤ¹¤ë¡£¤³¤ì¤ÏÀǽ¤ËÉÒ´¶¤Ê¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ë¤ª¤¤¤Æ
144 »Ò¥×¥í¥»¥¹¤òÀ¸À®¤·¤Æ¤¹¤°¤Ë
146 ¤¹¤ë¾ì¹ç¤ËÍÍѤ«¤â¤·¤ì¤Ê¤¤¡£
151 .\"O in that the parent is suspended until the child terminates
152 .\"O (either normally,
155 .\"O or abnormally, after delivery of a fatal signal),
156 .\"O or it makes a call to
161 ¤È°ã¤¤¡¢»Ò¥×¥í¥»¥¹¤¬½ªÎ»¤¹¤ë¤«¡¢
163 ¤ò¥³¡¼¥ë¤¹¤ë¤Þ¤Ç¿Æ¥×¥í¥»¥¹¤òÄä»ß (suspend) ¤µ¤»¤ë¡£
166 ¤Î¸Æ¤Ó½Ð¤·¤Ë¤è¤ëÄ̾ェλ¡¢
167 Ã×̿Ū¤Ê¥·¥°¥Ê¥ë¤ÎÇÛÁ÷¸å¤Î°Û¾ï½ªÎ»¤ÎÆó¤Ä¤Î¥±¡¼¥¹¤¬¤¢¤ë¡£
168 .\"O Until that point, the child shares all memory with its parent,
169 .\"O including the stack.
170 .\"O The child must not return from the current function or call
174 ¤³¤Î»þÅÀ¤Þ¤Ç¤Ï¡¢»Ò¥×¥í¥»¥¹¤Ï¥¹¥¿¥Ã¥¯¤ò´Þ¤àÁ´¤Æ¤Î¥á¥â¥ê¤ò
175 ¿Æ¥×¥í¥»¥¹¤È¶¦Í¤¹¤ë¡£
176 »Ò¥×¥í¥»¥¹¤Ï¸½ºß¤Î´Ø¿ô¤«¤é return ¤·¤Æ¤Ï¤Ê¤é¤º¡¢
178 ¤â¥³¡¼¥ë¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤¤¬¡¢
180 ¤Ê¤é¤Ð¥³¡¼¥ë¤·¤Æ¤â¤è¤¤¡£
182 .\"O Signal handlers are inherited, but not shared.
183 .\"O Signals to the parent
184 .\"O arrive after the child releases the parent's memory
185 .\"O (i.e., after the child terminates
187 .\"O .BR execve (2)).
188 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ï·Ñ¾µ¤µ¤ì¤ë¤¬¡¢¶¦Í¤Ï¤µ¤ì¤Ê¤¤¡£
189 ¿Æ¥×¥í¥»¥¹¤Ø¤Î¥·¥°¥Ê¥ë¤Ï¡¢»Ò¥×¥í¥»¥¹¤¬¿Æ¥×¥í¥»¥¹¤Î¥á¥â¥ê¤ò
190 ²òÊü¤·¤¿¸å (¤¹¤Ê¤ï¤Á¡¢»Ò¥×¥í¥»¥¹¤¬½ªÎ»¤¹¤ë¤«
192 ¤ò¸Æ¤ó¤À¸å) ¤ËÅþÃ夹¤ë¡£
193 .\"O .SS "Historic Description"
197 .\"O is implemented using copy-on-write pages, so the only penalty incurred by
199 .\"O is the time and memory required to duplicate the parent's page tables,
200 .\"O and to create a unique task structure for the child.
203 ¤Ï½ñ¤¹þ¤ß»þ¥³¥Ô¡¼ (copy-on-write) ¥Ú¡¼¥¸¤ò»ÈÍѤ·¤Æ¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
206 ¤ò»ÈÍѤ¹¤ë¤³¤È¤Ë¤è¤Ã¤ÆÈï¤ë»³²¤Ï¿Æ¥×¥í¥»¥¹¤Î¥Ú¡¼¥¸¡¦¥Æ¡¼¥Ö¥ë¤ò
207 Ê£À½¤¹¤ë¤¿¤á¤ËɬÍפʻþ´Ö¤È¥á¥â¥ê¤À¤±¤Ç¤¢¤ë¡£
208 .\"O However, in the bad old days a
210 .\"O would require making a complete copy of the caller's data space,
211 .\"O often needlessly, since usually immediately afterward an
214 .\"O Thus, for greater efficiency, BSD introduced the
216 .\"O system call, which did not fully copy the address space of
217 .\"O the parent process, but borrowed the parent's memory and thread
218 .\"O of control until a call to
220 .\"O or an exit occurred.
221 .\"O The parent process was suspended while the
222 .\"O child was using its resources.
225 .\"O was tricky: for example, not modifying data
226 .\"O in the parent process depended on knowing which variables were
227 .\"O held in a register.
228 ¤·¤«¤·¤Ê¤¬¤é¡¢´÷¤·¤ÀΤˤÏ
230 ¤Ï¸Æ¤Ó½Ð¤·¤¿¥×¥í¥»¥¹¤Î¥Ç¡¼¥¿¶õ´Ö¤ÎÁ´¤Æ¤Î¥³¥Ô¡¼¤·¤Æ¤¤¤¿¤¬¡¢
231 ¤³¤ì¤Ï¤·¤Ð¤·¤ÐÉÔɬÍפǤ¢¤Ã¤¿¡£¤Ê¤¼¤Ê¤é¡¢¤¿¤¤¤Æ¤¤¤Ï¤¹¤°¸å¤Ë
233 ¤ò¼Â¹Ô¤·¤Æ¤¤¤¿¤«¤é¤Ç¤¢¤ë¡£
234 ¤³¤Î¾ì¹ç¤Î¸úΨ¤ò¾å¤²¤ë¤¿¤á¤Ë BSD ¤Ï
236 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤òƳÆþ¤·¤Æ¿Æ¥×¥í¥»¥¹¤Î¥¢¥É¥ì¥¹¶õ´Ö¤ò´°Á´¤Ë¥³¥Ô¡¼
239 ¤ò¥³¡¼¥ë¤¹¤ë¤« exit ¤¬µ¯¤¤ë¤Þ¤Ç¿Æ¥×¥í¥»¥¹¤Î¥á¥â¥ê¤ÈÀ©¸æ¥¹¥ì¥Ã¥É
241 ¿Æ¥×¥í¥»¥¹¤Ï»Ò¥×¥í¥»¥¹¤¬¤½¤Î»ñ¸»¤ò»ÈÍѤ·¤Æ¤¤¤ë´Ö¤ÏÄä»ß¤µ¤ì¤¿¡£
243 ¤Ï»È¤¤¤Ë¤¯¤¤¤â¤Î¤Ç¤¢¤Ã¤¿: Î㤨¤Ð¡¢¿Æ¥×¥í¥»¥¹¤ÎÊÑ¿ô¤òÊѹ¹¤·¤Ê
244 ¤¤¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¤Ï¤É¤ÎÊÑ¿ô¤¬¥ì¥¸¥¹¥¿¤ËÊÝ»ý¤µ¤ì¤Æ¤¤¤ë¤«¤òÃΤé¤Ê
246 .\"O .SH "CONFORMING TO"
248 4.3BSD, POSIX.1-2001.
249 .\"O POSIX.1-2008 removes the specification of
253 ¤Îµ¬Ä꤬ºï½ü¤µ¤ì¤Æ¤¤¤ë¡£
254 .\"O The requirements put on
256 .\"O by the standards are weaker than those put on
258 .\"O so an implementation where the two are synonymous is compliant.
259 .\"O In particular, the programmer cannot rely on the parent
260 .\"O remaining blocked until the child either terminates or calls
262 .\"O and cannot rely on any specific behavior with respect to shared memory.
264 ¥³¡¼¥ë¤Ï¾¤Î¥ª¥Ú¥ì¡¼¥Æ¥£¥ó¥°¡¦¥·¥¹¥Æ¥à¤ÎƱ̾¤Î¥³¡¼¥ë¤È
265 ¤Á¤ç¤Ã¤È»÷¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£µ¬³Ê¤¬
267 ¤ËÍ׵ᤷ¤Æ¤¤¤ë¤³¤È¤Ï¡¢
269 ¤ËÍ׵ᤷ¤Æ¤¤¤ë¤³¤È¤è¤ê¤Ï¼å¤¤¡£¤·¤¿¤¬¤Ã¤Æ¡¢
270 ξ¼Ô¤òƱ¤¸¤â¤Î¤È¤·¤Æ¼ÂÁõ¤·¤Æ¤â¡¢µ¬³Ê¤Ë½àµò¤·¤Æ¤¤¤ë¤³¤È¤Ë¤Ê¤ë¡£
271 ÆÃ¤Ë¥×¥í¥°¥é¥Þ¡¼¤Ï¡¢»Ò¥×¥í¥»¥¹¤¬½ªÎ»¤¹¤ë¤«
273 ¤ò¸Æ¤Ó½Ð¤¹¤Þ¤Ç¿Æ¥×¥í¥»¥¹¤¬Ää»ß¤·¤Æ¤¤¤ë¤³¤È¤ä¡¢¥á¥â¥ê¤ò¶¦Í¤¹¤ë¤³
274 ¤È¤Ë¤è¤ëÆüì¤ÊÆ°ºî¤ò¤¢¤Æ¤Ë¤¹¤Ù¤¤Ç¤Ï¤Ê¤¤¡£
275 .\" In AIXv3.1 vfork is equivalent to fork.
280 .\"O Fork handlers established using
281 .\"O .BR pthread_atfork (3)
282 .\"O are not called when a multithreaded program employing
283 .\"O the NPTL threading library calls
285 .\"O Fork handlers are called in this case in a program using the
286 .\"O LinuxThreads threading library.
288 .\"O .BR pthreads (7)
289 .\"O for a description of Linux threading libraries.)
290 .BR pthread_atfork (3)
291 ¤ò»È¤Ã¤ÆÀßÄꤵ¤ì¤¿ fork ¥Ï¥ó¥É¥é¤Ï
292 NPTL ¥¹¥ì¥Ã¥É¥é¥¤¥Ö¥é¥ê¥³¡¼¥ë¤òºÎÍѤ·¤¿¥Þ¥ë¥Á¥¹¥ì¥Ã¥É¥×¥í¥°¥é¥à¤Ç¤Ï
293 ¸Æ¤Ó½Ð¤µ¤ì¤Ê¤¤¡£°ìÊý¡¢LinuxThreads ¥¹¥ì¥Ã¥É¥é¥¤¥Ö¥é¥ê¤ò»È¤Ã¤¿
294 ¥×¥í¥°¥é¥à¤Ç¤Ï¡¢fork ¥Ï¥ó¥É¥é¤Ï¸Æ¤Ó½Ð¤µ¤ì¤ë¡£
295 (Linux ¤Î¥¹¥ì¥Ã¥É¥é¥¤¥Ö¥é¥ê¤ÎÀâÌÀ¤Ï
302 .\"O system call appeared in 3.0BSD.
304 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï 3.0BSD ¤Ë¸½¤ï¤ì¤¿¡£
305 .\" In the release notes for 4.2BSD Sam Leffler wrote: `vfork: Is still
306 .\" present, but definitely on its way out'.
307 .\"O In 4.4BSD it was made synonymous to
309 .\"O but NetBSD introduced it again,
310 .\"O cf. http://www.netbsd.org/Documentation/kernel/vfork.html .
313 ¤ÎƱµÁ¸ì¤È¤Ê¤Ã¤¿¤¬¡¢NetBSD ¤Ç¤ÏºÆ¤ÓƳÆþ¤µ¤ì¤¿¡£
314 http://www.netbsd.org/Documentation/kernel/vfork.html ¤ò»²¾È¡£
315 .\"O In Linux, it has been equivalent to
317 .\"O until 2.2.0-pre6 or so.
318 .\"O Since 2.2.0-pre9 (on i386, somewhat later on
319 .\"O other architectures) it is an independent system call.
320 .\"O Support was added in glibc 2.0.112.
321 Linux ¤Ç¤Ï 2.2.0-pre6 ¤¢¤¿¤ê¤Þ¤Ç¤Ï
323 ¤ÈÅù²Á¤Ç¤¢¤Ã¤¿¡£(i386 ¤Ç¤Ï) 2.2.0-pre9 ¤«¤é (¾¤Î¥¢¡¼¥¥Æ¥¯¥Á¥ã¤Ç¤Ï
324 ¾¯¤·ÃÙ¤ì¤Æ) ÆÈΩ¤·¤¿¥·¥¹¥Æ¥à¥³¡¼¥ë¤È¤Ê¤Ã¤¿¡£
325 glibc ¤Ç¤Î¥µ¥Ý¡¼¥È¤Ï glibc-2.0.112 ¤ÇÄɲ䵤줿¡£
328 .\"O It is rather unfortunate that Linux revived this specter from the past.
329 .\"O The BSD man page states:
330 .\"O "This system call will be eliminated when proper system sharing mechanisms
331 .\"O are implemented.
332 .\"O Users should not depend on the memory sharing semantics of
334 .\"O as it will, in that case, be made synonymous to
337 Linux ¤¬¤³¤Î²áµî¤ÎË´Îî¤òÉü³è¤µ¤»¤¿¤³¤È¤Ï¡¢¤à¤·¤íÉÔ¹¬¤È¸À¤¦¤Ù¤¤Ç¤¢¤ë¡£
338 BSD ¤Î¥Þ¥Ë¥å¥¢¥ë¤Ë¤Ï¡¢
339 ¡Ö¤³¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤ÏÂÅÅö¤Ê¥·¥¹¥Æ¥à¶¦Íµ¡¹½¤¬¼ÂÁõ¤µ¤ì¤¿¾ì¹ç¤Ë¤Ï
342 ¤Î¥á¥â¥ê¶¦Íµ¡Ç½¤Ë°Í¸¤¹¤ë¤Ù¤¤Ç¤Ï¤Ê¤¤¡£²¿¸Î¤Ê¤é¤Ð¡¢¤³¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë
343 ¤¬ºï½ü¤µ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢¤½¤ì¤Ï
345 ¤ÎƱµÁ¸ì¤È¤µ¤ì¤ë¤«¤é¤Ç¤¢¤ë¡£¡×¤È½ñ¤«¤ì¤Æ¤¤¤ë¡£
347 .\"O Details of the signal handling are obscure and differ between systems.
348 .\"O The BSD man page states:
349 .\"O "To avoid a possible deadlock situation, processes that are children
350 .\"O in the middle of a
356 .\"O signals; rather, output or
358 .\"O are allowed and input attempts result in an end-of-file indication."
359 ¥·¥°¥Ê¥ë¤Î°·¤¤¤Î¾ÜºÙ¤ÏÉÔÌÀÎƤǥ·¥¹¥Æ¥à¤´¤È¤Ë°Û¤Ã¤Æ¤¤¤ë¡£
360 BSD ¤Î¥Þ¥Ë¥å¥¢¥ë¤Ë¤Ï¡¢
361 ¡Ö¥Ç¥Ã¥É¥í¥Ã¥¯¾õÂ֤ˤʤë²ÄǽÀ¤¬¤¢¤ë¤Î¤Ç
367 ¥·¥°¥Ê¥ë¤òÁ÷¿®¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤;
370 ¤Ïµö¤µ¤ì¤ë¤¬¡¢ÆþÎϤò»î¤ß¤¿¾ì¹ç¤Ë¤Ï·ë²Ì¤Ï¥Õ¥¡¥¤¥ë½ªÃ¼ (EOF) ¤Ë¤Ê¤ë¡£¡×
373 .\" As far as I can tell, the following is not true in 2.6.19:
374 .\"O .\" Currently (Linux 2.3.25),
375 .\"O .\" .BR strace (1)
376 .\"O .\" cannot follow
377 .\"O .\" .BR vfork ()
378 .\"O .\" and requires a kernel patch.
379 .\" ¸½ºß¤Î¤È¤³¤í (Linux 2.3.25)¡¢
383 .\" ¤òÄÉÀפ¹¤ë¤³¤È¤¬¤Ç¤¤º¡¢¥«¡¼¥Í¥ë¥Ñ¥Ã¥Á¤¬É¬ÍפǤ¢¤ë¡£