1FORK(3P)                   POSIX Programmer's Manual                  FORK(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       fork — create a new process
13

SYNOPSIS

15       #include <unistd.h>
16
17       pid_t fork(void);
18

DESCRIPTION

20       The fork() function shall create a new process. The new process  (child
21       process) shall be an exact copy of the calling process (parent process)
22       except as detailed below:
23
24        *  The child process shall have a unique process ID.
25
26        *  The child process ID also shall not match any active process  group
27           ID.
28
29        *  The  child  process shall have a different parent process ID, which
30           shall be the process ID of the calling process.
31
32        *  The child process shall have its own  copy  of  the  parent's  file
33           descriptors.  Each  of  the child's file descriptors shall refer to
34           the same open file description with the corresponding file descrip‐
35           tor of the parent.
36
37        *  The  child  process  shall  have  its own copy of the parent's open
38           directory streams. Each open directory stream in the child  process
39           may  share  directory  stream  positioning  with  the corresponding
40           directory stream of the parent.
41
42        *  The child process shall have its own copy of the  parent's  message
43           catalog descriptors.
44
45        *  The  child  process values of tms_utime, tms_stime, tms_cutime, and
46           tms_cstime shall be set to 0.
47
48        *  The time left until an alarm clock signal shall be reset  to  zero,
49           and the alarm, if any, shall be canceled; see alarm().
50
51        *  All semadj values shall be cleared.
52
53        *  File  locks set by the parent process shall not be inherited by the
54           child process.
55
56        *  The set of signals pending for the child process shall be  initial‐
57           ized to the empty set.
58
59        *  Interval timers shall be reset in the child process.
60
61        *  Any  semaphores  that  are open in the parent process shall also be
62           open in the child process.
63
64        *  The child process shall not inherit any address space memory  locks
65           established  by  the  parent  process  via  calls  to mlockall() or
66           mlock().
67
68        *  Memory mappings created in the parent  shall  be  retained  in  the
69           child process. MAP_PRIVATE mappings inherited from the parent shall
70           also be MAP_PRIVATE mappings in the child, and any modifications to
71           the  data  in  these  mappings  made by the parent prior to calling
72           fork() shall be visible to the child. Any modifications to the data
73           in  MAP_PRIVATE  mappings  made  by the parent after fork() returns
74           shall be visible only to the parent. Modifications to the  data  in
75           MAP_PRIVATE mappings made by the child shall be visible only to the
76           child.
77
78        *  For the SCHED_FIFO and  SCHED_RR  scheduling  policies,  the  child
79           process  shall inherit the policy and priority settings of the par‐
80           ent process during a fork() function. For  other  scheduling  poli‐
81           cies,  the  policy  and priority settings on fork() are implementa‐
82           tion-defined.
83
84        *  Per-process timers created by the parent shall not be inherited  by
85           the child process.
86
87        *  The  child  process  shall  have  its own copy of the message queue
88           descriptors of the parent. Each of the message descriptors  of  the
89           child shall refer to the same open message queue description as the
90           corresponding message descriptor of the parent.
91
92        *  No asynchronous input or asynchronous output  operations  shall  be
93           inherited  by  the  child  process. Any use of asynchronous control
94           blocks created by the parent produces undefined behavior.
95
96        *  A process shall be created  with  a  single  thread.  If  a  multi-
97           threaded  process  calls  fork(),  the  new process shall contain a
98           replica of the calling thread and its entire address space,  possi‐
99           bly  including  the  states  of mutexes and other resources. Conse‐
100           quently, to avoid errors, the child process may only execute async-
101           signal-safe operations until such time as one of the exec functions
102           is called.
103
104           When the application calls fork() from a signal handler and any  of
105           the  fork  handlers registered by pthread_atfork() calls a function
106           that is not async-signal-safe, the behavior is undefined.
107
108        *  If the Trace option and the Trace  Inherit  option  are  both  sup‐
109           ported:
110
111           If  the calling process was being traced in a trace stream that had
112           its inheritance policy  set  to  POSIX_TRACE_INHERITED,  the  child
113           process  shall  be  traced  into  that  trace stream, and the child
114           process shall inherit the parent's mapping of trace event names  to
115           trace  event  type  identifiers.  If  the trace stream in which the
116           calling process was being traced had its inheritance policy set  to
117           POSIX_TRACE_CLOSE_FOR_CHILD,  the child process shall not be traced
118           into that trace stream. The inheritance policy is set by a call  to
119           the posix_trace_attr_setinherited() function.
120
121        *  If  the  Trace option is supported, but the Trace Inherit option is
122           not supported:
123
124           The child process shall not be traced into any of the trace streams
125           of its parent process.
126
127        *  If the Trace option is supported, the child process of a trace con‐
128           troller process shall not control the trace streams  controlled  by
129           its parent process.
130
131        *  The  initial value of the CPU-time clock of the child process shall
132           be set to zero.
133
134        *  The initial value of the CPU-time clock of the single thread of the
135           child process shall be set to zero.
136
137       All  other process characteristics defined by POSIX.1‐2008 shall be the
138       same in the parent and child  processes.  The  inheritance  of  process
139       characteristics   not   defined   by  POSIX.1‐2008  is  unspecified  by
140       POSIX.1‐2008.
141
142       After fork(), both the parent and the child processes shall be  capable
143       of executing independently before either one terminates.
144

