1FORK(2)                    Linux Programmer's Manual                   FORK(2)
2
3
4

NAME

6       fork - create a child process
7

SYNOPSIS

9       #include <unistd.h>
10
11       pid_t fork(void);
12

DESCRIPTION

14       fork()  creates  a new process by duplicating the calling process.  The
15       new process is referred to as the child process.  The  calling  process
16       is referred to as the parent process.
17
18       The child process and the parent process run in separate memory spaces.
19       At the time of fork() both memory spaces have the same content.  Memory
20       writes,  file  mappings (mmap(2)), and unmappings (munmap(2)) performed
21       by one of the processes do not affect the other.
22
23       The child process is an exact duplicate of the  parent  process  except
24       for the following points:
25
26       *  The child has its own unique process ID, and this PID does not match
27          the ID of any existing process group (setpgid(2)) or session.
28
29       *  The child's parent process ID is the same as  the  parent's  process
30          ID.
31
32       *  The  child  does  not  inherit  its parent's memory locks (mlock(2),
33          mlockall(2)).
34
35       *  Process resource utilizations (getrusage(2)) and CPU  time  counters
36          (times(2)) are reset to zero in the child.
37
38       *  The  child's  set  of  pending  signals is initially empty (sigpend‐
39          ing(2)).
40
41       *  The child does not inherit semaphore  adjustments  from  its  parent
42          (semop(2)).
43
44       *  The  child does not inherit process-associated record locks from its
45          parent (fcntl(2)).  (On the other hand,  it  does  inherit  fcntl(2)
46          open file description locks and flock(2) locks from its parent.)
47
48       *  The  child  does  not  inherit timers from its parent (setitimer(2),
49          alarm(2), timer_create(2)).
50
51       *  The child does not inherit outstanding asynchronous  I/O  operations
52          from its parent (aio_read(3), aio_write(3)), nor does it inherit any
53          asynchronous I/O contexts from its parent (see io_setup(2)).
54
55       The process attributes in the  preceding  list  are  all  specified  in
56       POSIX.1.   The parent and child also differ with respect to the follow‐
57       ing Linux-specific process attributes:
58
59       *  The child does not inherit directory change notifications  (dnotify)
60          from its parent (see the description of F_NOTIFY in fcntl(2)).
61
62       *  The  prctl(2)  PR_SET_PDEATHSIG  setting  is reset so that the child
63          does not receive a signal when its parent terminates.
64
65       *  The default timer slack value is set to the parent's  current  timer
66          slack value.  See the description of PR_SET_TIMERSLACK in prctl(2).
67
68       *  Memory mappings that have been marked with the madvise(2) MADV_DONT‐
69          FORK flag are not inherited across a fork().
70
71       *  Memory in address ranges that have been marked with  the  madvise(2)
72          MADV_WIPEONFORK  flag  is  zeroed in the child after a fork().  (The
73          MADV_WIPEONFORK setting remains in place for those address ranges in
74          the child.)
75
76       *  The   termination  signal  of  the  child  is  always  SIGCHLD  (see
77          clone(2)).
78
79       *  The port access permission bits set by ioperm(2) are  not  inherited
80          by the child; the child must turn on any bits that it requires using
81          ioperm(2).
82
83       Note the following further points:
84
85       *  The child process is created  with  a  single  thread—the  one  that
86          called  fork().   The  entire virtual address space of the parent is
87          replicated in the child, including the states of mutexes,  condition
88          variables,  and other pthreads objects; the use of pthread_atfork(3)
89          may be helpful for dealing with problems that this can cause.
90
91       *  After a fork() in a multithreaded program, the child can safely call
92          only  async-signal-safe  functions (see signal-safety(7)) until such
93          time as it calls execve(2).
94
95       *  The child inherits copies of the parent's set of open file  descrip‐
96          tors.   Each  file  descriptor  in the child refers to the same open
97          file description (see open(2)) as the corresponding file  descriptor
98          in  the parent.  This means that the two file descriptors share open
99          file status flags, file offset,  and  signal-driven  I/O  attributes
100          (see the description of F_SETOWN and F_SETSIG in fcntl(2)).
101
102       *  The  child inherits copies of the parent's set of open message queue
103          descriptors (see mq_overview(7)).  Each file descriptor in the child
104          refers to the same open message queue description as the correspond‐
105          ing file descriptor in the parent.  This means that the two file de‐
106          scriptors share the same flags (mq_flags).
107
108       *  The  child  inherits  copies  of  the parent's set of open directory
109          streams (see opendir(3)).  POSIX.1 says that the  corresponding  di‐
110          rectory  streams  in  the  parent  and child may share the directory
111          stream positioning; on Linux/glibc they do not.
112

