fork
Section: System Calls (2)
Updated: 2026-02-08
Index
JM Home Page
roff page
名前
fork - 子プロセスを生成する
ライブラリ
標準 C ライブラリ (libc,~-lc)
書式
#include <unistd.h>
pid_t fork(void);
説明
fork()
は呼び出し元プロセスを複製して新しいプロセスを生成する。新しいプロセスは「子」プロセスと呼ばれ、呼び出し元プロセスは「親」プロセスと呼ばれる。
The child process and the parent process run in separate memory spaces. At
the time of fork() both memory spaces have the same content. Memory
writes, file mappings (mmap(2)), and unmappings (munmap(2)) performed
by one of the processes do not affect the other.
The child process is an exact duplicate of the parent process except for the
following points:
- •
-
The child has its own unique process ID, and this PID does not match the ID
of any existing process group (setpgid(2)) or session.
- •
-
子プロセスの親プロセス ID は、親プロセスのプロセス ID と同じである。
- •
-
子プロセスは親プロセスのメモリーロック (mlock(2), mlockall(2)) を引き継がない。
- •
-
プロセスの資源利用量 (getrusage(2)) と CPU タイムカウンター (times(2)) が、子プロセスでは 0
にリセットされる。
- •
-
子プロセスの処理待ちのシグナルの集合 (sigpending(2)) は、初期状態では空になる。
- •
-
子プロセスは親プロセスからセマフォ調整 (semop(2)) を引き継がない。
- •
-
子プロセスは親プロセスからプロセスに関連付けられたレコードロックを引き継がない (fcntl(2))。 (一方、子プロセスは親プロセスから
fcntl(2) オープンファイル記述ロックと flock(2) ロックを引き継ぐ。)
- •
-
子プロセスは親プロセスからタイマー (setitimer(2), alarm(2), timer_create(2))
を引き継がない。
- •
-
子プロセスは親プロセスから主だった非同期 I/O 操作を引き継がない (aio_read(3), aio_write(3) 参照)。
また、親プロセスから非同期 I/O コンテキストを引き継がない (io_setup(2) 参照)。
上記のリストにあるプロセス属性は、POSIX.1 で全て指定されている。 親プロセスと子プロセスは、以下の Linux 固有のプロセス属性も異なる:
- •
-
子プロセスは親プロセスからディレクトリ変更通知 (dnotify) (fcntl(2) における F_NOTIFY の説明を参照)
を引き継がない。
- •
-
prctl(2) の PR_SET_PDEATHSIG の設定がリセットされ、子プロセスは親プロセスが終了したときに
シグナルを受信しない。
- •
-
timer slack value のデフォルト値には、親プロセスの現在の timer slack value が設定される。 prctl(2)
の PR_SET_TIMERSLACK の説明を参照。
- •
-
madvise(2) の MADV_DONTFORK フラグでマークされたメモリーマッピングは、 fork()
によって引き継がれない。
- •
-
Memory in address ranges that have been marked with the madvise(2)
MADV_WIPEONFORK flag is zeroed in the child after a fork(). (The
MADV_WIPEONFORK setting remains in place for those address ranges in the
child.)
- •
-
子プロセスの終了シグナルは常に SIGCHLD である (clone(2) を参照)。
- •
-
ioperm(2) で設定されるポートアクセス許可ビットは、子プロセスには継承されない。子プロセスでは、 ioperm(2)
を使って必要なビットをセットしなければならない。
以下の点についても注意すること:
- •
-
The child process is created with a single thread---the one that called
fork(). The entire virtual address space of the parent is replicated in
the child, including the states of mutexes, condition variables, and other
pthreads objects; the use of pthread_atfork(3) may be helpful for
dealing with problems that this can cause.
- •
-
After a fork() in a multithreaded program, the child can safely call
only async-signal-safe functions (see signal-safety(7)) until such time
as it calls execve(2).
- •
-
子プロセスは親プロセスが持つ オープンファイルディスクリプターの集合のコピーを引き継ぐ。 子プロセスの各ファイルディスクリプターは、
親プロセスのファイルディスクリプターに対応する 同じオープンファイル記述 (file description) を参照する (open(2)
を参照)。 これは 2 つのファイルディスクリプターが、ファイル状態フラグ・ ファイルオフセット、シグナル駆動 (signal-driven) I/O
属性 (fcntl(2) における F_SETOWN, F_SETSIG の説明を参照) を共有することを意味する。
- •
-
子プロセスは親プロセスが持つオープンメッセージキューディスクリプター (mq_overview(7) を参照) の集合のコピーを引き継ぐ。
子プロセスの各ファイルディスクリプターは、 親プロセスのファイルディスクリプターに対応する同じオープンメッセージキューディスクリプターを参照する。
これは 2 つのファイルディスクリプターが同じフラグ (mq_flags) を共有することを意味する。
- •
-
子プロセスは、親プロセスのオープン済みのディレクトリストリームの集合 (opendir(3) 参照) のコピーを継承する。 POSIX.1
では、親プロセスと子プロセス間の対応するディレクトリストリーム はディレクトリストリームの位置 (positioning)
を共有してもよいとされている。 Linux/glibc ではディレクトリストリームの位置の共有は行われていない。
返り値
On success, the PID of the child process is returned in the parent, and 0 is
returned in the child. On failure, -1 is returned in the parent, no child
process is created, and errno is set to indicate the error.
エラー
- EAGAIN
-
A system-imposed limit on the number of threads was encountered. There are
a number of limits that may trigger this error:
-
- •
-
the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which
limits the number of processes and threads for a real user ID, was reached;
- •
-
the kernel's system-wide limit on the number of processes and threads,
/proc/sys/kernel/threads-max, was reached (see proc(5));
- •
-
the maximum number of PIDs, /proc/sys/kernel/pid_max, was reached (see
proc(5)); or
- •
-
the PID limit (pids.max) imposed by the cgroup "process number" (PIDs)
controller was reached.
- EAGAIN
-
呼び出し元は、スケジューリングポリシー SCHED_DEADLINE で動作しており、かつ reset-on-fork
フラグがセットされていない。 sched(7) 参照。
- ENOMEM
-
メモリーが足りないために、 fork() は必要なカーネル構造体を割り当てることができなかった。
- ENOMEM
-
An attempt was made to create a child process in a PID namespace whose
"init" process has terminated. See pid_namespaces(7).
- ENOSYS
-
fork() はこのプラットフォームではサポートされていない
(例えば、メモリー管理ユニット (MMU) がないハードウェア)。
- ERESTARTNOINTR (Linux 2.6.17 以降)
-
System call was interrupted by a signal and will be restarted. (This can be
seen only during a trace.)
バージョン
C ライブラリとカーネルの違い
Since glibc 2.3.3, rather than invoking the kernel's fork() system call,
the glibc fork() wrapper that is provided as part of the NPTL threading
implementation invokes clone(2) with flags that provide the same effect
as the traditional system call. (A call to fork() is equivalent to a
call to clone(2) specifying flags as just SIGCHLD.) The glibc
wrapper invokes any fork handlers that have been established using
pthread_atfork(3).
Async-signal safety
_Fork(3) is an async-signal safe variant of fork(2).
標準規格
POSIX.1-2024.
歴史
4.3BSD, SVr4, POSIX.1-1988.
注意
Linux では、 fork() を 書き込み時コピー (copy-on-write) ページを用いて実装している。 したがって、fork
を行うことの唯一のデメリットは、 親プロセスのページテーブルを複製と 子プロセス自身のタスク構造の作成のための時間とメモリーが必要なことである。
例
See pipe(2) and wait(2) for more examples.
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void)
{
pid_t pid;
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
perror("signal");
exit(EXIT_FAILURE);
}
pid = fork();
switch (pid) {
case -1:
perror("fork");
exit(EXIT_FAILURE);
case 0:
puts("Child exiting.");
fflush(stdout);
_exit(EXIT_SUCCESS);
default:
printf("Child is PID %jd\n", (intmax_t) pid);
puts("Parent exiting.");
exit(EXIT_SUCCESS);
}
}
関連項目
clone(2), execve(2), exit(2), _exit(2), setrlimit(2),
unshare(2), vfork(2), wait(2), daemon(3), _Fork(3),
pthread_atfork(3), capabilities(7), credentials(7)
Index
- 名前
-
- ライブラリ
-
- 書式
-
- 説明
-
- 返り値
-
- エラー
-
- バージョン
-
- C ライブラリとカーネルの違い
-
- Async-signal safety
-
- 標準規格
-
- 歴史
-
- 注意
-
- 例
-
- 関連項目
-
This document was created by
man2html,
using the manual pages.