1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992
4 .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005
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
16 .\" Added CLONE_PARENT and revised entire page remove ambiguity
17 .\" between "calling process" and "parent process"
18 .\" Added CLONE_PTRACE and CLONE_VFORK
19 .\" Added EPERM and EINVAL error codes
20 .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>)
21 .\" various other minor tidy ups and clarifications.
22 .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk-manpages@gmx.net>
23 .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD
24 .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk-manpages@gmx.net>
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
29 .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc - aeb.
30 .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid()
31 .\" wrapper under BUGS.
32 .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED.
33 .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD.
35 .TH CLONE 2 2007-06-01 "Linux" "Linux Programmer's Manual"
37 clone \- create a child process
42 .BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
43 .BI " int " flags ", void *" "arg" ", ... "
44 .BI " /* pid_t *" pid ", struct user_desc *" tls \
45 ", pid_t *" ctid " */ );"
49 creates a new process, in a manner similar to
51 It is actually a library function layered on top of the underlying
53 system call, hereinafter referred to as
57 is given towards the end of this page.
62 allow the child process to share parts of its execution context with
63 the calling process, such as the memory space, the table of file
64 descriptors, and the table of signal handlers.
65 (Note that on this manual
66 page, "calling process" normally corresponds to "parent process".
67 But see the description of
73 is to implement threads: multiple threads of control in a program that
74 run concurrently in a shared memory space.
76 When the child process is created with
78 it executes the function
83 where execution continues in the child from the point
89 argument is a pointer to a function that is called by the child
90 process at the beginning of its execution.
93 argument is passed to the
99 function application returns, the child process terminates.
100 The integer returned by
102 is the exit code for the child process.
103 The child process may also terminate explicitly by calling
105 or after receiving a fatal signal.
109 argument specifies the location of the stack used by the child process.
110 Since the child and calling process may share memory,
111 it is not possible for the child process to execute in the
112 same stack as the calling process.
113 The calling process must therefore
114 set up memory space for the child stack and pass a pointer to this
117 Stacks grow downwards on all processors that run Linux
118 (except the HP PA processors), so
120 usually points to the topmost address of the memory space set up for
125 contains the number of the
126 .I "termination signal"
127 sent to the parent when the child dies.
128 If this signal is specified as anything other than
130 then the parent process must specify the
134 options when waiting for the child with
136 If no signal is specified, then the parent process is not signaled
137 when the child terminates.
140 may also be bitwise-or'ed with zero or more of the following constants,
141 in order to specify what is shared between the calling process
142 and the child process:
144 .BR CLONE_PARENT " (since Linux 2.3.12)"
147 is set, then the parent of the new child (as returned by
149 will be the same as that of the calling process.
153 is not set, then (as with
155 the child's parent is the calling process.
157 Note that it is the parent process, as returned by
159 which is signaled when the child terminates, so that
162 is set, then the parent of the calling process, rather than the
163 calling process itself, will be signaled.
168 is set, the caller and the child processes share the same file system
170 This includes the root of the file system, the current
171 working directory, and the umask.
177 performed by the calling process or the child process also affects the
182 is not set, the child process works on a copy of the file system
183 information of the calling process at the time of the
190 performed later by one of the processes do not affect the other process.
195 is set, the calling process and the child processes share the same file
197 Any file descriptor created by the calling process or by the child
198 process is also valid in the other process.
199 Similarly, if one of the processes closes a file descriptor,
200 or changes its associated flags (using the
203 operation), the other process is also affected.
207 is not set, the child process inherits a copy of all file descriptors
208 opened in the calling process at the time of
210 (The duplicated file descriptors in the child refer to the
211 same open file descriptions (see
213 as the corresponding file descriptors in the calling process.)
214 Subsequent operations that open or close file descriptors,
215 or change file descriptor flags,
216 performed by either the calling
217 process or the child process do not affect the other process.
219 .BR CLONE_NEWNS " (since Linux 2.4.19)"
220 Start the child in a new namespace.
222 Every process lives in a namespace.
225 of a process is the data (the set of mounts) describing the file hierarchy
226 as seen by that process.
233 flag is not set, the child lives in the same namespace as the parent.
238 change the namespace of the calling process, and hence affect
239 all processes that live in the same namespace, but do not affect
240 processes in a different namespace.
246 flag is set, the cloned child is started in a new namespace,
247 initialized with a copy of the namespace of the parent.
249 Only a privileged process (one having the CAP_SYS_ADMIN capability)
253 It is not permitted to specify both
264 is set, the calling process and the child processes share the same table of
266 If the calling process or child process calls
268 to change the behavior associated with a signal, the behavior is
269 changed in the other process as well.
270 However, the calling process and child
271 processes still have distinct signal masks and sets of pending
273 So, one of them may block or unblock some signals using
275 without affecting the other process.
279 is not set, the child process inherits a copy of the signal handlers
280 of the calling process at the time
285 performed later by one of the processes have no effect on the other
288 Since Linux 2.6.0-test6,
299 is specified, and the calling process is being traced,
300 then trace the child also (see
303 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
306 is specified, then a tracing process cannot force
308 on this child process.
310 .BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
313 is set, then the child is initially stopped (as though it was sent a
315 signal), and must be resumed by sending it a
322 is set, the execution of the calling process is suspended
323 until the child releases its virtual memory
324 resources via a call to
333 is not set then both the calling process and the child are schedulable
334 after the call, and an application should not rely on execution occurring
335 in any particular order.
340 is set, the calling process and the child processes run in the same memory
342 In particular, memory writes performed by the calling process
343 or by the child process are also visible in the other process.
344 Moreover, any memory mapping or unmapping performed with
348 by the child or calling process also affects the other process.
352 is not set, the child process runs in a separate copy of the memory
353 space of the calling process at the time of
355 Memory writes or file mappings/unmappings performed by one of the
356 processes do not affect the other, as with
359 .BR CLONE_PID " (obsolete)"
362 is set, the child process is created with the same process ID as
364 This is good for hacking the system, but otherwise
366 Since 2.3.21 this flag can be
367 specified only by the system boot process (PID 0).
368 It disappeared in Linux 2.5.16.
370 .BR CLONE_THREAD " (since Linux 2.4.0-test8)"
373 is set, the child is placed in the same thread group as the calling process.
374 To make the remainder of the discussion of
376 more readable, the term "thread" is used to refer to the
377 processes within a thread group.
379 Thread groups were a feature added in Linux 2.4 to support the
380 POSIX threads notion of a set of threads that share a single PID.
381 Internally, this shared PID is the so-called
382 thread group identifier (TGID) for the thread group.
383 Since Linux 2.4, calls to
385 return the TGID of the caller.
387 The threads within a group can be distinguished by their (system-wide)
388 unique thread IDs (TID).
389 A new thread's TID is available as the function result
390 returned to the caller of
392 and a thread can obtain
396 When a call is made to
400 then the resulting thread is placed in a new thread group
401 whose TGID is the same as the thread's TID.
404 of the new thread group.
406 A new thread created with
408 has the same parent process as the caller of
414 return the same value for all of the threads in a thread group.
417 thread terminates, the thread that created it using
421 (or other termination) signal;
422 nor can the status of such a thread be obtained
425 (The thread is said to be
428 After all of the threads in a thread group terminate
429 the parent process of the thread group is sent a
431 (or other termination) signal.
433 If any of the threads in a thread group performs an
435 then all threads other than the thread group leader are terminated,
436 and the new program is executed in the thread group leader.
438 If one of the threads in a thread group creates a child using
440 then any thread in the group can
452 Signals may be sent to a thread group as a whole (i.e., a TGID) using
454 or to a specific thread (i.e., TID) using
457 Signal dispositions and actions are process-wide:
458 if an unhandled signal is delivered to a thread, then
459 it will affect (terminate, stop, continue, be ignored in)
460 all members of the thread group.
462 Each thread has its own signal mask, as set by
464 but signals can be pending either: for the whole process
465 (i.e., deliverable to any member of the thread group),
468 or for an individual thread, when sent with
472 returns a signal set that is the union of the signals pending for the
473 whole process and the signals that are pending for the calling thread.
477 is used to send a signal to a thread group,
478 and the thread group has installed a handler for the signal, then
479 the handler will be invoked in exactly one, arbitrarily selected
480 member of the thread group that has not blocked the signal.
481 If multiple threads in a group are waiting to accept the same signal using
483 the kernel will arbitrarily select one of these threads
484 to receive a signal sent using
487 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
490 is set, then the child and the calling process share
491 a single list of System V semaphore undo values (see
493 If this flag is not set, then the child has a separate undo list,
494 which is initially empty.
496 .BR CLONE_SETTLS " (since Linux 2.5.32)"
499 parameter is the new TLS (Thread Local Storage) descriptor.
501 .BR set_thread_area (2).)
503 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
504 Store child thread ID at location
506 in parent and child memory.
507 (In Linux 2.5.32-2.5.48 there was a flag CLONE_SETTID that did this.)
509 .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
510 Store child thread ID at location
514 .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
515 Erase child thread ID at location
517 in child memory when the child exits, and do a wakeup on the futex
519 The address involved may be changed by the
520 .BR set_tid_address (2)
522 This is used by threading libraries.
526 system call corresponds more closely to
528 in that execution in the child continues from the point of the
536 arguments, which have the same meaning as for
538 (Note that the order of these arguments differs from
541 Another difference for
545 argument may be zero, in which case copy-on-write semantics ensure that the
546 child gets separate copies of stack pages when either process modifies
548 In this case, for correct operation, the
550 option should not be specified.
552 Since Linux 2.5.49 the system call has five parameters.
553 The two new parameters are
555 which points to the location (in parent and child memory) where
556 the child thread ID will be written in case CLONE_PARENT_SETTID
559 which points to the location (in child memory) where the child thread ID
560 will be written in case CLONE_CHILD_SETTID was specified.
562 .\" gettid(2) returns current->pid;
563 .\" getpid(2) returns current->tgid;
564 On success, the thread ID of the child process is returned
565 in the caller's thread of execution.
566 On failure, a \-1 will be returned
567 in the caller's context, no child process will be created, and
569 will be set appropriately.
573 Too many processes are already running.
580 (Since Linux 2.6.0-test6.)
586 was not. (Since Linux 2.5.35.)
590 .\" .B CLONE_DETACHED
593 .\" was specified. (Since Linux 2.6.0-test6.)
606 when a zero value is specified for
610 Cannot allocate sufficient memory to allocate a task structure for the
611 child, or to copy those parts of the caller's context that need to be
616 was specified by a non-root process (process without CAP_SYS_ADMIN).
620 was specified by a process other than process 0.
622 There is no entry for
627 as described in this manual page.
633 calls are Linux specific and should not be used in programs
634 intended to be portable.
636 In the kernel 2.4.x series,
638 generally does not make the parent of the new thread the same
639 as the parent of the calling process.
640 However, for kernel versions 2.4.7 to 2.4.18 the
644 flag (as in kernel 2.6).
646 For a while there was
648 (introduced in 2.5.32):
649 parent wants no child-exit signal.
650 In 2.6.2 the need to give this
654 This flag is still defined, but has no effect.
658 should not be called through vsyscall, but directly through
661 On IA-64, a different system call is used:
664 .BI "int clone2(int (*" "fn" ")(void *), "
665 .BI " void *" child_stack_base ", size_t " stack_size ,
666 .BI " int " flags ", void *" "arg" ", ... "
667 .BI " /* pid_t *" pid ", struct user_desc *" tls \
668 ", pid_t *" ctid " */ );"
673 system call operates in the same way as
677 points to the lowest address of the child's stack area,
680 specifies the size of the stack pointed to by
681 .IR child_stack_base .
683 Versions of the GNU C library that include the NPTL threading library
684 contain a wrapper function for
686 that performs caching of PIDs.
687 In programs linked against such libraries, calls to
689 may return the same value, even when the threads were not created using
691 (and thus are not in the same thread group).
692 To get the truth, it may be necessary to use code such as the following
699 mypid = syscall(SYS_getpid);
706 .BR set_thread_area (2),
707 .BR set_tid_address (2),
711 .BR capabilities (7),