1fork(2)                       System Calls Manual                      fork(2)
2
3
4

NAME

6       fork - create a child process
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       pid_t fork(void);
15

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

156   C library/kernel differences
157       Since glibc 2.3.3, rather than  invoking  the  kernel's  fork()  system
158       call,  the  glibc  fork()  wrapper that is provided as part of the NPTL
159       threading implementation invokes clone(2) with flags that  provide  the
160       same  effect  as  the  traditional  system  call.  (A call to fork() is
161       equivalent to a call to clone(2) specifying  flags  as  just  SIGCHLD.)
162       The  glibc wrapper invokes any fork handlers that have been established
163       using pthread_atfork(3).
164

STANDARDS

166       POSIX.1-2008.
167

HISTORY

169       POSIX.1-2001, SVr4, 4.3BSD.
170

NOTES

172       Under Linux, fork() is implemented using copy-on-write  pages,  so  the
173       only  penalty  that it incurs is the time and memory required to dupli‐
174       cate the parent's page tables, and to create a  unique  task  structure
175       for the child.
176

EXAMPLES

178       See pipe(2) and wait(2) for more examples.
179
180       #include <signal.h>
181       #include <stdint.h>
182       #include <stdio.h>
183       #include <stdlib.h>
184       #include <unistd.h>
185
186       int
187       main(void)
188       {
189           pid_t pid;
190
191           if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
192               perror("signal");
193               exit(EXIT_FAILURE);
194           }
195           pid = fork();
196           switch (pid) {
197           case -1:
198               perror("fork");
199               exit(EXIT_FAILURE);
200           case 0:
201               puts("Child exiting.");
202               exit(EXIT_SUCCESS);
203           default:
204               printf("Child is PID %jd\n", (intmax_t) pid);
205               puts("Parent exiting.");
206               exit(EXIT_SUCCESS);
207           }
208       }
209

SEE ALSO

211       clone(2),   execve(2),  exit(2),  setrlimit(2),  unshare(2),  vfork(2),
212       wait(2), daemon(3), pthread_atfork(3), capabilities(7), credentials(7)
213
214
215
216Linux man-pages 6.04              2023-03-30                           fork(2)
Impressum