]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/fork.2
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[thirdparty/man-pages.git] / man2 / fork.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
4 .\" and Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu),
5 .\" March 28, 1992
6 .\"
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
27 .\" Modified by Michael Haardt (michael@moria.de)
28 .\" Modified Sat Jul 24 13:22:07 1993 by Rik Faith (faith@cs.unc.edu)
29 .\" Modified 21 Aug 1994 by Michael Chastain (mec@shell.portal.com):
30 .\" Referenced 'clone(2)'.
31 .\" Modified 1995-06-10, 1996-04-18, 1999-11-01, 2000-12-24
32 .\" by Andries Brouwer (aeb@cwi.nl)
33 .\" Modified, 27 May 2004, Michael Kerrisk <mtk-manpages@gmx.net>
34 .\" Added notes on capability requirements
35 .\" 2006-09-04, Michael Kerrisk
36 .\" Greatly expanded, to describe all attributes that differ
37 .\" parent and child.
38 .\"
39 .TH FORK 2 2006-09-04 "Linux 2.6.17" "Linux Programmer's Manual"
40 .SH NAME
41 fork \- create a child process
42 .SH SYNOPSIS
43 .B #include <sys/types.h>
44 .br
45 .B #include <unistd.h>
46 .sp
47 .B pid_t fork(void);
48 .SH DESCRIPTION
49 .BR fork ()
50 creates a new process by duplicating the calling process.
51 The new process, referred to as the \fIchild\fP,
52 is an exact duplicate of the calling process,
53 referred to as the \fIparent\fP, except for the following points:
54 .IP * 4
55 The child has its own unique process ID,
56 and this PID does not match the ID of any existing process group
57 .RB ( setpgid (2)).
58 .IP * 4
59 The child's parent process ID is the same as the parent's process ID.
60 .IP * 4
61 The child does not inherit its parent's memory locks
62 .RB ( mlock (2),
63 .BR mlockall (2)).
64 .IP * 4
65 Process resource utilisations
66 .RB ( getrusage (2))
67 and CPU time counters
68 .RB ( times (2))
69 are reset to zero in the child.
70 .IP * 4
71 The child set of pending signals is initially empty
72 .RB ( sigpending (2)).
73 .IP * 4
74 The child does not inherit semaphore adjustments from its parent
75 .RB ( semop (2)).
76 .IP * 4
77 The child does not inherit record locks from its parent
78 .RB ( fcntl (2)).
79 .IP * 4
80 The parent does not inherit timers from its parent
81 .RB ( setitimer (2)
82 .BR alarm (3),
83 .BR timer_create (3)).
84 .IP * 4
85 The child does not inherit outstanding asynchronous I/O operations
86 from its parent
87 .RB ( aio_read (3),
88 .BR aio_write (3)).
89 .PP
90 The process attributes in the preceding list are all specified
91 in POSIX.1-2001.
92 The parent and child also differ with respect to the following
93 Linux-specific process attributes:
94 .IP * 4
95 The child does not inherit directory change notifications (dnotify)
96 from its parent
97 (see the description of
98 .B F_NOTIFY
99 in
100 .BR fcntl (2)).
101 .IP * 4
102 The
103 .BR prctl (2)
104 .B PR_SET_PDEATHSIG
105 setting is reset so that the child does not receive a signal
106 when its parent terminates.
107 .IP * 4
108 Memory mappings that have been marked with the
109 .BR madvise (2)
110 .B MADV_DONTFORK
111 flag are not inherited across a
112 .BR fork (2).
113 .IP * 4
114 The termination signal of the child is always SIGCHLD
115 (see
116 .BR clone (2)).
117 .PP
118 Note the following further points:
119 .IP * 4
120 The child process is created with a single thread \(em the
121 one that called
122 .BR fork (2).
123 The entire virtual address space of the parent is replicated in the child,
124 including the states of mutexes, condition variables,
125 and other pthreads objects; the use of
126 .BR pthread_atfork (3)
127 may be helpful for dealing with problems that this can cause.
128 .IP * 4
129 The child inherits copies of the parent's set of open file descriptors.
130 Each file descriptor in the child refers to the same
131 open file description (see
132 .BR open (2))
133 as the corresponding file descriptor in the parent.
134 This means that the two descriptors share open file status flags,
135 current file offset,
136 and signal-driven I/O attributes (see the description of
137 .B F_SETOWN
138 and
139 .B F_SETSIG
140 in
141 .BR fcntl (2)).
142 .IP * 4
143 The child inherits copies of the parent's set of open message
144 queue descriptors (see
145 .BR mq_overview (7)).
146 Each descriptor in the child refers to the same
147 open message queue description
148 as the corresponding descriptor in the parent.
149 This means that the two descriptors share the same flags
150 .RI ( mq_flags ).
151 .SH "RETURN VALUE"
152 On success, the PID of the child process is returned in the parent's thread
153 of execution, and a 0 is returned in the child's thread of execution.
154 On failure, a \-1 will be returned in the parent's context,
155 no child process will be created, and
156 .I errno
157 will be set appropriately.
158 .SH ERRORS
159 .TP
160 .B EAGAIN
161 .BR fork ()
162 cannot allocate sufficient memory to copy the parent's page tables and
163 allocate a task structure for the child.
164 .TP
165 .B EAGAIN
166 It was not possible to create a new process because the caller's
167 .B RLIMIT_NPROC
168 resource limit was encountered.
169 To exceed this limit, the process must have either the
170 .BR CAP_SYS_ADMIN
171 or the
172 .BR CAP_SYS_RESOURCE
173 capability.
174 .TP
175 .B ENOMEM
176 .BR fork ()
177 failed to allocate the necessary kernel structures because memory is tight.
178 .SH "CONFORMING TO"
179 SVr4, 4.3BSD, POSIX.1-2001.
180 .SH EXAMPLE
181 See
182 .BR pipe (2)
183 and
184 .BR wait (2).
185 .SH NOTES
186 .PP
187 Under Linux,
188 .BR fork ()
189 is implemented using copy-on-write pages, so the only penalty that it incurs
190 is the time and memory required to duplicate the parent's page tables,
191 and to create a unique task structure for the child.
192 .SH "SEE ALSO"
193 .BR clone (2),
194 .BR execve (2),
195 .BR setrlimit (2),
196 .BR unshare (2),
197 .BR vfork (2),
198 .BR wait (2),
199 .BR capabilities (7)