]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Hey Emacs! This file is -*- nroff -*- source. |
2 | .\" | |
3 | .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992 | |
1130df60 | 4 | .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005 |
fea681da MK |
5 | .\" May be distributed under the GNU General Public License. |
6 | .\" Modified by Michael Haardt <michael@moria.de> | |
7 | .\" Modified 24 Jul 1993 by Rik Faith <faith@cs.unc.edu> | |
8 | .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>: | |
9 | .\" New man page (copied from 'fork.2'). | |
10 | .\" Modified 10 June 1995 by Andries Brouwer <aeb@cwi.nl> | |
11 | .\" Modified 25 April 1998 by Xavier Leroy <Xavier.Leroy@inria.fr> | |
12 | .\" Modified 26 Jun 2001 by Michael Kerrisk | |
13 | .\" Mostly upgraded to 2.4.x | |
14 | .\" Added prototype for sys_clone() plus description | |
15 | .\" Added CLONE_THREAD with a brief description of thread groups | |
c13182ef | 16 | .\" Added CLONE_PARENT and revised entire page remove ambiguity |
fea681da MK |
17 | .\" between "calling process" and "parent process" |
18 | .\" Added CLONE_PTRACE and CLONE_VFORK | |
19 | .\" Added EPERM and EINVAL error codes | |
fd8a5be4 | 20 | .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>) |
fea681da | 21 | .\" various other minor tidy ups and clarifications. |
c11b1abf | 22 | .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com> |
d9bfdb9c | 23 | .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD |
c11b1abf | 24 | .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
25 | .\" Added description for CLONE_NEWNS, which was added in 2.4.19 |
26 | .\" Slightly rephrased, aeb. | |
27 | .\" Modified 1 Feb 2003 - added CLONE_SIGHAND restriction, aeb. | |
28 | .\" Modified 1 Jan 2004 - various updates, aeb | |
0967c11f | 29 | .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb. |
d9bfdb9c | 30 | .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid() |
31830ef0 | 31 | .\" wrapper under BUGS. |
fd8a5be4 MK |
32 | .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED. |
33 | .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD. | |
82ee147a MK |
34 | .\" 2008-11-18, mtk, order CLONE_* flags alphabetically |
35 | .\" 2008-11-18, mtk, document CLONE_NEWPID | |
43ce9dda | 36 | .\" 2008-11-19, mtk, document CLONE_NEWUTS |
667417b3 | 37 | .\" 2008-11-19, mtk, document CLONE_NEWIPC |
cfdc761b | 38 | .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO |
fea681da | 39 | .\" |
185341d4 MK |
40 | .\" FIXME Document CLONE_NEWUSER, which is new in 2.6.23 |
41 | .\" (also supported for unshare()?) | |
cf1c6c80 | 42 | .\" FIXME Document CLONE_NEWNET, new in Linux 2.6.24 |
6807c79a | 43 | .\" FIXME . 2.6.25 marks the unused CLONE_STOPPED as obsolete, and it will |
21a0b03d | 44 | .\" probably be removed in the future. |
360ed6b3 | 45 | .\" |
732e54dd | 46 | .TH CLONE 2 2008-11-20 "Linux" "Linux Programmer's Manual" |
fea681da | 47 | .SH NAME |
9b0e0996 | 48 | clone, __clone2 \- create a child process |
fea681da | 49 | .SH SYNOPSIS |
c10859eb | 50 | .nf |
cc4615cc MK |
51 | .B #define _GNU_SOURCE |
52 | .\" Actually _BSD_SOURCE || _SVID_SOURCE | |
53 | .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749 | |
fea681da | 54 | .B #include <sched.h> |
c10859eb | 55 | |
ff929e3b MK |
56 | .BI "int clone(int (*" "fn" ")(void *), void *" child_stack , |
57 | .BI " int " flags ", void *" "arg" ", ... " | |
58 | .BI " /* pid_t *" pid ", struct user_desc *" tls \ | |
59 | ", pid_t *" ctid " */ );" | |
c10859eb | 60 | .fi |
fea681da | 61 | .SH DESCRIPTION |
edcc65ff MK |
62 | .BR clone () |
63 | creates a new process, in a manner similar to | |
fea681da | 64 | .BR fork (2). |
735f354f | 65 | It is actually a library function layered on top of the underlying |
e511ffb6 | 66 | .BR clone () |
fea681da MK |
67 | system call, hereinafter referred to as |
68 | .BR sys_clone . | |
69 | A description of | |
0daa9e92 | 70 | .B sys_clone |
fea681da MK |
71 | is given towards the end of this page. |
72 | ||
73 | Unlike | |
74 | .BR fork (2), | |
c13182ef | 75 | these calls |
fea681da MK |
76 | allow the child process to share parts of its execution context with |
77 | the calling process, such as the memory space, the table of file | |
c13182ef MK |
78 | descriptors, and the table of signal handlers. |
79 | (Note that on this manual | |
80 | page, "calling process" normally corresponds to "parent process". | |
81 | But see the description of | |
82 | .B CLONE_PARENT | |
fea681da MK |
83 | below.) |
84 | ||
85 | The main use of | |
edcc65ff | 86 | .BR clone () |
fea681da MK |
87 | is to implement threads: multiple threads of control in a program that |
88 | run concurrently in a shared memory space. | |
89 | ||
90 | When the child process is created with | |
c13182ef | 91 | .BR clone (), |
fea681da MK |
92 | it executes the function |
93 | application | |
c13182ef | 94 | .IR fn ( arg ). |
fea681da | 95 | (This differs from |
c13182ef | 96 | .BR fork (2), |
fea681da | 97 | where execution continues in the child from the point |
c13182ef MK |
98 | of the |
99 | .BR fork (2) | |
fea681da MK |
100 | call.) |
101 | The | |
102 | .I fn | |
103 | argument is a pointer to a function that is called by the child | |
104 | process at the beginning of its execution. | |
105 | The | |
106 | .I arg | |
107 | argument is passed to the | |
108 | .I fn | |
109 | function. | |
110 | ||
c13182ef | 111 | When the |
fea681da | 112 | .IR fn ( arg ) |
c13182ef MK |
113 | function application returns, the child process terminates. |
114 | The integer returned by | |
fea681da | 115 | .I fn |
c13182ef MK |
116 | is the exit code for the child process. |
117 | The child process may also terminate explicitly by calling | |
fea681da MK |
118 | .BR exit (2) |
119 | or after receiving a fatal signal. | |
120 | ||
121 | The | |
122 | .I child_stack | |
c13182ef MK |
123 | argument specifies the location of the stack used by the child process. |
124 | Since the child and calling process may share memory, | |
fea681da | 125 | it is not possible for the child process to execute in the |
c13182ef MK |
126 | same stack as the calling process. |
127 | The calling process must therefore | |
fea681da MK |
128 | set up memory space for the child stack and pass a pointer to this |
129 | space to | |
edcc65ff | 130 | .BR clone (). |
fea681da MK |
131 | Stacks grow downwards on all processors that run Linux |
132 | (except the HP PA processors), so | |
133 | .I child_stack | |
134 | usually points to the topmost address of the memory space set up for | |
135 | the child stack. | |
136 | ||
137 | The low byte of | |
138 | .I flags | |
fd8a5be4 MK |
139 | contains the number of the |
140 | .I "termination signal" | |
141 | sent to the parent when the child dies. | |
142 | If this signal is specified as anything other than | |
fea681da MK |
143 | .BR SIGCHLD , |
144 | then the parent process must specify the | |
c13182ef MK |
145 | .B __WALL |
146 | or | |
fea681da | 147 | .B __WCLONE |
c13182ef MK |
148 | options when waiting for the child with |
149 | .BR wait (2). | |
fea681da MK |
150 | If no signal is specified, then the parent process is not signaled |
151 | when the child terminates. | |
152 | ||
153 | .I flags | |
fd8a5be4 MK |
154 | may also be bitwise-or'ed with zero or more of the following constants, |
155 | in order to specify what is shared between the calling process | |
fea681da | 156 | and the child process: |
fea681da | 157 | .TP |
f5dbc7c8 MK |
158 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" |
159 | Erase child thread ID at location | |
160 | .I child_tidptr | |
161 | in child memory when the child exits, and do a wakeup on the futex | |
162 | at that address. | |
163 | The address involved may be changed by the | |
164 | .BR set_tid_address (2) | |
165 | system call. | |
166 | This is used by threading libraries. | |
167 | .TP | |
168 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
169 | Store child thread ID at location | |
170 | .I child_tidptr | |
171 | in child memory. | |
172 | .TP | |
173 | .B CLONE_FILES | |
fea681da | 174 | If |
f5dbc7c8 MK |
175 | .B CLONE_FILES |
176 | is set, the calling process and the child process share the same file | |
177 | descriptor table. | |
178 | Any file descriptor created by the calling process or by the child | |
179 | process is also valid in the other process. | |
180 | Similarly, if one of the processes closes a file descriptor, | |
181 | or changes its associated flags (using the | |
182 | .BR fcntl (2) | |
183 | .B F_SETFD | |
184 | operation), the other process is also affected. | |
fea681da MK |
185 | |
186 | If | |
f5dbc7c8 MK |
187 | .B CLONE_FILES |
188 | is not set, the child process inherits a copy of all file descriptors | |
189 | opened in the calling process at the time of | |
190 | .BR clone (). | |
191 | (The duplicated file descriptors in the child refer to the | |
192 | same open file descriptions (see | |
193 | .BR open (2)) | |
194 | as the corresponding file descriptors in the calling process.) | |
195 | Subsequent operations that open or close file descriptors, | |
196 | or change file descriptor flags, | |
197 | performed by either the calling | |
198 | process or the child process do not affect the other process. | |
fea681da MK |
199 | .TP |
200 | .B CLONE_FS | |
201 | If | |
202 | .B CLONE_FS | |
314c8ff4 | 203 | is set, the caller and the child process share the same file system |
c13182ef MK |
204 | information. |
205 | This includes the root of the file system, the current | |
206 | working directory, and the umask. | |
207 | Any call to | |
fea681da MK |
208 | .BR chroot (2), |
209 | .BR chdir (2), | |
210 | or | |
211 | .BR umask (2) | |
edcc65ff | 212 | performed by the calling process or the child process also affects the |
fea681da MK |
213 | other process. |
214 | ||
c13182ef | 215 | If |
fea681da MK |
216 | .B CLONE_FS |
217 | is not set, the child process works on a copy of the file system | |
218 | information of the calling process at the time of the | |
edcc65ff | 219 | .BR clone () |
fea681da MK |
220 | call. |
221 | Calls to | |
222 | .BR chroot (2), | |
223 | .BR chdir (2), | |
224 | .BR umask (2) | |
225 | performed later by one of the processes do not affect the other process. | |
fea681da | 226 | .TP |
a4cc375e | 227 | .BR CLONE_IO " (since Linux 2.6.25)" |
11f27a1c JA |
228 | If |
229 | .B CLONE_IO | |
230 | is set, then the new process shares an I/O context with | |
231 | the calling process. | |
232 | If this flag is not set, then (as with | |
233 | .BR fork (2)) | |
234 | the new process has its own I/O context. | |
235 | ||
236 | .\" The following based on text from Jens Axboe | |
237 | The I/O context is the I/O scope of the disk scheduler (i.e, | |
238 | what the I/O scheduler uses to model scheduling of a process's I/O). | |
239 | If processes share the same I/O context, | |
240 | they are treated as one by the I/O scheduler. | |
241 | As a consequence, they get to share disk time. | |
242 | For some I/O schedulers, | |
243 | .\" the anticipatory and CFQ scheduler | |
244 | if two processes share an I/O context, | |
245 | they will be allowed to interleave their disk access. | |
246 | If several threads are doing I/O on behalf of the same process | |
247 | .RB ( aio_read (3), | |
248 | for instance), they should employ | |
249 | .BR CLONE_IO | |
250 | to get better I/O performance. | |
251 | .\" with CFQ and AS. | |
252 | ||
253 | If the kernel is not configured with the | |
254 | .B CONFIG_BLOCK | |
255 | option, this flag is a no-op. | |
256 | .TP | |
667417b3 MK |
257 | .BR CLONE_NEWIPC " (since Linux 2.4.19)" |
258 | If | |
259 | .B CLONE_NEWIPC | |
260 | is set, then create the process in a new IPC namespace. | |
261 | If this flag is not set, then (as with | |
262 | .BR fork (2)), | |
263 | the process is created in the same IPC namespace as | |
264 | the calling process. | |
0236bea9 | 265 | This flag is intended for the implementation of containers. |
667417b3 MK |
266 | |
267 | An IPC namespace consists of the set of identifiers for | |
268 | System V IPC objects. | |
269 | (These objects are created using | |
270 | .BR msgctl (2), | |
271 | .BR semctl (2), | |
272 | and | |
273 | .BR shmctl (2)). | |
274 | Objects created in an IPC namespace are visible to other processes | |
275 | that are members of that namespace, | |
276 | but are not visible to processes in other IPC namespaces. | |
277 | ||
83c1f4b5 MK |
278 | When an IPC namespace is destroyed |
279 | (i.e, when the last process that is a member of the namespace terminates), | |
280 | all IPC objects in the namespace are automatically destroyed. | |
281 | ||
667417b3 MK |
282 | Use of this flag requires: a kernel configured with the |
283 | .B CONFIG_SYSVIPC | |
284 | and | |
285 | .B CONFIG_IPC_NS | |
286 | configuration options and that the process be privileged | |
287 | .RB ( CAP_SYS_ADMIN ). | |
288 | This flag can't be specified in conjunction with | |
289 | .BR CLONE_SYSVSEM . | |
290 | .TP | |
c10859eb | 291 | .BR CLONE_NEWNS " (since Linux 2.4.19)" |
732e54dd | 292 | Start the child in a new mount namespace. |
fea681da | 293 | |
732e54dd | 294 | Every process lives in a mount namespace. |
c13182ef | 295 | The |
fea681da MK |
296 | .I namespace |
297 | of a process is the data (the set of mounts) describing the file hierarchy | |
c13182ef MK |
298 | as seen by that process. |
299 | After a | |
fea681da MK |
300 | .BR fork (2) |
301 | or | |
2777b1ca | 302 | .BR clone () |
fea681da MK |
303 | where the |
304 | .B CLONE_NEWNS | |
732e54dd | 305 | flag is not set, the child lives in the same mount |
4df2eb09 | 306 | namespace as the parent. |
fea681da MK |
307 | The system calls |
308 | .BR mount (2) | |
309 | and | |
310 | .BR umount (2) | |
732e54dd | 311 | change the mount namespace of the calling process, and hence affect |
fea681da | 312 | all processes that live in the same namespace, but do not affect |
732e54dd | 313 | processes in a different mount namespace. |
fea681da MK |
314 | |
315 | After a | |
2777b1ca | 316 | .BR clone () |
fea681da MK |
317 | where the |
318 | .B CLONE_NEWNS | |
732e54dd | 319 | flag is set, the cloned child is started in a new mount namespace, |
fea681da MK |
320 | initialized with a copy of the namespace of the parent. |
321 | ||
0b9bdf82 | 322 | Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability) |
fea681da MK |
323 | may specify the |
324 | .B CLONE_NEWNS | |
325 | flag. | |
326 | It is not permitted to specify both | |
327 | .B CLONE_NEWNS | |
328 | and | |
329 | .B CLONE_FS | |
330 | in the same | |
e511ffb6 | 331 | .BR clone () |
fea681da | 332 | call. |
fea681da | 333 | .TP |
82ee147a MK |
334 | .BR CLONE_NEWPID " (since Linux 2.6.24)" |
335 | .\" This explanation draws a lot of details from | |
336 | .\" http://lwn.net/Articles/259217/ | |
337 | .\" Authors: Pavel Emelyanov <xemul@openvz.org> | |
338 | .\" and Kir Kolyshkin <kir@openvz.org> | |
339 | .\" | |
340 | .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264 | |
341 | .\" Author: Pavel Emelyanov <xemul@openvz.org> | |
342 | If | |
5c95e5e8 | 343 | .B CLONE_NEWPID |
82ee147a MK |
344 | is set, then create the process in a new PID namespace. |
345 | If this flag is not set, then (as with | |
346 | .BR fork (2)), | |
347 | the process is created in the same PID namespace as | |
348 | the calling process. | |
0236bea9 | 349 | This flag is intended for the implementation of containers. |
82ee147a MK |
350 | |
351 | A PID namespace provides an isolated environment for PIDs: | |
352 | PIDs in a new namespace start at 1, | |
353 | somewhat like a standalone system, and calls to | |
354 | .BR fork (2), | |
355 | .BR vfork (2), | |
356 | or | |
357 | .BR clone (2) | |
358 | will produce processes whose PIDs within the namespace | |
359 | are only guaranteed to be unique within that namespace. | |
360 | ||
361 | The first process created in a new namespace | |
362 | (i.e., the process created using the | |
363 | .BR CLONE_NEWPID | |
364 | flag) has the PID 1, and is the "init" process for the namespace. | |
365 | Children that are orphaned within the namespace will be reparented | |
366 | to this process rather than | |
367 | .BR init (8). | |
368 | Unlike the traditional | |
369 | .B init | |
370 | process, the "init" process of a PID namespace can terminate, | |
371 | and if it does, all of the processes in the namespace are terminated. | |
372 | ||
373 | PID namespaces form a hierarchy. | |
374 | When a PID new namespace is created, | |
375 | the PIDs of the processes in that namespace are visible | |
376 | in the PID namespace of the process that created the new namespace; | |
377 | analogously, if the parent PID namespace is itself | |
378 | the child of another PID namespace, | |
379 | then PIDs of the child and parent PID namespaces will both be | |
380 | visible in the grandparent PID namespace. | |
381 | Conversely, the processes in the "child" PID namespace do not see | |
382 | the PIDs of the processes in the parent namespace. | |
383 | The existence of a namespace hierarchy means that each process | |
384 | may now have multiple PIDs: | |
385 | one for each namespace in which it is visible. | |
386 | (A call to | |
387 | .BR getpid (2) | |
388 | always returns the PID associated with the namespace in which | |
389 | the process was created.) | |
390 | ||
391 | After creating the new namespace, | |
392 | it is useful for the child to change its root directory | |
393 | and mount a new procfs instance at | |
394 | .I /proc | |
395 | so that tools such as | |
396 | .BR ps (1) | |
397 | work correctly. | |
398 | .\" mount -t proc proc /proc | |
399 | ||
400 | Use of this flag requires: a kernel configured with the | |
401 | .B CONFIG_PID_NS | |
402 | configuration option and that the process be privileged | |
af3c728e | 403 | .RB ( CAP_SYS_ADMIN ). |
82ee147a MK |
404 | This flag can't be specified in conjunction with |
405 | .BR CLONE_THREAD . | |
406 | .TP | |
43ce9dda MK |
407 | .BR CLONE_NEWUTS " (since Linux 2.6.19)" |
408 | If | |
409 | .B CLONE_NEWUTS | |
410 | is set, then create the process in a new UTS namespace. | |
411 | If this flag is not set, then (as with | |
412 | .BR fork (2)), | |
413 | the process is created in the same UTS namespace as | |
414 | the calling process. | |
0236bea9 | 415 | This flag is intended for the implementation of containers. |
43ce9dda MK |
416 | |
417 | A UTS namespace is the set of identifiers returned by | |
418 | .BR uname (2); | |
419 | among these, the domain name and the host name can be modified by | |
420 | .BR setdomainname (2) | |
421 | and | |
422 | .BR | |
423 | .BR sethostname (2), | |
424 | respectively. | |
425 | Changes made to these identifiers in one UTS namespace | |
426 | are visible to other processes in the same namespace, | |
427 | but are not visible to processes in other UTS namespaces. | |
428 | ||
429 | Use of this flag requires: a kernel configured with the | |
430 | .B CONFIG_UTS_NS | |
431 | configuration option and that the process be privileged | |
432 | .RB ( CAP_SYS_ADMIN ). | |
433 | .TP | |
f5dbc7c8 MK |
434 | .BR CLONE_PARENT " (since Linux 2.3.12)" |
435 | If | |
436 | .B CLONE_PARENT | |
437 | is set, then the parent of the new child (as returned by | |
438 | .BR getppid (2)) | |
439 | will be the same as that of the calling process. | |
440 | ||
441 | If | |
442 | .B CLONE_PARENT | |
443 | is not set, then (as with | |
444 | .BR fork (2)) | |
445 | the child's parent is the calling process. | |
446 | ||
447 | Note that it is the parent process, as returned by | |
448 | .BR getppid (2), | |
449 | which is signaled when the child terminates, so that | |
450 | if | |
451 | .B CLONE_PARENT | |
452 | is set, then the parent of the calling process, rather than the | |
453 | calling process itself, will be signaled. | |
454 | .TP | |
455 | .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)" | |
456 | Store child thread ID at location | |
457 | .I parent_tidptr | |
458 | in parent and child memory. | |
459 | (In Linux 2.5.32-2.5.48 there was a flag | |
460 | .B CLONE_SETTID | |
461 | that did this.) | |
462 | .TP | |
463 | .BR CLONE_PID " (obsolete)" | |
464 | If | |
465 | .B CLONE_PID | |
466 | is set, the child process is created with the same process ID as | |
467 | the calling process. | |
468 | This is good for hacking the system, but otherwise | |
469 | of not much use. | |
470 | Since 2.3.21 this flag can be | |
471 | specified only by the system boot process (PID 0). | |
472 | It disappeared in Linux 2.5.16. | |
473 | .TP | |
474 | .B CLONE_PTRACE | |
475 | If | |
476 | .B CLONE_PTRACE | |
477 | is specified, and the calling process is being traced, | |
478 | then trace the child also (see | |
479 | .BR ptrace (2)). | |
480 | .TP | |
481 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
482 | The | |
483 | .I newtls | |
484 | argument is the new TLS (Thread Local Storage) descriptor. | |
485 | (See | |
486 | .BR set_thread_area (2).) | |
487 | .TP | |
fea681da MK |
488 | .B CLONE_SIGHAND |
489 | If | |
490 | .B CLONE_SIGHAND | |
314c8ff4 | 491 | is set, the calling process and the child process share the same table of |
c13182ef MK |
492 | signal handlers. |
493 | If the calling process or child process calls | |
fea681da | 494 | .BR sigaction (2) |
c13182ef MK |
495 | to change the behavior associated with a signal, the behavior is |
496 | changed in the other process as well. | |
497 | However, the calling process and child | |
fea681da | 498 | processes still have distinct signal masks and sets of pending |
c13182ef MK |
499 | signals. |
500 | So, one of them may block or unblock some signals using | |
fea681da MK |
501 | .BR sigprocmask (2) |
502 | without affecting the other process. | |
503 | ||
504 | If | |
505 | .B CLONE_SIGHAND | |
506 | is not set, the child process inherits a copy of the signal handlers | |
507 | of the calling process at the time | |
edcc65ff | 508 | .BR clone () |
c13182ef MK |
509 | is called. |
510 | Calls to | |
fea681da MK |
511 | .BR sigaction (2) |
512 | performed later by one of the processes have no effect on the other | |
513 | process. | |
29546c24 MK |
514 | |
515 | Since Linux 2.6.0-test6, | |
516 | .I flags | |
517 | must also include | |
518 | .B CLONE_VM | |
519 | if | |
520 | .B CLONE_SIGHAND | |
521 | is specified | |
fea681da | 522 | .TP |
a69b6bda MK |
523 | .BR CLONE_STOPPED " (since Linux 2.6.0-test2)" |
524 | If | |
525 | .B CLONE_STOPPED | |
526 | is set, then the child is initially stopped (as though it was sent a | |
527 | .B SIGSTOP | |
528 | signal), and must be resumed by sending it a | |
529 | .B SIGCONT | |
530 | signal. | |
ef37eaf2 MK |
531 | |
532 | .I "From Linux 2.6.25 this flag is deprecated." | |
533 | You probably never wanted to use it, | |
53c94269 | 534 | you certainly shouldn't be using it, and soon it will go away. |
a5a061ee | 535 | .\" glibc 2.8 removed this defn from bits/sched.h |
a69b6bda | 536 | .TP |
f5dbc7c8 | 537 | .BR CLONE_SYSVSEM " (since Linux 2.5.10)" |
fea681da | 538 | If |
f5dbc7c8 MK |
539 | .B CLONE_SYSVSEM |
540 | is set, then the child and the calling process share | |
541 | a single list of System V semaphore undo values (see | |
542 | .BR semop (2)). | |
543 | If this flag is not set, then the child has a separate undo list, | |
544 | which is initially empty. | |
fea681da MK |
545 | .TP |
546 | .BR CLONE_THREAD " (since Linux 2.4.0-test8)" | |
547 | If | |
548 | .B CLONE_THREAD | |
549 | is set, the child is placed in the same thread group as the calling process. | |
fd8a5be4 MK |
550 | To make the remainder of the discussion of |
551 | .B CLONE_THREAD | |
552 | more readable, the term "thread" is used to refer to the | |
553 | processes within a thread group. | |
fea681da | 554 | |
fd8a5be4 MK |
555 | Thread groups were a feature added in Linux 2.4 to support the |
556 | POSIX threads notion of a set of threads that share a single PID. | |
557 | Internally, this shared PID is the so-called | |
558 | thread group identifier (TGID) for the thread group. | |
c13182ef | 559 | Since Linux 2.4, calls to |
fea681da | 560 | .BR getpid (2) |
fd8a5be4 MK |
561 | return the TGID of the caller. |
562 | ||
563 | The threads within a group can be distinguished by their (system-wide) | |
564 | unique thread IDs (TID). | |
565 | A new thread's TID is available as the function result | |
566 | returned to the caller of | |
567 | .BR clone (), | |
568 | and a thread can obtain | |
569 | its own TID using | |
570 | .BR gettid (2). | |
571 | ||
c13182ef | 572 | When a call is made to |
fd8a5be4 MK |
573 | .BR clone () |
574 | without specifying | |
575 | .BR CLONE_THREAD , | |
576 | then the resulting thread is placed in a new thread group | |
577 | whose TGID is the same as the thread's TID. | |
578 | This thread is the | |
579 | .I leader | |
580 | of the new thread group. | |
581 | ||
582 | A new thread created with | |
583 | .B CLONE_THREAD | |
584 | has the same parent process as the caller of | |
585 | .BR clone () | |
c13182ef | 586 | (i.e., like |
fd8a5be4 MK |
587 | .BR CLONE_PARENT ), |
588 | so that calls to | |
589 | .BR getppid (2) | |
590 | return the same value for all of the threads in a thread group. | |
591 | When a | |
c13182ef | 592 | .B CLONE_THREAD |
fd8a5be4 MK |
593 | thread terminates, the thread that created it using |
594 | .BR clone () | |
595 | is not sent a | |
596 | .B SIGCHLD | |
597 | (or other termination) signal; | |
598 | nor can the status of such a thread be obtained | |
599 | using | |
600 | .BR wait (2). | |
601 | (The thread is said to be | |
602 | .IR detached .) | |
603 | ||
e2fbf61d MK |
604 | After all of the threads in a thread group terminate |
605 | the parent process of the thread group is sent a | |
fd8a5be4 MK |
606 | .B SIGCHLD |
607 | (or other termination) signal. | |
608 | ||
609 | If any of the threads in a thread group performs an | |
610 | .BR execve (2), | |
611 | then all threads other than the thread group leader are terminated, | |
612 | and the new program is executed in the thread group leader. | |
613 | ||
f7110f60 MK |
614 | If one of the threads in a thread group creates a child using |
615 | .BR fork (2), | |
616 | then any thread in the group can | |
617 | .BR wait (2) | |
618 | for that child. | |
619 | ||
edcc65ff | 620 | Since Linux 2.5.35, |
fd8a5be4 MK |
621 | .I flags |
622 | must also include | |
623 | .B CLONE_SIGHAND | |
624 | if | |
625 | .B CLONE_THREAD | |
626 | is specified. | |
e2fbf61d MK |
627 | |
628 | Signals may be sent to a thread group as a whole (i.e., a TGID) using | |
629 | .BR kill (2), | |
630 | or to a specific thread (i.e., TID) using | |
631 | .BR tgkill (2). | |
632 | ||
633 | Signal dispositions and actions are process-wide: | |
634 | if an unhandled signal is delivered to a thread, then | |
635 | it will affect (terminate, stop, continue, be ignored in) | |
636 | all members of the thread group. | |
637 | ||
99408a60 | 638 | Each thread has its own signal mask, as set by |
e2fbf61d | 639 | .BR sigprocmask (2), |
82a06020 | 640 | but signals can be pending either: for the whole process |
e2fbf61d MK |
641 | (i.e., deliverable to any member of the thread group), |
642 | when sent with | |
82a06020 | 643 | .BR kill (2); |
e2fbf61d MK |
644 | or for an individual thread, when sent with |
645 | .BR tgkill (2). | |
99408a60 MK |
646 | A call to |
647 | .BR sigpending (2) | |
648 | returns a signal set that is the union of the signals pending for the | |
649 | whole process and the signals that are pending for the calling thread. | |
e2fbf61d | 650 | |
c13182ef | 651 | If |
e2fbf61d MK |
652 | .BR kill (2) |
653 | is used to send a signal to a thread group, | |
654 | and the thread group has installed a handler for the signal, then | |
655 | the handler will be invoked in exactly one, arbitrarily selected | |
656 | member of the thread group that has not blocked the signal. | |
c13182ef | 657 | If multiple threads in a group are waiting to accept the same signal using |
e2fbf61d MK |
658 | .BR sigwaitinfo (2), |
659 | the kernel will arbitrarily select one of these threads | |
c13182ef | 660 | to receive a signal sent using |
e2fbf61d | 661 | .BR kill (2). |
a69b6bda | 662 | .TP |
f5dbc7c8 | 663 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" |
a69b6bda | 664 | If |
f5dbc7c8 MK |
665 | .B CLONE_UNTRACED |
666 | is specified, then a tracing process cannot force | |
667 | .B CLONE_PTRACE | |
668 | on this child process. | |
fea681da | 669 | .TP |
f5dbc7c8 MK |
670 | .B CLONE_VFORK |
671 | If | |
672 | .B CLONE_VFORK | |
673 | is set, the execution of the calling process is suspended | |
674 | until the child releases its virtual memory | |
675 | resources via a call to | |
676 | .BR execve (2) | |
677 | or | |
678 | .BR _exit (2) | |
679 | (as with | |
680 | .BR vfork (2)). | |
681 | ||
682 | If | |
683 | .B CLONE_VFORK | |
684 | is not set then both the calling process and the child are schedulable | |
685 | after the call, and an application should not rely on execution occurring | |
686 | in any particular order. | |
fea681da | 687 | .TP |
f5dbc7c8 MK |
688 | .B CLONE_VM |
689 | If | |
690 | .B CLONE_VM | |
691 | is set, the calling process and the child process run in the same memory | |
692 | space. | |
693 | In particular, memory writes performed by the calling process | |
694 | or by the child process are also visible in the other process. | |
695 | Moreover, any memory mapping or unmapping performed with | |
696 | .BR mmap (2) | |
697 | or | |
698 | .BR munmap (2) | |
699 | by the child or calling process also affects the other process. | |
700 | ||
701 | If | |
702 | .B CLONE_VM | |
703 | is not set, the child process runs in a separate copy of the memory | |
704 | space of the calling process at the time of | |
705 | .BR clone (). | |
706 | Memory writes or file mappings/unmappings performed by one of the | |
707 | processes do not affect the other, as with | |
708 | .BR fork (2). | |
fea681da MK |
709 | .SS "sys_clone" |
710 | The | |
711 | .B sys_clone | |
712 | system call corresponds more closely to | |
713 | .BR fork (2) | |
714 | in that execution in the child continues from the point of the | |
c13182ef MK |
715 | call. |
716 | Thus, | |
fea681da MK |
717 | .B sys_clone |
718 | only requires the | |
719 | .I flags | |
c13182ef | 720 | and |
fea681da | 721 | .I child_stack |
c13182ef MK |
722 | arguments, which have the same meaning as for |
723 | .BR clone (). | |
fea681da | 724 | (Note that the order of these arguments differs from |
c13182ef | 725 | .BR clone ().) |
fea681da | 726 | |
c13182ef | 727 | Another difference for |
fea681da MK |
728 | .B sys_clone |
729 | is that the | |
730 | .I child_stack | |
c13182ef | 731 | argument may be zero, in which case copy-on-write semantics ensure that the |
fea681da | 732 | child gets separate copies of stack pages when either process modifies |
c13182ef MK |
733 | the stack. |
734 | In this case, for correct operation, the | |
fea681da MK |
735 | .B CLONE_VM |
736 | option should not be specified. | |
737 | ||
c4bb193f MK |
738 | Since Linux 2.5.49 the system call has five arguments. |
739 | The two new arguments are | |
fea681da MK |
740 | .I parent_tidptr |
741 | which points to the location (in parent and child memory) where | |
682edefb MK |
742 | the child thread ID will be written in case |
743 | .B CLONE_PARENT_SETTID | |
fea681da MK |
744 | was specified, and |
745 | .I child_tidptr | |
746 | which points to the location (in child memory) where the child thread ID | |
682edefb MK |
747 | will be written in case |
748 | .B CLONE_CHILD_SETTID | |
749 | was specified. | |
fea681da | 750 | .SH "RETURN VALUE" |
0bfa087b MK |
751 | .\" gettid(2) returns current->pid; |
752 | .\" getpid(2) returns current->tgid; | |
fea681da | 753 | On success, the thread ID of the child process is returned |
c13182ef | 754 | in the caller's thread of execution. |
84811e86 | 755 | On failure, \-1 is returned |
fea681da MK |
756 | in the caller's context, no child process will be created, and |
757 | .I errno | |
758 | will be set appropriately. | |
fea681da MK |
759 | .SH ERRORS |
760 | .TP | |
761 | .B EAGAIN | |
762 | Too many processes are already running. | |
763 | .TP | |
764 | .B EINVAL | |
765 | .B CLONE_SIGHAND | |
766 | was specified, but | |
767 | .B CLONE_VM | |
2e8a7fb3 MK |
768 | was not. |
769 | (Since Linux 2.6.0-test6.) | |
fea681da MK |
770 | .TP |
771 | .B EINVAL | |
772 | .B CLONE_THREAD | |
773 | was specified, but | |
774 | .B CLONE_SIGHAND | |
6387216b MK |
775 | was not. |
776 | (Since Linux 2.5.35.) | |
29546c24 MK |
777 | .\" .TP |
778 | .\" .B EINVAL | |
779 | .\" Precisely one of | |
780 | .\" .B CLONE_DETACHED | |
781 | .\" and | |
782 | .\" .B CLONE_THREAD | |
6387216b MK |
783 | .\" was specified. |
784 | .\" (Since Linux 2.6.0-test6.) | |
fea681da MK |
785 | .TP |
786 | .B EINVAL | |
787 | Both | |
788 | .B CLONE_FS | |
789 | and | |
790 | .B CLONE_NEWNS | |
791 | were specified in | |
792 | .IR flags . | |
793 | .TP | |
794 | .B EINVAL | |
82ee147a | 795 | Both |
667417b3 MK |
796 | .B CLONE_NEWIPC |
797 | and | |
798 | .B CLONE_SYSVSEM | |
799 | were specified in | |
800 | .IR flags . | |
801 | .TP | |
802 | .B EINVAL | |
803 | Both | |
82ee147a MK |
804 | .BR CLONE_NEWPID |
805 | and | |
806 | .BR CLONE_THREAD | |
807 | were specified in | |
808 | .IR flags . | |
809 | .TP | |
810 | .B EINVAL | |
c13182ef | 811 | Returned by |
edcc65ff | 812 | .BR clone () |
c13182ef | 813 | when a zero value is specified for |
fea681da MK |
814 | .IR child_stack . |
815 | .TP | |
28cad2c1 | 816 | .B EINVAL |
667417b3 MK |
817 | .BR CLONE_NEWIPC |
818 | was specified in | |
819 | .IR flags , | |
820 | but the kernel was not configured with the | |
821 | .B CONFIG_SYSVIPC | |
822 | and | |
823 | .BR CONFIG_IPC_NS | |
824 | options. | |
825 | .TP | |
826 | .B EINVAL | |
28cad2c1 MK |
827 | .BR CLONE_NEWPID |
828 | was specified in | |
829 | .IR flags , | |
830 | but the kernel was not configured with the | |
831 | .B CONFIG_PID_NS | |
832 | option. | |
833 | .TP | |
43ce9dda MK |
834 | .B EINVAL |
835 | .BR CLONE_NEWUTS | |
836 | was specified in | |
837 | .IR flags , | |
838 | but the kernel was not configured with the | |
839 | .B CONFIG_UTS | |
840 | option. | |
841 | .TP | |
fea681da MK |
842 | .B ENOMEM |
843 | Cannot allocate sufficient memory to allocate a task structure for the | |
844 | child, or to copy those parts of the caller's context that need to be | |
845 | copied. | |
846 | .TP | |
847 | .B EPERM | |
667417b3 | 848 | .BR CLONE_NEWIPC , |
43ce9dda MK |
849 | .BR CLONE_NEWNS , |
850 | .BR CLONE_NEWPID , | |
82ee147a | 851 | or |
43ce9dda | 852 | .BR CLONE_NEWUTS |
0b9bdf82 | 853 | was specified by a non-root process (process without \fBCAP_SYS_ADMIN\fP). |
fea681da MK |
854 | .TP |
855 | .B EPERM | |
856 | .B CLONE_PID | |
857 | was specified by a process other than process 0. | |
a759cc87 | 858 | .SH VERSIONS |
fea681da | 859 | There is no entry for |
edcc65ff | 860 | .BR clone () |
a759cc87 MK |
861 | in libc5. |
862 | glibc2 provides | |
edcc65ff | 863 | .BR clone () |
fea681da | 864 | as described in this manual page. |
a1d5f77c MK |
865 | .SH "CONFORMING TO" |
866 | The | |
867 | .BR clone () | |
868 | and | |
869 | .B sys_clone | |
8382f16d | 870 | calls are Linux-specific and should not be used in programs |
a1d5f77c | 871 | intended to be portable. |
fea681da | 872 | .SH NOTES |
fd8a5be4 MK |
873 | In the kernel 2.4.x series, |
874 | .B CLONE_THREAD | |
875 | generally does not make the parent of the new thread the same | |
876 | as the parent of the calling process. | |
877 | However, for kernel versions 2.4.7 to 2.4.18 the | |
878 | .B CLONE_THREAD | |
879 | flag implied the | |
c13182ef | 880 | .B CLONE_PARENT |
fd8a5be4 | 881 | flag (as in kernel 2.6). |
fea681da | 882 | |
c13182ef MK |
883 | For a while there was |
884 | .B CLONE_DETACHED | |
a5053dcb | 885 | (introduced in 2.5.32): |
c13182ef | 886 | parent wants no child-exit signal. |
a5053dcb | 887 | In 2.6.2 the need to give this |
c13182ef MK |
888 | together with |
889 | .B CLONE_THREAD | |
a5053dcb MK |
890 | disappeared. |
891 | This flag is still defined, but has no effect. | |
892 | ||
34ccb744 | 893 | On i386, |
a5a997ca MK |
894 | .BR clone () |
895 | should not be called through vsyscall, but directly through | |
896 | .IR "int $0x80" . | |
ff929e3b | 897 | |
22399250 | 898 | On ia64, a different system call is used: |
ff929e3b MK |
899 | .nf |
900 | ||
9b0e0996 MK |
901 | .BI "int __clone2(int (*" "fn" ")(void *), " |
902 | .BI " void *" child_stack_base ", size_t " stack_size , | |
903 | .BI " int " flags ", void *" "arg" ", ... " | |
904 | .BI " /* pid_t *" pid ", struct user_desc *" tls \ | |
ff929e3b MK |
905 | ", pid_t *" ctid " */ );" |
906 | .fi | |
907 | .PP | |
908 | The | |
9b0e0996 | 909 | .BR __clone2 () |
c13182ef | 910 | system call operates in the same way as |
ff929e3b MK |
911 | .BR clone (), |
912 | except that | |
913 | .I child_stack_base | |
914 | points to the lowest address of the child's stack area, | |
915 | and | |
916 | .I stack_size | |
917 | specifies the size of the stack pointed to by | |
918 | .IR child_stack_base . | |
31830ef0 MK |
919 | .SH BUGS |
920 | Versions of the GNU C library that include the NPTL threading library | |
c13182ef | 921 | contain a wrapper function for |
0bfa087b | 922 | .BR getpid (2) |
31830ef0 | 923 | that performs caching of PIDs. |
c60237c9 MK |
924 | This caching relies on support in the glibc wrapper for |
925 | .BR clone (), | |
926 | but as currently implemented, | |
927 | the cache may not be up to date in some circumstances. | |
928 | In particular, | |
929 | if a signal is delivered to the child immediately after the | |
930 | .BR clone () | |
931 | call, then a call to | |
932 | .BR getpid () | |
933 | in a handler for the signal may return the PID | |
934 | of the calling process ("the parent"), | |
88619baf | 935 | if the clone wrapper has not yet had a chance to update the PID |
c60237c9 MK |
936 | cache in the child. |
937 | (This discussion ignores the case where the child was created using | |
9291ce36 | 938 | .BR CLONE_THREAD , |
c60237c9 MK |
939 | when |
940 | .BR getpid () | |
941 | .I should | |
942 | return the same value in the child and in the process that called | |
943 | .BR clone (), | |
a1d48abb | 944 | since the caller and the child are in the same thread group. |
e7d807b7 | 945 | The stale-cache problem also does not occur if the |
a1d48abb JR |
946 | .I flags |
947 | argument includes | |
948 | .BR CLONE_VM .) | |
c60237c9 | 949 | To get the truth, it may be necessary to use code such as the following: |
31830ef0 MK |
950 | .nf |
951 | ||
952 | #include <syscall.h> | |
953 | ||
954 | pid_t mypid; | |
955 | ||
956 | mypid = syscall(SYS_getpid); | |
957 | .fi | |
c60237c9 MK |
958 | .\" See also the following bug reports |
959 | .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521 | |
960 | .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910 | |
fea681da MK |
961 | .SH "SEE ALSO" |
962 | .BR fork (2), | |
2b44301c | 963 | .BR futex (2), |
fea681da MK |
964 | .BR getpid (2), |
965 | .BR gettid (2), | |
f2d0bbf1 | 966 | .BR set_thread_area (2), |
2b44301c | 967 | .BR set_tid_address (2), |
f2d0bbf1 | 968 | .BR tkill (2), |
5cc01e9c | 969 | .BR unshare (2), |
fea681da | 970 | .BR wait (2), |
3616b7c0 MK |
971 | .BR capabilities (7), |
972 | .BR pthreads (7) |