OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / draft / man2 / fork.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" A few fragments remain from an earlier (1992) page by
5 .\" Drew Eckhardt (drew@cs.colorado.edu),
6 .\"
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
27 .\" Modified by Michael Haardt (michael@moria.de)
28 .\" Modified Sat Jul 24 13:22:07 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Modified 21 Aug 1994 by Michael Chastain (mec@shell.portal.com):
30 .\"   Referenced 'clone(2)'.
31 .\" Modified 1995-06-10, 1996-04-18, 1999-11-01, 2000-12-24
32 .\"   by Andries Brouwer (aeb@cwi.nl)
33 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\"     Added notes on capability requirements
35 .\" 2006-09-04, Michael Kerrisk
36 .\"     Greatly expanded, to describe all attributes that differ
37 .\"     parent and child.
38 .\"
39 .\" Japanese Version Copyright (c) 1996 TABATA Tomohira
40 .\"         all rights reserved.
41 .\" Translated Thu Jun 27 20:35:06 JST 1996
42 .\"         by TABATA Tomohira <loba@k2.t.u-tokyo.ac.jp>
43 .\" Modified Sun Dec 14 00:43:22 JST 1997
44 .\"         by HANATAKA Shinya <hanataka@abyss.rim.or.jp>
45 .\" Modified Tue Jul 10 05:36:22 JST 2001
46 .\"         by Yuichi SATO <ysato@h4.dion.ne.jp>, LDP v1.38
47 .\" Updated & Modified Wed Dec 29 12:33:12 JST 2004
48 .\"         by Yuichi SATO <ysato444@yahoo.co.jp>, LDP v2.01
49 .\" Updated & Modified Wed Jan  3 04:11:03 JST 2007 by Yuichi SATO, LDP v2.43
50 .\" Updated 2008-08-04, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
51 .\"
52 .TH FORK 2 2009-04-27 "Linux" "Linux Programmer's Manual"
53 .\"O .SH NAME
54 .SH 名前
55 .\"O fork \- create a child process
56 fork \- 子プロセスを生成する
57 .\"O .SH SYNOPSIS
58 .SH 書式
59 .B #include <unistd.h>
60 .sp
61 .B pid_t fork(void);
62 .\"O .SH DESCRIPTION
63 .SH 説明
64 .\"O .BR fork ()
65 .\"O creates a new process by duplicating the calling process.
66 .\"O The new process, referred to as the \fIchild\fP,
67 .\"O is an exact duplicate of the calling process,
68 .\"O referred to as the \fIparent\fP, except for the following points:
69 .BR fork ()
70 は呼び出し元プロセスを複製して新しいプロセスを生成する。
71 \fIchild\fP で参照される新しいプロセスは、以下の点を除き、
72 \fIparent\fP で参照される呼び出し元プロセスの完全な複製である:
73 .IP * 3
74 .\"O The child has its own unique process ID,
75 .\"O and this PID does not match the ID of any existing process group
76 .\"O .RB ( setpgid (2)).
77 子プロセスは独自のプロセス ID を持ち、
78 この PID は既存のどのプロセスグループ
79 .RB ( setpgid (2))
80 の ID とも一致しない。
81 .IP *
82 .\"O The child's parent process ID is the same as the parent's process ID.
83 子プロセスの親プロセス ID は、親プロセスのプロセス ID と同じである。
84 .IP *
85 .\"O The child does not inherit its parent's memory locks
86 .\"O .RB ( mlock (2),
87 .\"O .BR mlockall (2)).
88 子プロセスは親プロセスのメモリロック
89 .RB ( mlock (2),
90 .BR mlockall (2))
91 を引き継がない。
92 .IP *
93 .\"O Process resource utilizations
94 .\"O .RB ( getrusage (2))
95 .\"O and CPU time counters
96 .\"O .RB ( times (2))
97 .\"O are reset to zero in the child.
98 プロセスの資源利用量
99 .RB ( getrusage (2))
100 と CPU タイムカウンタ
101 .RB ( times (2))
102 が、子プロセスでは 0 にリセットされる。
103 .IP *
104 .\"O The child's set of pending signals is initially empty
105 .\"O .RB ( sigpending (2)).
106 子プロセスの処理待ちのシグナルの集合
107 .RB ( sigpending (2))
108 は、初期状態では空になる。
109 .IP *
110 .\"O The child does not inherit semaphore adjustments from its parent
111 .\"O .RB ( semop (2)).
112 子プロセスは親プロセスからセマフォ調整
113 .RB ( semop (2))
114 を引き継がない。
115 .IP *
116 .\"O The child does not inherit record locks from its parent
117 .\"O .RB ( fcntl (2)).
118 子プロセスは親プロセスからレコードロック
119 .RB ( fcntl (2))
120 を引き継がない。
121 .IP *
122 .\"O The child does not inherit timers from its parent
123 .\"O .RB ( setitimer (2),
124 .\"O .BR alarm (2),
125 .\"O .BR timer_create (2)).
126 子プロセスは親プロセスからタイマー
127 .RB ( setitimer (2),
128 .BR alarm (2),
129 .BR timer_create (2))
130 を引き継がない。
131 .IP *
132 .\"O The child does not inherit outstanding asynchronous I/O operations
133 .\"O from its parent
134 .\"O .RB ( aio_read (3),
135 .\"O .BR aio_write (3)),
136 .\"O nor does it inherit any asynchronous I/O contexts from its parent (see
137 .\"O .BR io_setup (2)).
138 子プロセスは親プロセスから主だった非同期 I/O 操作を引き継がない
139 .RB ( aio_read (3),
140 .BR aio_write (3)
141 参照)。
142 また、親プロセスから非同期 I/O コンテキストを引き継がない
143 .RB ( io_setup (2)
144 参照)。
145 .PP
146 .\"O The process attributes in the preceding list are all specified
147 .\"O in POSIX.1-2001.
148 .\"O The parent and child also differ with respect to the following
149 .\"O Linux-specific process attributes:
150 上記のリストにあるプロセス属性は、POSIX.1-2001 で全て指定されている。
151 親プロセスと子プロセスは、以下の Linux 固有のプロセス属性も異なる:
152 .IP * 3
153 .\"O The child does not inherit directory change notifications (dnotify)
154 .\"O from its parent
155 .\"O (see the description of
156 .\"O .B F_NOTIFY
157 .\"O in
158 .\"O .BR fcntl (2)).
159 子プロセスは親プロセスからディレクトリ変更通知 (dnotify)
160 .RB ( fcntl (2)
161 における
162 .B F_NOTIFY
163 の説明を参照) を引き継がない。
164 .IP *
165 .\"O The
166 .\"O .BR prctl (2)
167 .\"O .B PR_SET_PDEATHSIG
168 .\"O setting is reset so that the child does not receive a signal
169 .\"O when its parent terminates.
170 .BR prctl (2)
171
172 .B PR_SET_PDEATHSIG
173 の設定がリセットされ、子プロセスは親プロセスが終了したときに
174 シグナルを受信しない。
175 .IP *
176 .\"O Memory mappings that have been marked with the
177 .\"O .BR madvise (2)
178 .\"O .B MADV_DONTFORK
179 .\"O flag are not inherited across a
180 .\"O .BR fork ().
181 .BR madvise (2)
182
183 .B MADV_DONTFORK
184 フラグでマークされたメモリマッピングは、
185 .BR fork ()
186 によって引き継がれない。
187 .IP *
188 .\"O The termination signal of the child is always
189 .\"O .B SIGCHLD
190 .\"O (see
191 .\"O .BR clone (2)).
192 子プロセスの終了シグナルは常に
193 .B SIGCHLD
194 である
195 .RB ( clone (2)
196 を参照)。
197 .PP
198 .\"O Note the following further points:
199 さらに以下の点について注意すること:
200 .IP * 3
201 .\"O The child process is created with a single thread\(emthe
202 .\"O one that called
203 .\"O .BR fork ().
204 子プロセスはシングルスレッドで生成される。つまり、
205 .BR fork ()
206 を呼び出したスレッドとなる。
207 .\"O The entire virtual address space of the parent is replicated in the child,
208 .\"O including the states of mutexes, condition variables,
209 .\"O and other pthreads objects; the use of
210 .\"O .BR pthread_atfork (3)
211 .\"O may be helpful for dealing with problems that this can cause.
212 親プロセスの仮想アドレス空間全体が子プロセスに複製される。
213 これにはミューテックス (mutex) の状態・条件変数・
214 pthread オブジェクトが含まれる。
215 これが引き起こす問題を扱うには、
216 .BR pthread_atfork (3)
217 を使うと良いだろう。
218 .IP *
219 .\"O The child inherits copies of the parent's set of open file descriptors.
220 .\"O Each file descriptor in the child refers to the same
221 .\"O open file description (see
222 .\"O .BR open (2))
223 .\"O as the corresponding file descriptor in the parent.
224 子プロセスは親プロセスが持つ
225 オープンファイルディスクリプタの集合のコピーを引き継ぐ。
226 子プロセスの各ファイルディスクリプタは、
227 親プロセスのファイルディスクリプタに対応する
228 同じオープンファイル記述 (file description) を参照する
229 .RB ( open (2)
230 を参照)。
231 .\"O This means that the two descriptors share open file status flags,
232 .\"O current file offset,
233 .\"O and signal-driven I/O attributes (see the description of
234 .\"O .B F_SETOWN
235 .\"O and
236 .\"O .B F_SETSIG
237 .\"O in
238 .\"O .BR fcntl (2)).
239 これは 2 つのディスクリプタが、ファイル状態フラグ・
240 現在のファイルオフセット、シグナル駆動 (signal-driven) I/O 属性
241 .RB ( fcntl (2)
242 における
243 .BR F_SETOWN ,
244 .B F_SETSIG
245 の説明を参照) を共有することを意味する。
246 .IP *
247 .\"O The child inherits copies of the parent's set of open message
248 .\"O queue descriptors (see
249 .\"O .BR mq_overview (7)).
250 子プロセスは親プロセスが持つオープンメッセージキューディスクリプタ
251 .RB ( mq_overview (7)
252 を参照) の集合のコピーを引き継ぐ。
253 .\"O Each descriptor in the child refers to the same
254 .\"O open message queue description
255 .\"O as the corresponding descriptor in the parent.
256 子プロセスの各ディスクリプタは、
257 親プロセスのディスクリプタに対応する
258 同じオープンメッセージキューディスクリプタを参照する。
259 .\"O This means that the two descriptors share the same flags
260 .\"O .RI ( mq_flags ).
261 これは 2 つのディスクリプタが同じフラグ
262 .RI ( mq_flags )
263 を共有することを意味する。
264 .IP *
265 .\"O The child inherits copies of the parent's set of open directory streams (see
266 .\"O .BR opendir (3)).
267 .\"O POSIX.1-2001 says that the corresponding directory streams
268 .\"O in the parent and child
269 .\"O .I may
270 .\"O share the directory stream positioning;
271 .\"O on Linux/glibc they do not.
272 子プロセスは、親プロセスのオープン済みのディレクトリストリームの集合
273 .RB ( opendir (3)
274 参照) のコピーを継承する。
275 POSIX.1-2001 では、親プロセスと子プロセス間の対応するディレクトリストリーム
276 はディレクトリストリームの位置 (positioning) を共有してもよいとされている。
277 Linux/glibc ではディレクトリストリームの位置の共有は行われていない。
278 .\"O .SH "RETURN VALUE"
279 .SH 返り値
280 .\"O On success, the PID of the child process is returned in the parent,
281 .\"O and 0 is returned in the child.
282 .\"O On failure, \-1 is returned in the parent,
283 .\"O no child process is created, and
284 .\"O .I errno
285 .\"O is set appropriately.
286 成功した場合、親プロセスには子プロセスの PID が返され、
287 子プロセスには 0 が返される。
288 失敗した場合、親プロセスに \-1 が返され、子プロセスは生成されず、
289 .I errno
290 が適切に設定される。
291 .\"O .SH ERRORS
292 .SH エラー
293 .TP
294 .B EAGAIN
295 .\"O .BR fork ()
296 .\"O cannot allocate sufficient memory to copy the parent's page tables and
297 .\"O allocate a task structure for the child.
298 親プロセスのページ・テーブルのコピーと
299 子プロセスのタスク構造に生成に必要なメモリを
300 .BR fork ()
301 が割り当てることができなかった。
302 .TP
303 .B EAGAIN
304 .\"O It was not possible to create a new process because the caller's
305 .\"O .B RLIMIT_NPROC
306 .\"O resource limit was encountered.
307 呼び出し元の
308 .B RLIMIT_NPROC
309 資源の制限 (resource limit) に達したために、新しいプロセスを生成できなかった。
310 .\"O To exceed this limit, the process must have either the
311 .\"O .B CAP_SYS_ADMIN
312 .\"O or the
313 .\"O .B CAP_SYS_RESOURCE
314 .\"O capability.
315 この制限を超えるには、プロセスは
316 .B CAP_SYS_ADMIN
317 または
318 .B CAP_SYS_RESOURCE
319 ケーパビリティ (capability) を持っていなくてはならない。
320 .TP
321 .B ENOMEM
322 .\"O .BR fork ()
323 .\"O failed to allocate the necessary kernel structures because memory is tight.
324 メモリが足りないために、
325 .BR fork ()
326 は必要なカーネル構造体を割り当てることができなかった。
327 .\"O .SH "CONFORMING TO"
328 .SH 準拠
329 SVr4, 4.3BSD, POSIX.1-2001.
330 .\"O .SH NOTES
331 .SH 注意
332 .\"O .PP
333 .\"O Under Linux,
334 .\"O .BR fork ()
335 .\"O is implemented using copy-on-write pages, so the only penalty that it incurs
336 .\"O is the time and memory required to duplicate the parent's page tables,
337 .\"O and to create a unique task structure for the child.
338 Linux では、
339 .BR fork ()
340 を 書き込み時コピー (copy-on-write)・ページを用いて実装している。
341 したがって、fork を行うことの唯一のデメリットは、
342 親プロセスのページ・テーブルを複製と
343 子プロセス自身のタスク構造の作成のための時間とメモリが必要なことである。
344
345 .\"O Since version 2.3.3,
346 .\"O .\" nptl/sysdeps/unix/sysv/linux/fork.c
347 .\"O rather than invoking the kernel's
348 .\"O .BR fork ()
349 .\"O system call,
350 .\"O the glibc
351 .\"O .BR fork ()
352 .\"O wrapper that is provided as part of the
353 .\"O NPTL threading implementation invokes
354 .\"O .BR clone (2)
355 .\"O with flags that provide the same effect as the traditional system call.
356 .\"O The glibc wrapper invokes any fork handlers that have been
357 .\"O established using
358 .\"O .BR pthread_atfork (3).
359 .\"O .\" and does some magic to ensure that getpid(2) returns the right value.
360 glibc 2.3.3 以降では、
361 NPTL スレッド実装の一部として提供されている glibc の
362 .BR fork ()
363 ラッパー関数は、
364 カーネルの
365 .BR fork ()
366 システムコールを起動するのではなく、
367 .BR clone (2)
368 を起動する。
369 .BR clone (2)
370 に渡すフラグとして、伝統的な
371 .BR fork ()
372 システムコールと同じ効果が得られるようなフラグが指定される。
373 glibc のラッパー関数は
374 .BR pthread_atfork (3)
375 を使って設定されている任意の fork ハンドラを起動する。
376 .\" getpid(2) が正しい値を返すことができるように何らかの処理を行う。
377
378 .\"O .SH EXAMPLE
379 .SH 例
380 .\"O See
381 .\"O .BR pipe (2)
382 .\"O and
383 .\"O .BR wait (2).
384 .BR pipe (2)
385 および
386 .BR wait (2)
387 を参照。
388 .\"O .SH "SEE ALSO"
389 .SH 関連項目
390 .BR clone (2),
391 .BR execve (2),
392 .BR setrlimit (2),
393 .BR unshare (2),
394 .BR vfork (2),
395 .BR wait (2),
396 .BR daemon (3),
397 .BR capabilities (7),
398 .BR credentials (7)