1 .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992
2 .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" May be distributed under the GNU General Public License.
8 .\" Modified by Michael Haardt <michael@moria.de>
9 .\" Modified 24 Jul 1993 by Rik Faith <faith@cs.unc.edu>
10 .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>:
11 .\" New man page (copied from 'fork.2').
12 .\" Modified 10 June 1995 by Andries Brouwer <aeb@cwi.nl>
13 .\" Modified 25 April 1998 by Xavier Leroy <Xavier.Leroy@inria.fr>
14 .\" Modified 26 Jun 2001 by Michael Kerrisk
15 .\" Mostly upgraded to 2.4.x
16 .\" Added prototype for sys_clone() plus description
17 .\" Added CLONE_THREAD with a brief description of thread groups
18 .\" Added CLONE_PARENT and revised entire page remove ambiguity
19 .\" between "calling process" and "parent process"
20 .\" Added CLONE_PTRACE and CLONE_VFORK
21 .\" Added EPERM and EINVAL error codes
22 .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>)
23 .\" various other minor tidy ups and clarifications.
24 .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com>
25 .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD
26 .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
27 .\" Added description for CLONE_NEWNS, which was added in 2.4.19
28 .\" Slightly rephrased, aeb.
29 .\" Modified 1 Feb 2003 - added CLONE_SIGHAND restriction, aeb.
30 .\" Modified 1 Jan 2004 - various updates, aeb
31 .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb.
32 .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid()
33 .\" wrapper under BUGS.
34 .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED.
35 .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD.
36 .\" 2008-11-18, mtk, order CLONE_* flags alphabetically
37 .\" 2008-11-18, mtk, document CLONE_NEWPID
38 .\" 2008-11-19, mtk, document CLONE_NEWUTS
39 .\" 2008-11-19, mtk, document CLONE_NEWIPC
40 .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO
42 .TH CLONE 2 2014-08-19 "Linux" "Linux Programmer's Manual"
44 clone, __clone2 \- create a child process
47 /* Prototype for the glibc wrapper function */
51 .BI "int clone(int (*" "fn" ")(void *), void *" child_stack ,
52 .BI " int " flags ", void *" "arg" ", ... "
53 .BI " /* pid_t *" ptid ", struct user_desc *" tls \
54 ", pid_t *" ctid " */ );"
56 /* Prototype for the raw system call */
58 .BI "long clone(unsigned long " flags ", void *" child_stack ,
59 .BI " void *" ptid ", void *" ctid ,
60 .BI " struct pt_regs *" regs );
64 Feature Test Macro Requirements for glibc wrapper function (see
65 .BR feature_test_macros (7)):
76 .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
78 _BSD_SOURCE || _SVID_SOURCE
79 /* _GNU_SOURCE also suffices */
85 creates a new process, in a manner similar to
88 This page describes both the glibc
90 wrapper function and the underlying system call on which it is based.
91 The main text describes the wrapper function;
92 the differences for the raw system call
93 are described toward the end of this page.
98 allows the child process to share parts of its execution context with
99 the calling process, such as the memory space, the table of file
100 descriptors, and the table of signal handlers.
101 (Note that on this manual
102 page, "calling process" normally corresponds to "parent process".
103 But see the description of
109 is to implement threads: multiple threads of control in a program that
110 run concurrently in a shared memory space.
112 When the child process is created with
114 it executes the function
118 where execution continues in the child from the point
124 argument is a pointer to a function that is called by the child
125 process at the beginning of its execution.
128 argument is passed to the
134 function application returns, the child process terminates.
135 The integer returned by
137 is the exit code for the child process.
138 The child process may also terminate explicitly by calling
140 or after receiving a fatal signal.
144 argument specifies the location of the stack used by the child process.
145 Since the child and calling process may share memory,
146 it is not possible for the child process to execute in the
147 same stack as the calling process.
148 The calling process must therefore
149 set up memory space for the child stack and pass a pointer to this
152 Stacks grow downward on all processors that run Linux
153 (except the HP PA processors), so
155 usually points to the topmost address of the memory space set up for
160 contains the number of the
161 .I "termination signal"
162 sent to the parent when the child dies.
163 If this signal is specified as anything other than
165 then the parent process must specify the
169 options when waiting for the child with
171 If no signal is specified, then the parent process is not signaled
172 when the child terminates.
175 may also be bitwise-or'ed with zero or more of the following constants,
176 in order to specify what is shared between the calling process
177 and the child process:
179 .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
180 Erase child thread ID at location
182 in child memory when the child exits, and do a wakeup on the futex
184 The address involved may be changed by the
185 .BR set_tid_address (2)
187 This is used by threading libraries.
189 .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
190 Store child thread ID at location
194 .BR CLONE_FILES " (since Linux 2.0)"
197 is set, the calling process and the child process share the same file
199 Any file descriptor created by the calling process or by the child
200 process is also valid in the other process.
201 Similarly, if one of the processes closes a file descriptor,
202 or changes its associated flags (using the
205 operation), the other process is also affected.
209 is not set, the child process inherits a copy of all file descriptors
210 opened in the calling process at the time of
212 (The duplicated file descriptors in the child refer to the
213 same open file descriptions (see
215 as the corresponding file descriptors in the calling process.)
216 Subsequent operations that open or close file descriptors,
217 or change file descriptor flags,
218 performed by either the calling
219 process or the child process do not affect the other process.
221 .BR CLONE_FS " (since Linux 2.0)"
224 is set, the caller and the child process share the same filesystem
226 This includes the root of the filesystem, the current
227 working directory, and the umask.
233 performed by the calling process or the child process also affects the
238 is not set, the child process works on a copy of the filesystem
239 information of the calling process at the time of the
246 performed later by one of the processes do not affect the other process.
248 .BR CLONE_IO " (since Linux 2.6.25)"
251 is set, then the new process shares an I/O context with
253 If this flag is not set, then (as with
255 the new process has its own I/O context.
257 .\" The following based on text from Jens Axboe
258 The I/O context is the I/O scope of the disk scheduler (i.e,
259 what the I/O scheduler uses to model scheduling of a process's I/O).
260 If processes share the same I/O context,
261 they are treated as one by the I/O scheduler.
262 As a consequence, they get to share disk time.
263 For some I/O schedulers,
264 .\" the anticipatory and CFQ scheduler
265 if two processes share an I/O context,
266 they will be allowed to interleave their disk access.
267 If several threads are doing I/O on behalf of the same process
269 for instance), they should employ
271 to get better I/O performance.
274 If the kernel is not configured with the
276 option, this flag is a no-op.
278 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
281 is set, then create the process in a new IPC namespace.
282 If this flag is not set, then (as with
284 the process is created in the same IPC namespace as
286 This flag is intended for the implementation of containers.
288 An IPC namespace provides an isolated view of System\ V IPC objects (see
290 and (since Linux 2.6.30)
291 .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
292 .\" https://lwn.net/Articles/312232/
295 .BR mq_overview (7)).
296 The common characteristic of these IPC mechanisms is that IPC
297 objects are identified by mechanisms other than filesystem
300 Objects created in an IPC namespace are visible to all other processes
301 that are members of that namespace,
302 but are not visible to processes in other IPC namespaces.
304 When an IPC namespace is destroyed
305 (i.e., when the last process that is a member of the namespace terminates),
306 all IPC objects in the namespace are automatically destroyed.
308 Only a privileged process
309 .RB ( CAP_SYS_ADMIN )
312 This flag can't be specified in conjunction with
315 For further information on IPC namespaces, see
318 .BR CLONE_NEWNET " (since Linux 2.6.24)"
319 (The implementation of this flag was completed only
320 by about kernel version 2.6.29.)
324 is set, then create the process in a new network namespace.
325 If this flag is not set, then (as with
327 the process is created in the same network namespace as
329 This flag is intended for the implementation of containers.
331 A network namespace provides an isolated view of the networking stack
332 (network device interfaces, IPv4 and IPv6 protocol stacks,
333 IP routing tables, firewall rules, the
337 directory trees, sockets, etc.).
338 A physical network device can live in exactly one
340 A virtual network device ("veth") pair provides a pipe-like abstraction
341 .\" FIXME . Add pointer to veth(4) page when it is eventually completed
342 that can be used to create tunnels between network namespaces,
343 and can be used to create a bridge to a physical network device
344 in another namespace.
346 When a network namespace is freed
347 (i.e., when the last process in the namespace terminates),
348 its physical network devices are moved back to the
349 initial network namespace (not to the parent of the process).
350 For further information on network namespaces, see
353 Only a privileged process
354 .RB ( CAP_SYS_ADMIN )
359 .BR CLONE_NEWNS " (since Linux 2.4.19)"
362 is set, the cloned child is started in a new mount namespace,
363 initialized with a copy of the namespace of the parent.
366 is not set, the child lives in the same mount
367 namespace as the parent.
369 For further information on mount namespaces, see
372 Only a privileged process
373 .RB ( CAP_SYS_ADMIN )
376 It is not permitted to specify both
385 .BR CLONE_NEWPID " (since Linux 2.6.24)"
386 .\" This explanation draws a lot of details from
387 .\" http://lwn.net/Articles/259217/
388 .\" Authors: Pavel Emelyanov <xemul@openvz.org>
389 .\" and Kir Kolyshkin <kir@openvz.org>
391 .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
392 .\" Author: Pavel Emelyanov <xemul@openvz.org>
395 is set, then create the process in a new PID namespace.
396 If this flag is not set, then (as with
398 the process is created in the same PID namespace as
400 This flag is intended for the implementation of containers.
402 For further information on PID namespaces, see
405 Only a privileged process
406 .RB ( CAP_SYS_ADMIN )
409 This flag can't be specified in conjunction with
414 (This flag first became meaningful for
419 semantics were merged in Linux 3.5,
420 and the final pieces to make the user namespaces completely usable were
421 merged in Linux 3.8.)
425 is set, then create the process in a new user namespace.
426 If this flag is not set, then (as with
428 the process is created in the same user namespace as the calling process.
430 For further information on user namespaces, see
433 Before Linux 3.8, use of
435 required that the caller have three capabilities:
440 .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
441 Starting with Linux 3.8,
442 no privileges are needed to create a user namespace.
445 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
448 is set, then create the process in a new UTS namespace,
449 whose identifiers are initialized by duplicating the identifiers
450 from the UTS namespace of the calling process.
451 If this flag is not set, then (as with
453 the process is created in the same UTS namespace as
455 This flag is intended for the implementation of containers.
457 A UTS namespace is the set of identifiers returned by
459 among these, the domain name and the hostname can be modified by
460 .BR setdomainname (2)
464 Changes made to the identifiers in a UTS namespace
465 are visible to all other processes in the same namespace,
466 but are not visible to processes in other UTS namespaces.
468 Only a privileged process
469 .RB ( CAP_SYS_ADMIN )
473 For further information on UTS namespaces, see
476 .BR CLONE_PARENT " (since Linux 2.3.12)"
479 is set, then the parent of the new child (as returned by
481 will be the same as that of the calling process.
485 is not set, then (as with
487 the child's parent is the calling process.
489 Note that it is the parent process, as returned by
491 which is signaled when the child terminates, so that
494 is set, then the parent of the calling process, rather than the
495 calling process itself, will be signaled.
497 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
498 Store child thread ID at location
500 in parent and child memory.
501 (In Linux 2.5.32-2.5.48 there was a flag
505 .BR CLONE_PID " (obsolete)"
508 is set, the child process is created with the same process ID as
510 This is good for hacking the system, but otherwise
512 Since 2.3.21 this flag can be
513 specified only by the system boot process (PID 0).
514 It disappeared in Linux 2.5.16.
516 .BR CLONE_PTRACE " (since Linux 2.2)"
519 is specified, and the calling process is being traced,
520 then trace the child also (see
523 .BR CLONE_SETTLS " (since Linux 2.5.32)"
526 argument is the new TLS (Thread Local Storage) descriptor.
528 .BR set_thread_area (2).)
530 .BR CLONE_SIGHAND " (since Linux 2.0)"
533 is set, the calling process and the child process share the same table of
535 If the calling process or child process calls
537 to change the behavior associated with a signal, the behavior is
538 changed in the other process as well.
539 However, the calling process and child
540 processes still have distinct signal masks and sets of pending
542 So, one of them may block or unblock some signals using
544 without affecting the other process.
548 is not set, the child process inherits a copy of the signal handlers
549 of the calling process at the time
554 performed later by one of the processes have no effect on the other
557 Since Linux 2.6.0-test6,
565 .BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
568 is set, then the child is initially stopped (as though it was sent a
570 signal), and must be resumed by sending it a
576 from Linux 2.6.25 onward,
579 altogether in Linux 2.6.38.
580 .\" glibc 2.8 removed this defn from bits/sched.h
582 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
585 is set, then the child and the calling process share
586 a single list of System\ V semaphore undo values (see
588 If this flag is not set, then the child has a separate undo list,
589 which is initially empty.
591 .BR CLONE_THREAD " (since Linux 2.4.0-test8)"
594 is set, the child is placed in the same thread group as the calling process.
595 To make the remainder of the discussion of
597 more readable, the term "thread" is used to refer to the
598 processes within a thread group.
600 Thread groups were a feature added in Linux 2.4 to support the
601 POSIX threads notion of a set of threads that share a single PID.
602 Internally, this shared PID is the so-called
603 thread group identifier (TGID) for the thread group.
604 Since Linux 2.4, calls to
606 return the TGID of the caller.
608 The threads within a group can be distinguished by their (system-wide)
609 unique thread IDs (TID).
610 A new thread's TID is available as the function result
611 returned to the caller of
613 and a thread can obtain
617 When a call is made to
621 then the resulting thread is placed in a new thread group
622 whose TGID is the same as the thread's TID.
625 of the new thread group.
627 A new thread created with
629 has the same parent process as the caller of
635 return the same value for all of the threads in a thread group.
638 thread terminates, the thread that created it using
642 (or other termination) signal;
643 nor can the status of such a thread be obtained
646 (The thread is said to be
649 After all of the threads in a thread group terminate
650 the parent process of the thread group is sent a
652 (or other termination) signal.
654 If any of the threads in a thread group performs an
656 then all threads other than the thread group leader are terminated,
657 and the new program is executed in the thread group leader.
659 If one of the threads in a thread group creates a child using
661 then any thread in the group can
672 (and note that, since Linux 2.6.0-test6,
678 Signals may be sent to a thread group as a whole (i.e., a TGID) using
680 or to a specific thread (i.e., TID) using
683 Signal dispositions and actions are process-wide:
684 if an unhandled signal is delivered to a thread, then
685 it will affect (terminate, stop, continue, be ignored in)
686 all members of the thread group.
688 Each thread has its own signal mask, as set by
690 but signals can be pending either: for the whole process
691 (i.e., deliverable to any member of the thread group),
694 or for an individual thread, when sent with
698 returns a signal set that is the union of the signals pending for the
699 whole process and the signals that are pending for the calling thread.
703 is used to send a signal to a thread group,
704 and the thread group has installed a handler for the signal, then
705 the handler will be invoked in exactly one, arbitrarily selected
706 member of the thread group that has not blocked the signal.
707 If multiple threads in a group are waiting to accept the same signal using
709 the kernel will arbitrarily select one of these threads
710 to receive a signal sent using
713 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
716 is specified, then a tracing process cannot force
718 on this child process.
720 .BR CLONE_VFORK " (since Linux 2.2)"
723 is set, the execution of the calling process is suspended
724 until the child releases its virtual memory
725 resources via a call to
734 is not set, then both the calling process and the child are schedulable
735 after the call, and an application should not rely on execution occurring
736 in any particular order.
738 .BR CLONE_VM " (since Linux 2.0)"
741 is set, the calling process and the child process run in the same memory
743 In particular, memory writes performed by the calling process
744 or by the child process are also visible in the other process.
745 Moreover, any memory mapping or unmapping performed with
749 by the child or calling process also affects the other process.
753 is not set, the child process runs in a separate copy of the memory
754 space of the calling process at the time of
756 Memory writes or file mappings/unmappings performed by one of the
757 processes do not affect the other, as with
759 .SS C library/kernel ABI differences
762 system call corresponds more closely to
764 in that execution in the child continues from the point of the
772 wrapper function are omitted.
773 Furthermore, the argument order changes.
774 The raw system call interface on x86 and many other architectures is roughly:
778 .BI "long clone(unsigned long " flags ", void *" child_stack ,
779 .BI " void *" ptid ", void *" ctid ,
780 .BI " struct pt_regs *" regs );
784 Another difference for the raw system call is that the
786 argument may be zero, in which case copy-on-write semantics ensure that the
787 child gets separate copies of stack pages when either process modifies
789 In this case, for correct operation, the
791 option should not be specified.
793 For some architectures, the order of the arguments for the system call
794 differs from that shown above.
795 On the score, microblaze, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
796 and MIPS architectures,
797 the order of the fourth and fifth arguments is reversed.
798 On the cris and s390 architectures,
799 the order of the first and second arguments is reversed.
800 .SS blackfin, m68k, and sparc
801 The argument-passing conventions on
802 blackfin, m68k, and sparc are different from the descriptions above.
803 For details, see the kernel (and glibc) source.
805 On ia64, a different interface is used:
808 .BI "int __clone2(int (*" "fn" ")(void *), "
809 .BI " void *" child_stack_base ", size_t " stack_size ,
810 .BI " int " flags ", void *" "arg" ", ... "
811 .BI " /* pid_t *" ptid ", struct user_desc *" tls \
812 ", pid_t *" ctid " */ );"
815 The prototype shown above is for the glibc wrapper function;
816 the raw system call interface has no
820 argument, and changes the order of the arguments so that
822 is the first argument, and
824 is the last argument.
827 operates in the same way as
831 points to the lowest address of the child's stack area,
834 specifies the size of the stack pointed to by
835 .IR child_stack_base .
836 .SS Linux 2.4 and earlier
837 In Linux 2.4 and earlier,
839 does not take arguments
845 .\" gettid(2) returns current->pid;
846 .\" getpid(2) returns current->tgid;
847 On success, the thread ID of the child process is returned
848 in the caller's thread of execution.
849 On failure, \-1 is returned
850 in the caller's context, no child process will be created, and
852 will be set appropriately.
856 Too many processes are already running; see
864 (Since Linux 2.6.0-test6.)
871 (Since Linux 2.5.35.)
875 .\" .B CLONE_DETACHED
879 .\" (Since Linux 2.6.0-test6.)
908 when a zero value is specified for
915 but the kernel was not configured with the
925 but the kernel was not configured with the
933 but the kernel was not configured with the
941 but the kernel was not configured with the
946 Cannot allocate sufficient memory to allocate a task structure for the
947 child, or to copy those parts of the caller's context that need to be
957 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
961 was specified by a process other than process 0.
964 is Linux-specific and should not be used in programs
965 intended to be portable.
967 In the kernel 2.4.x series,
969 generally does not make the parent of the new thread the same
970 as the parent of the calling process.
971 However, for kernel versions 2.4.7 to 2.4.18 the
975 flag (as in kernel 2.6).
977 For a while there was
979 (introduced in 2.5.32):
980 parent wants no child-exit signal.
981 In 2.6.2 the need to give this
985 This flag is still defined, but has no effect.
989 should not be called through vsyscall, but directly through
992 Versions of the GNU C library that include the NPTL threading library
993 contain a wrapper function for
995 that performs caching of PIDs.
996 This caching relies on support in the glibc wrapper for
998 but as currently implemented,
999 the cache may not be up to date in some circumstances.
1001 if a signal is delivered to the child immediately after the
1003 call, then a call to
1005 in a handler for the signal may return the PID
1006 of the calling process ("the parent"),
1007 if the clone wrapper has not yet had a chance to update the PID
1009 (This discussion ignores the case where the child was created using
1014 return the same value in the child and in the process that called
1016 since the caller and the child are in the same thread group.
1017 The stale-cache problem also does not occur if the
1021 To get the truth, it may be necessary to use code such as the following:
1024 #include <syscall.h>
1028 mypid = syscall(SYS_getpid);
1030 .\" See also the following bug reports
1031 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1032 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1034 The following program demonstrates the use of
1036 to create a child process that executes in a separate UTS namespace.
1037 The child changes the hostname in its UTS namespace.
1038 Both parent and child then display the system hostname,
1039 making it possible to see that the hostname
1040 differs in the UTS namespaces of the parent and child.
1041 For an example of the use of this program, see
1046 #include <sys/wait.h>
1047 #include <sys/utsname.h>
1054 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1057 static int /* Start function for cloned child */
1058 childFunc(void *arg)
1062 /* Change hostname in UTS namespace of child */
1064 if (sethostname(arg, strlen(arg)) == \-1)
1065 errExit("sethostname");
1067 /* Retrieve and display hostname */
1069 if (uname(&uts) == \-1)
1071 printf("uts.nodename in child: %s\\n", uts.nodename);
1073 /* Keep the namespace open for a while, by sleeping.
1074 This allows some experimentation\-\-for example, another
1075 process might join the namespace. */
1079 return 0; /* Child terminates now */
1082 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1085 main(int argc, char *argv[])
1087 char *stack; /* Start of stack buffer */
1088 char *stackTop; /* End of stack buffer */
1093 fprintf(stderr, "Usage: %s <child\-hostname>\\n", argv[0]);
1097 /* Allocate stack for child */
1099 stack = malloc(STACK_SIZE);
1102 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1104 /* Create child that has its own UTS namespace;
1105 child commences execution in childFunc() */
1107 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1110 printf("clone() returned %ld\\n", (long) pid);
1112 /* Parent falls through to here */
1114 sleep(1); /* Give child time to change its hostname */
1116 /* Display hostname in parent\(aqs UTS namespace. This will be
1117 different from hostname in child\(aqs UTS namespace. */
1119 if (uname(&uts) == \-1)
1121 printf("uts.nodename in parent: %s\\n", uts.nodename);
1123 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1125 printf("child has terminated\\n");
1136 .BR set_thread_area (2),
1137 .BR set_tid_address (2),
1143 .BR capabilities (7),