RETURN VALUE

114       On success, the PID of the child process is returned in the parent, and
115       0  is returned in the child.  On failure, -1 is returned in the parent,
116       no child process is created, and errno is set to indicate the error.
117

ERRORS

119       EAGAIN A system-imposed limit on the number of threads was encountered.
120              There are a number of limits that may trigger this error:
121
122              *  the  RLIMIT_NPROC soft resource limit (set via setrlimit(2)),
123                 which limits the number of processes and threads for  a  real
124                 user ID, was reached;
125
126              *  the kernel's system-wide limit on the number of processes and
127                 threads,  /proc/sys/kernel/threads-max,  was   reached   (see
128                 proc(5));
129
130              *  the  maximum  number  of  PIDs, /proc/sys/kernel/pid_max, was
131                 reached (see proc(5)); or
132
133              *  the PID limit (pids.max) imposed by the cgroup "process  num‐
134                 ber" (PIDs) controller was reached.
135
136       EAGAIN The caller is operating under the SCHED_DEADLINE scheduling pol‐
137              icy and does not have the reset-on-fork flag set.  See sched(7).
138
139       ENOMEM fork() failed to allocate the necessary  kernel  structures  be‐
140              cause memory is tight.
141
142       ENOMEM An attempt was made to create a child process in a PID namespace
143              whose "init" process has terminated.  See pid_namespaces(7).
144
145       ENOSYS fork() is not supported on this platform (for example,  hardware
146              without a Memory-Management Unit).
147
148       ERESTARTNOINTR (since Linux 2.6.17)
149              System  call  was interrupted by a signal and will be restarted.
150              (This can be seen only during a trace.)
151

CONFORMING TO

153       POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
154

NOTES

156       Under Linux, fork() is implemented using copy-on-write  pages,  so  the
157       only  penalty  that it incurs is the time and memory required to dupli‐
158       cate the parent's page tables, and to create a  unique  task  structure
159       for the child.
160
161   C library/kernel differences
162       Since  version  2.3.3,  rather than invoking the kernel's fork() system
163       call, the glibc fork() wrapper that is provided as  part  of  the  NPTL
164       threading  implementation  invokes clone(2) with flags that provide the
165       same effect as the traditional system  call.   (A  call  to  fork()  is
166       equivalent  to  a  call  to clone(2) specifying flags as just SIGCHLD.)
167       The glibc wrapper invokes any fork handlers that have been  established
168       using pthread_atfork(3).
169

EXAMPLES

171       See pipe(2) and wait(2).
172

SEE ALSO

174       clone(2),   execve(2),  exit(2),  setrlimit(2),  unshare(2),  vfork(2),
175       wait(2), daemon(3), pthread_atfork(3), capabilities(7), credentials(7)
176

COLOPHON

178       This page is part of release 5.13 of the Linux  man-pages  project.   A
179       description  of  the project, information about reporting bugs, and the
180       latest    version    of    this    page,    can     be     found     at
181       https://www.kernel.org/doc/man-pages/.
182
183
184
185Linux                             2021-03-22                           FORK(2)
Impressum