.\" Copyright (C) 2006 Michael Kerrisk .\" A few fragments remain from an earlier (1992) page by .\" Drew Eckhardt (drew@cs.colorado.edu), .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Modified by Michael Haardt (michael@moria.de) .\" Modified Sat Jul 24 13:22:07 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified 21 Aug 1994 by Michael Chastain (mec@shell.portal.com): .\" Referenced 'clone(2)'. .\" Modified 1995-06-10, 1996-04-18, 1999-11-01, 2000-12-24 .\" by Andries Brouwer (aeb@cwi.nl) .\" Modified, 27 May 2004, Michael Kerrisk .\" Added notes on capability requirements .\" 2006-09-04, Michael Kerrisk .\" Greatly expanded, to describe all attributes that differ .\" parent and child. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1996 TABATA Tomohira .\" all rights reserved. .\" Translated Thu Jun 27 20:35:06 JST 1996 .\" by TABATA Tomohira .\" Modified Sun Dec 14 00:43:22 JST 1997 .\" by HANATAKA Shinya .\" Modified Tue Jul 10 05:36:22 JST 2001 .\" by Yuichi SATO , LDP v1.38 .\" Updated & Modified Wed Dec 29 12:33:12 JST 2004 .\" by Yuichi SATO , LDP v2.01 .\" Updated & Modified Wed Jan 3 04:11:03 JST 2007 by Yuichi SATO, LDP v2.43 .\" Updated 2008-08-04, Akihiro MOTOKI , LDP v3.05 .\" Updated 2012-05-29, Akihiro MOTOKI .\" Updated 2013-05-01, Akihiro MOTOKI .\" .TH FORK 2 2020\-06\-09 Linux "Linux Programmer's Manual" .SH 名前 fork \- 子プロセスを生成する .SH 書式 \fB#include \fP .br \fB#include \fP .PP \fBpid_t fork(void);\fP .SH 説明 \fBfork\fP() は呼び出し元プロセスを複製して新しいプロセスを生成する。新しいプロセスは「子」プロセスと呼ばれ、呼び出し元プロセスは「親」プロセスと呼ばれる。 .PP The child process and the parent process run in separate memory spaces. At the time of \fBfork\fP() both memory spaces have the same content. Memory writes, file mappings (\fBmmap\fP(2)), and unmappings (\fBmunmap\fP(2)) performed by one of the processes do not affect the other. .PP The child process is an exact duplicate of the parent process except for the following points: .IP * 3 The child has its own unique process ID, and this PID does not match the ID of any existing process group (\fBsetpgid\fP(2)) or session. .IP * 子プロセスの親プロセス ID は、親プロセスのプロセス ID と同じである。 .IP * 子プロセスは親プロセスのメモリーロック (\fBmlock\fP(2), \fBmlockall\fP(2)) を引き継がない。 .IP * プロセスの資源利用量 (\fBgetrusage\fP(2)) と CPU タイムカウンター (\fBtimes\fP(2)) が、子プロセスでは 0 にリセットされる。 .IP * 子プロセスの処理待ちのシグナルの集合 (\fBsigpending\fP(2)) は、初期状態では空になる。 .IP * 子プロセスは親プロセスからセマフォ調整 (\fBsemop\fP(2)) を引き継がない。 .IP * 子プロセスは親プロセスからプロセスに関連付けられたレコードロックを引き継がない (\fBfcntl\fP(2))。 (一方、子プロセスは親プロセスから \fBfcntl\fP(2) オープンファイル記述ロックと \fBflock\fP(2) ロックを引き継ぐ。) .IP * 子プロセスは親プロセスからタイマー (\fBsetitimer\fP(2), \fBalarm\fP(2), \fBtimer_create\fP(2)) を引き継がない。 .IP * 子プロセスは親プロセスから主だった非同期 I/O 操作を引き継がない (\fBaio_read\fP(3), \fBaio_write\fP(3) 参照)。 また、親プロセスから非同期 I/O コンテキストを引き継がない (\fBio_setup\fP(2) 参照)。 .PP 上記のリストにあるプロセス属性は、POSIX.1 で全て指定されている。 親プロセスと子プロセスは、以下の Linux 固有のプロセス属性も異なる: .IP * 3 子プロセスは親プロセスからディレクトリ変更通知 (dnotify) (\fBfcntl\fP(2) における \fBF_NOTIFY\fP の説明を参照) を引き継がない。 .IP * \fBprctl\fP(2) の \fBPR_SET_PDEATHSIG\fP の設定がリセットされ、子プロセスは親プロセスが終了したときに シグナルを受信しない。 .IP * timer slack value のデフォルト値には、親プロセスの現在の timer slack value が設定される。 \fBprctl\fP(2) の \fBPR_SET_TIMERSLACK\fP の説明を参照。 .IP * \fBmadvise\fP(2) の \fBMADV_DONTFORK\fP フラグでマークされたメモリーマッピングは、 \fBfork\fP() によって引き継がれない。 .IP * Memory in address ranges that have been marked with the \fBmadvise\fP(2) \fBMADV_WIPEONFORK\fP flag is zeroed in the child after a \fBfork\fP(). (The \fBMADV_WIPEONFORK\fP setting remains in place for those address ranges in the child.) .IP * 子プロセスの終了シグナルは常に \fBSIGCHLD\fP である (\fBclone\fP(2) を参照)。 .IP * \fBioperm\fP(2) で設定されるポートアクセス許可ビットは、子プロセスには継承されない。子プロセスでは、 \fBioperm\fP(2) を使って必要なビットをセットしなければならない。 .PP 以下の点についても注意すること: .IP * 3 子プロセスはシングルスレッドで生成される。つまり、 \fBfork\fP() を呼び出したスレッドとなる。 親プロセスの仮想アドレス空間全体が子プロセスに複製される。 これにはミューテックス (mutex) の状態・条件変数・ pthread オブジェクトが含まれる。 これが引き起こす問題を扱うには、 \fBpthread_atfork\fP(3) を使うと良いだろう。 .IP * After a \fBfork\fP() in a multithreaded program, the child can safely call only async\-signal\-safe functions (see \fBsignal\-safety\fP(7)) until such time as it calls \fBexecve\fP(2). .IP * 子プロセスは親プロセスが持つ オープンファイルディスクリプターの集合のコピーを引き継ぐ。 子プロセスの各ファイルディスクリプターは、 親プロセスのファイルディスクリプターに対応する 同じオープンファイル記述 (file description) を参照する (\fBopen\fP(2) を参照)。 これは 2 つのファイルディスクリプターが、ファイル状態フラグ・ ファイルオフセット、シグナル駆動 (signal\-driven) I/O 属性 (\fBfcntl\fP(2) における \fBF_SETOWN\fP, \fBF_SETSIG\fP の説明を参照) を共有することを意味する。 .IP * 子プロセスは親プロセスが持つオープンメッセージキューディスクリプター (\fBmq_overview\fP(7) を参照) の集合のコピーを引き継ぐ。 子プロセスの各ファイルディスクリプターは、 親プロセスのファイルディスクリプターに対応する同じオープンメッセージキューディスクリプターを参照する。 これは 2 つのファイルディスクリプターが同じフラグ (\fImq_flags\fP) を共有することを意味する。 .IP * 子プロセスは、親プロセスのオープン済みのディレクトリストリームの集合 (\fBopendir\fP(3) 参照) のコピーを継承する。 POSIX.1 では、親プロセスと子プロセス間の対応するディレクトリストリーム はディレクトリストリームの位置 (positioning) を共有してもよいとされている。 Linux/glibc ではディレクトリストリームの位置の共有は行われていない。 .SH 返り値 成功した場合、親プロセスには子プロセスの PID が返され、 子プロセスには 0 が返される。 失敗した場合、親プロセスに \-1 が返され、子プロセスは生成されず、 \fIerrno\fP が適切に設定される。 .SH エラー .TP \fBEAGAIN\fP .\" NOTE! The following should match the description in pthread_create(3) A system\-imposed limit on the number of threads was encountered. There are a number of limits that may trigger this error: .RS .IP * 3 the \fBRLIMIT_NPROC\fP soft resource limit (set via \fBsetrlimit\fP(2)), which limits the number of processes and threads for a real user ID, was reached; .IP * the kernel's system\-wide limit on the number of processes and threads, \fI/proc/sys/kernel/threads\-max\fP, was reached (see \fBproc\fP(5)); .IP * the maximum number of PIDs, \fI/proc/sys/kernel/pid_max\fP, was reached (see \fBproc\fP(5)); or .IP * the PID limit (\fIpids.max\fP) imposed by the cgroup "process number" (PIDs) controller was reached. .RE .TP \fBEAGAIN\fP 呼び出し元は、スケジューリングポリシー \fBSCHED_DEADLINE\fP で動作しており、かつ reset\-on\-fork フラグがセットされていない。 \fBsched\fP(7) 参照。 .TP \fBENOMEM\fP メモリーが足りないために、 \fBfork\fP() は必要なカーネル構造体を割り当てることができなかった。 .TP \fBENOMEM\fP An attempt was made to create a child process in a PID namespace whose "init" process has terminated. See \fBpid_namespaces\fP(7). .TP \fBENOSYS\fP .\" e.g., arm (optionally), blackfin, c6x, frv, h8300, microblaze, xtensa \fBfork\fP() はこのプラットフォームではサポートされていない (例えば、メモリー管理ユニット (MMU) がないハードウェア)。 .TP \fBERESTARTNOINTR\fP (Linux 2.6.17 以降) .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56 System call was interrupted by a signal and will be restarted. (This can be seen only during a trace.) .SH 準拠 POSIX.1\-2001, POSIX.1\-2008, SVr4, 4.3BSD. .SH 注意 Linux では、 \fBfork\fP() を 書き込み時コピー (copy\-on\-write) ページを用いて実装している。 したがって、fork を行うことの唯一のデメリットは、 親プロセスのページテーブルを複製と 子プロセス自身のタスク構造の作成のための時間とメモリーが必要なことである。 .SS "C ライブラリとカーネルの違い" .\" nptl/sysdeps/unix/sysv/linux/fork.c .\" and does some magic to ensure that getpid(2) returns the right value. glibc 2.3.3 以降では、 NPTL スレッド実装の一部として提供されている glibc の\fBfork\fP() ラッパー関数は、 カーネルの \fBfork\fP() システムコール を起動するのではなく、\fBclone\fP(2) を起動する。 \fBclone\fP(2) に渡すフラグとして、伝統的な \fBfork\fP() システムコールと 同じ効果が得られるようなフラグが指定される (\fBfork\fP() の呼び出しは、 \fIflags\fP に \fBSIGCHLD\fP だけを指定して \fBclone\fP(2) を呼び出すのと等価である)。 glibc のラッパー関数は \fBpthread_atfork\fP(3) を使って設定されている 任意の fork ハンドラーを起動する。 .SH 例 \fBpipe\fP(2) および \fBwait\fP(2) を参照。 .SH 関連項目 \fBclone\fP(2), \fBexecve\fP(2), \fBexit\fP(2), \fBsetrlimit\fP(2), \fBunshare\fP(2), \fBvfork\fP(2), \fBwait\fP(2), \fBdaemon\fP(3), \fBpthread_atfork\fP(3), \fBcapabilities\fP(7), \fBcredentials\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。