RETURN VALUE

146       Upon  successful completion, fork() shall return 0 to the child process
147       and shall return the process ID of the  child  process  to  the  parent
148       process. Both processes shall continue to execute from the fork() func‐
149       tion. Otherwise, -1 shall be returned to the parent process,  no  child
150       process shall be created, and errno shall be set to indicate the error.
151

ERRORS

153       The fork() function shall fail if:
154
155       EAGAIN The  system  lacked  the  necessary  resources to create another
156              process, or the system-imposed limit on the total number of pro‐
157              cesses   under   execution  system-wide  or  by  a  single  user
158              {CHILD_MAX} would be exceeded.
159
160       The fork() function may fail if:
161
162       ENOMEM Insufficient storage space is available.
163
164       The following sections are informative.
165

EXAMPLES

167       None.
168

APPLICATION USAGE

170       None.
171

RATIONALE

173       Many historical implementations have timing windows where a signal sent
174       to  a  process group (for example, an interactive SIGINT) just prior to
175       or during execution of fork() is delivered to the parent following  the
176       fork()  but not to the child because the fork() code clears the child's
177       set of pending signals. This volume of POSIX.1‐2017 does  not  require,
178       or  even permit, this behavior. However, it is pragmatic to expect that
179       problems of this nature may continue to exist in  implementations  that
180       appear  to  conform  to  this volume of POSIX.1‐2017 and pass available
181       verification suites. This behavior is only a consequence of the  imple‐
182       mentation  failing  to  make the interval between signal generation and
183       delivery totally invisible.   From  the  application's  perspective,  a
184       fork()  call  should appear atomic. A signal that is generated prior to
185       the fork() should be delivered prior to the fork().  A signal  sent  to
186       the  process  group after the fork() should be delivered to both parent
187       and child. The implementation may  actually  initialize  internal  data
188       structures  corresponding  to  the  child's  set  of pending signals to
189       include signals sent to the process group during the fork().  Since the
190       fork() call can be considered as atomic from the application's perspec‐
191       tive, the set would be initialized as empty and such signals would have
192       arrived after the fork(); see also <signal.h>.
193
194       One  approach  that has been suggested to address the problem of signal
195       inheritance across fork() is to add an [EINTR] error,  which  would  be
196       returned  when  a  signal  is  detected  during the call. While this is
197       preferable to losing signals, it was not considered  an  optimal  solu‐
198       tion.  Although  it  is not recommended for this purpose, such an error
199       would be an allowable extension for an implementation.
200
201       The [ENOMEM] error value is reserved  for  those  implementations  that
202       detect  and distinguish such a condition. This condition occurs when an
203       implementation detects that there is not enough memory  to  create  the
204       process. This is intended to be returned when [EAGAIN] is inappropriate
205       because there can never be enough memory (either primary  or  secondary
206       storage)  to perform the operation. Since fork() duplicates an existing
207       process, this must be a condition where there is sufficient memory  for
208       one  such  process,  but  not  for two. Many historical implementations
209       actually return [ENOMEM] due to temporary lack of memory, a  case  that
210       is  not generally distinct from [EAGAIN] from the perspective of a con‐
211       forming application.
212
213       Part of the reason for including the optional error [ENOMEM] is because
214       the SVID specifies it and it should be reserved for the error condition
215       specified there. The condition is not applicable  on  many  implementa‐
216       tions.
217
218       IEEE Std 1003.1‐1988  neglected  to require concurrent execution of the
219       parent and child of fork().  A system that single-threads processes was
220       clearly  not intended and is considered an unacceptable ``toy implemen‐
221       tation'' of this volume of POSIX.1‐2017.  The  only  objection  antici‐
222       pated  to  the  phrase  ``executing independently'' is testability, but
223       this assertion should be testable.  Such tests require  that  both  the
224       parent and child can block on a detectable action of the other, such as
225       a write to a pipe or a signal.  An interactive exchange of such actions
226       should be possible for the system to conform to the intent of this vol‐
227       ume of POSIX.1‐2017.
228
229       The [EAGAIN] error exists to warn applications that  such  a  condition
230       might  occur.   Whether  it occurs or not is not in any practical sense
231       under the control of the application because the condition is usually a
232       consequence  of  the user's use of the system, not of the application's
233       code. Thus, no application can or should rely upon its occurrence under
234       any  circumstances,  nor  should the exact semantics of what concept of
235       ``user'' is used be of concern to the application  developer.   Valida‐
236       tion writers should be cognizant of this limitation.
237
238       There are two reasons why POSIX programmers call fork().  One reason is
239       to create a new thread of control within the same  program  (which  was
240       originally only possible in POSIX by creating a new process); the other
241       is to create a new process running a different program. In  the  latter
242       case,  the call to fork() is soon followed by a call to one of the exec
243       functions.
244
245       The general problem with making fork() work in a  multi-threaded  world
246       is what to do with all of the threads.  There are two alternatives. One
247       is to copy all of the threads into the new  process.  This  causes  the
248       programmer or implementation to deal with threads that are suspended on
249       system calls or that might be about to execute system calls that should
250       not  be  executed in the new process.  The other alternative is to copy
251       only the thread that calls fork().  This creates  the  difficulty  that
252       the state of process-local resources is usually held in process memory.
253       If a thread that is not calling fork() holds a resource, that  resource
254       is  never released in the child process because the thread whose job it
255       is to release the resource does not exist in the child process.
256
257       When a programmer  is  writing  a  multi-threaded  program,  the  first
258       described  use  of fork(), creating new threads in the same program, is
259       provided by the pthread_create() function. The fork() function is  thus
260       used  only  to  run  new programs, and the effects of calling functions
261       that require certain resources between the call to fork() and the  call
262       to an exec function are undefined.
263
264       The  addition  of the forkall() function to the standard was considered
265       and rejected. The forkall() function lets all the threads in the parent
266       be  duplicated  in  the child. This essentially duplicates the state of
267       the parent in the child. This allows threads in the child  to  continue
268       processing  and  allows  locks  and  the  state to be preserved without
269       explicit pthread_atfork() code. The calling process has to ensure  that
270       the  threads  processing  state  that  is shared between the parent and
271       child (that is, file descriptors or MAP_SHARED memory) behaves properly
272       after forkall().  For example, if a thread is reading a file descriptor
273       in the parent when forkall() is called, then two threads  (one  in  the
274       parent  and one in the child) are reading the file descriptor after the
275       forkall().  If this is not desired behavior, the parent process has  to
276       synchronize with such threads before calling forkall().
277
278       While  the fork() function is async-signal-safe, there is no way for an
279       implementation to determine whether the fork  handlers  established  by
280       pthread_atfork()  are  async-signal-safe. The fork handlers may attempt
281       to execute portions of the implementation that  are  not  async-signal-
282       safe,  such  as those that are protected by mutexes, leading to a dead‐
283       lock condition.  It is therefore undefined for  the  fork  handlers  to
284       execute  functions that are not async-signal-safe when fork() is called
285       from a signal handler.
286
287       When forkall() is called, threads, other than the calling thread,  that
288       are  in  functions that can return with an [EINTR] error may have those
289       functions return [EINTR] if the implementation cannot ensure  that  the
290       function  behaves  correctly  in  the  parent and child. In particular,
291       pthread_cond_wait() and  pthread_cond_timedwait()  need  to  return  in
292       order  to  ensure  that the condition has not changed.  These functions
293       can be awakened by a spurious condition wakeup  rather  than  returning
294       [EINTR].
295

FUTURE DIRECTIONS

297       None.
298

SEE ALSO

300       alarm(),      exec,      fcntl(),      posix_trace_attr_getinherited(),
301       posix_trace_eventid_equal(),   pthread_atfork(),   semop(),   signal(),
302       times()
303
304       The  Base Definitions volume of POSIX.1‐2017, Section 4.12, Memory Syn‐
305       chronization, <sys_types.h>, <unistd.h>
306
308       Portions of this text are reprinted and reproduced in  electronic  form
309       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
310       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
311       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
312       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
313       event of any discrepancy between this version and the original IEEE and
314       The Open Group Standard, the original IEEE and The Open Group  Standard
315       is  the  referee document. The original Standard can be obtained online
316       at http://www.opengroup.org/unix/online.html .
317
318       Any typographical or formatting errors that appear  in  this  page  are
319       most likely to have been introduced during the conversion of the source
320       files to man page format. To report such errors,  see  https://www.ker
321       nel.org/doc/man-pages/reporting_bugs.html .
322
323
324
325IEEE/The Open Group                  2017                             FORK(3P)
Impressum