2 .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992
3 .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013, 2019
5 .\" SPDX-License-Identifier: GPL-1.0-or-later
7 .\" Modified by Michael Haardt <michael@moria.de>
8 .\" Modified 24 Jul 1993 by Rik Faith <faith@cs.unc.edu>
9 .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>:
10 .\" New man page (copied from 'fork.2').
11 .\" Modified 10 June 1995 by Andries Brouwer <aeb@cwi.nl>
12 .\" Modified 25 April 1998 by Xavier Leroy <Xavier.Leroy@inria.fr>
13 .\" Modified 26 Jun 2001 by Michael Kerrisk
14 .\" Mostly upgraded to Linux 2.4.x
15 .\" Added prototype for sys_clone() plus description
16 .\" Added CLONE_THREAD with a brief description of thread groups
17 .\" Added CLONE_PARENT and revised entire page remove ambiguity
18 .\" between "calling process" and "parent process"
19 .\" Added CLONE_PTRACE and CLONE_VFORK
20 .\" Added EPERM and EINVAL error codes
21 .\" Renamed "__clone" to "clone" (which is the prototype in <sched.h>)
22 .\" various other minor tidy ups and clarifications.
23 .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk.manpages@gmail.com>
24 .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD
25 .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
26 .\" Added description for CLONE_NEWNS, which was added in Linux 2.4.19
27 .\" Slightly rephrased, aeb.
28 .\" Modified 1 Feb 2003 - added CLONE_SIGHAND restriction, aeb.
29 .\" Modified 1 Jan 2004 - various updates, aeb
30 .\" Modified 2004-09-10 - added CLONE_PARENT_SETTID etc. - aeb.
31 .\" 2005-04-12, mtk, noted the PID caching behavior of NPTL's getpid()
32 .\" wrapper under BUGS.
33 .\" 2005-05-10, mtk, added CLONE_SYSVSEM, CLONE_UNTRACED, CLONE_STOPPED.
34 .\" 2005-05-17, mtk, Substantially enhanced discussion of CLONE_THREAD.
35 .\" 2008-11-18, mtk, order CLONE_* flags alphabetically
36 .\" 2008-11-18, mtk, document CLONE_NEWPID
37 .\" 2008-11-19, mtk, document CLONE_NEWUTS
38 .\" 2008-11-19, mtk, document CLONE_NEWIPC
39 .\" 2008-11-19, Jens Axboe, mtk, document CLONE_IO
41 .TH clone 2 (date) "Linux man-pages (unreleased)"
43 clone, __clone2, clone3 \- create a child process
46 .RI ( libc ", " \-lc )
49 /* Prototype for the glibc wrapper function */
51 .B #define _GNU_SOURCE
54 .BI "int clone(int (*" "fn" ")(void *_Nullable), void *" stack \
56 .BI " void *_Nullable " "arg" ", ..." \
57 " \fR/*\fP" " pid_t *_Nullable " parent_tid ,
58 .BI " void *_Nullable " tls ,
59 .BI " pid_t *_Nullable " child_tid " \fR*/\fP );"
61 /* For the prototype of the raw clone() system call, see NOTES */
63 .BR "#include <linux/sched.h>" " /* Definition of " "struct clone_args" " */"
64 .BR "#include <sched.h>" " /* Definition of " CLONE_* " constants */"
65 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
66 .B #include <unistd.h>
68 .BI "long syscall(SYS_clone3, struct clone_args *" cl_args ", size_t " size );
72 glibc provides no wrapper for
74 necessitating the use of
78 create a new ("child") process, in a manner similar to
83 these system calls provide more precise control over what pieces of execution
84 context are shared between the calling process and the child process.
85 For example, using these system calls, the caller can control whether
86 or not the two processes share the virtual address space,
87 the table of file descriptors, and the table of signal handlers.
88 These system calls also allow the new child process to be placed
92 Note that in this manual
93 page, "calling process" normally corresponds to "parent process".
94 But see the descriptions of
100 This page describes the following interfaces:
104 wrapper function and the underlying system call on which it is based.
105 The main text describes the wrapper function;
106 the differences for the raw system call
107 are described toward the end of this page.
113 In the remainder of this page, the terminology "the clone call" is used
114 when noting details that apply to all of these interfaces.
116 .SS The clone() wrapper function
117 When the child process is created with the
120 it commences execution by calling the function pointed to by the argument
124 where execution continues in the child from the point
130 argument is passed as the argument of the function
135 function returns, the child process terminates.
136 The integer returned by
138 is the exit status for the child process.
139 The child process may also terminate explicitly by calling
141 or after receiving a fatal signal.
145 argument specifies the location of the stack used by the child process.
146 Since the child and calling process may share memory,
147 it is not possible for the child process to execute in the
148 same stack as the calling process.
149 The calling process must therefore
150 set up memory space for the child stack and pass a pointer to this
153 Stacks grow downward on all processors that run Linux
154 (except the HP PA processors), so
156 usually points to the topmost address of the memory space set up for
160 does not provide a means whereby the caller can inform the kernel of the
161 size of the stack area.
163 The remaining arguments to
170 system call provides a superset of the functionality of the older
173 It also provides a number of API improvements, including:
174 space for additional flags bits;
175 cleaner separation in the use of various arguments;
176 and the ability to specify the size of the child's stack area.
181 returns in both the parent and the child.
182 It returns 0 in the child process and returns the PID of the child
189 is a structure of the following form:
194 u64 flags; /* Flags bit mask */
195 u64 pidfd; /* Where to store PID file descriptor
197 u64 child_tid; /* Where to store child TID,
198 in child\[aq]s memory (\fIpid_t *\fP) */
199 u64 parent_tid; /* Where to store child TID,
200 in parent\[aq]s memory (\fIpid_t *\fP) */
201 u64 exit_signal; /* Signal to deliver to parent on
203 u64 stack; /* Pointer to lowest byte of stack */
204 u64 stack_size; /* Size of stack */
205 u64 tls; /* Location of new TLS */
206 u64 set_tid; /* Pointer to a \fIpid_t\fP array
208 u64 set_tid_size; /* Number of elements in \fIset_tid\fP
210 u64 cgroup; /* File descriptor for target cgroup
211 of child (since Linux 5.7) */
218 argument that is supplied to
220 should be initialized to the size of this structure.
221 (The existence of the
223 argument permits future extensions to the
227 The stack for the child process is specified via
229 which points to the lowest byte of the stack area,
231 .IR cl_args.stack_size ,
232 which specifies the size of the stack in bytes.
233 In the case where the
235 flag (see below) is specified, a stack must be explicitly allocated
237 Otherwise, these two fields can be specified as NULL and 0,
238 which causes the child to use the same stack area as the parent
239 (in the child's own virtual address space).
241 The remaining fields in the
243 argument are discussed below.
245 .SS Equivalence between clone() and clone3() arguments
248 interface, where arguments are passed individually, in the newer
250 interface the arguments are packaged into the
252 structure shown above.
253 This structure allows for a superset of the information passed via the
257 The following table shows the equivalence between the arguments of
259 and the fields in the
268 clone() clone3() Notes
270 flags & \[ti]0xff flags T{
271 For most flags; details below
273 parent_tid pidfd See CLONE_PIDFD
274 child_tid child_tid See CLONE_CHILD_SETTID
275 parent_tid parent_tid See CLONE_PARENT_SETTID
276 flags & 0xff exit_signal
279 tls tls See CLONE_SETTLS
280 \fP---\fP set_tid See below for details
281 \fP---\fP set_tid_size
282 \fP---\fP cgroup See CLONE_INTO_CGROUP
286 .SS The child termination signal
287 When the child process terminates, a signal may be sent to the parent.
288 The termination signal is specified in the low byte of
292 .I cl_args.exit_signal
294 If this signal is specified as anything other than
296 then the parent process must specify the
300 options when waiting for the child with
302 If no signal (i.e., zero) is specified, then the parent process is not signaled
303 when the child terminates.
305 .SS The set_tid array
306 By default, the kernel chooses the next sequential PID for the new
307 process in each of the PID namespaces where it is present.
308 When creating a process with
312 array (available since Linux 5.5)
313 can be used to select specific PIDs for the process in some
314 or all of the PID namespaces where it is present.
315 If the PID of the newly created process should be set only for the current
316 PID namespace or in the newly created PID namespace (if
320 then the first element in the
322 array has to be the desired PID and
326 If the PID of the newly created process should have a certain value in
327 multiple PID namespaces, then the
329 array can have multiple entries.
330 The first entry defines the PID in the most
331 deeply nested PID namespace and each of the following entries contains
333 corresponding ancestor PID namespace.
334 The number of PID namespaces in which a PID
335 should be set is defined by
337 which cannot be larger than the number of currently nested PID namespaces.
339 To create a process with the following PIDs in a PID namespace hierarchy:
344 PID NS level Requested PID Notes
345 0 31496 Outermost PID namespace
347 2 7 Innermost PID namespace
362 If only the PIDs in the two innermost PID namespaces
363 need to be specified, set the array to:
373 The PID in the PID namespaces outside the two innermost PID namespaces
374 is selected the same way as any other PID is selected.
382 .\" commit 124ea650d3072b005457faed69909221c2905a1f
383 .\" commit 1caef81da05a84a40dbf02110e967ce6d1135ff6
384 .B CAP_CHECKPOINT_RESTORE
385 in all owning user namespaces of the target PID namespaces.
387 Callers may only choose a PID greater than 1 in a given PID namespace
390 process (i.e., a process with PID 1) already exists in that namespace.
392 entry for this PID namespace must be 1.
399 allow a flags bit mask that modifies their behavior
400 and allows the caller to specify what is shared between the calling process
401 and the child process.
402 This bit mask\[em]the
412 mask in the remainder of this page.
416 mask is specified as a bitwise OR of zero or more of
417 the constants listed below.
418 Except as noted below, these flags are available
419 (and have the same effect) in both
424 .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
425 Clear (zero) the child thread ID at the location pointed to by
431 in child memory when the child exits, and do a wakeup on the futex
433 The address involved may be changed by the
434 .BR set_tid_address (2)
436 This is used by threading libraries.
438 .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
439 Store the child thread ID at the location pointed to by
445 in the child's memory.
446 The store operation completes before the clone call
447 returns control to user space in the child process.
448 (Note that the store operation may not have completed before the clone call
449 returns in the parent process, which is relevant if the
451 flag is also employed.)
453 .BR CLONE_CLEAR_SIGHAND " (since Linux 5.5)"
454 .\" commit b612e5df4587c934bd056bf05f4a1deca4de4f75
455 By default, signal dispositions in the child thread are the same as
457 If this flag is specified,
458 then all signals that are handled in the parent
461 are reset to their default dispositions
465 Specifying this flag together with
467 is nonsensical and disallowed.
469 .BR CLONE_DETACHED " (historical)"
470 For a while (during the Linux 2.5 development series)
471 .\" added in Linux 2.5.32; removed in Linux 2.6.0-test4
475 which caused the parent not to receive a signal when the child terminated.
476 Ultimately, the effect of this flag was subsumed under the
478 flag and by the time Linux 2.6.0 was released, this flag had no effect.
479 Starting in Linux 2.6.2, the need to give this flag together with
483 This flag is still defined, but it is usually ignored when calling
485 However, see the description of
489 .BR CLONE_FILES " (since Linux 2.0)"
492 is set, the calling process and the child process share the same file
494 Any file descriptor created by the calling process or by the child
495 process is also valid in the other process.
496 Similarly, if one of the processes closes a file descriptor,
497 or changes its associated flags (using the
500 operation), the other process is also affected.
501 If a process sharing a file descriptor table calls
503 its file descriptor table is duplicated (unshared).
507 is not set, the child process inherits a copy of all file descriptors
508 opened in the calling process at the time of the clone call.
509 Subsequent operations that open or close file descriptors,
510 or change file descriptor flags,
511 performed by either the calling
512 process or the child process do not affect the other process.
514 that the duplicated file descriptors in the child refer to the same
515 open file descriptions as the corresponding file descriptors
516 in the calling process,
517 and thus share file offsets and file status flags (see
520 .BR CLONE_FS " (since Linux 2.0)"
523 is set, the caller and the child process share the same filesystem
525 This includes the root of the filesystem, the current
526 working directory, and the umask.
532 performed by the calling process or the child process also affects the
537 is not set, the child process works on a copy of the filesystem
538 information of the calling process at the time of the clone call.
544 performed later by one of the processes do not affect the other process.
546 .BR CLONE_INTO_CGROUP " (since Linux 5.7)"
547 .\" commit ef2c41cf38a7559bbf91af42d5b6a4429db8fc68
548 By default, a child process is placed in the same version 2
549 cgroup as its parent.
552 flag allows the child process to be created in a different version 2 cgroup.
555 has effect only for version 2 cgroups.)
557 In order to place the child process in a different cgroup,
562 and passes a file descriptor that refers to a version 2 cgroup in the
565 (This file descriptor can be obtained by opening a cgroup v2 directory
571 Note that all of the usual restrictions (described in
573 on placing a process into a version 2 cgroup apply.
575 Among the possible use cases for
580 Spawning a process into a cgroup different from the parent's cgroup
581 makes it possible for a service manager to directly spawn new
582 services into dedicated cgroups.
583 This eliminates the accounting
584 jitter that would be caused if the child process was first created in the
585 same cgroup as the parent and then
586 moved into the target cgroup.
587 Furthermore, spawning the child process directly into a target cgroup
588 is significantly cheaper than moving the child process into
589 the target cgroup after it has been created.
593 flag also allows the creation of
594 frozen child processes by spawning them into a frozen cgroup.
597 for a description of the freezer controller.)
599 For threaded applications (or even thread implementations which
600 make use of cgroups to limit individual threads), it is possible to
601 establish a fixed cgroup layout before spawning each thread
602 directly into its target cgroup.
605 .BR CLONE_IO " (since Linux 2.6.25)"
608 is set, then the new process shares an I/O context with
610 If this flag is not set, then (as with
612 the new process has its own I/O context.
614 .\" The following based on text from Jens Axboe
615 The I/O context is the I/O scope of the disk scheduler (i.e.,
616 what the I/O scheduler uses to model scheduling of a process's I/O).
617 If processes share the same I/O context,
618 they are treated as one by the I/O scheduler.
619 As a consequence, they get to share disk time.
620 For some I/O schedulers,
621 .\" the anticipatory and CFQ scheduler
622 if two processes share an I/O context,
623 they will be allowed to interleave their disk access.
624 If several threads are doing I/O on behalf of the same process
626 for instance), they should employ
628 to get better I/O performance.
631 If the kernel is not configured with the
633 option, this flag is a no-op.
635 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
636 Create the process in a new cgroup namespace.
637 If this flag is not set, then (as with
639 the process is created in the same cgroup namespaces as the calling process.
641 For further information on cgroup namespaces, see
642 .BR cgroup_namespaces (7).
644 Only a privileged process
645 .RB ( CAP_SYS_ADMIN )
647 .BR CLONE_NEWCGROUP .
650 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
653 is set, then create the process in a new IPC namespace.
654 If this flag is not set, then (as with
656 the process is created in the same IPC namespace as
659 For further information on IPC namespaces, see
660 .BR ipc_namespaces (7).
662 Only a privileged process
663 .RB ( CAP_SYS_ADMIN )
666 This flag can't be specified in conjunction with
669 .BR CLONE_NEWNET " (since Linux 2.6.24)"
670 (The implementation of this flag was completed only
671 by about Linux 2.6.29.)
675 is set, then create the process in a new network namespace.
676 If this flag is not set, then (as with
678 the process is created in the same network namespace as
681 For further information on network namespaces, see
682 .BR network_namespaces (7).
684 Only a privileged process
685 .RB ( CAP_SYS_ADMIN )
689 .BR CLONE_NEWNS " (since Linux 2.4.19)"
692 is set, the cloned child is started in a new mount namespace,
693 initialized with a copy of the namespace of the parent.
696 is not set, the child lives in the same mount
697 namespace as the parent.
699 For further information on mount namespaces, see
702 .BR mount_namespaces (7).
704 Only a privileged process
705 .RB ( CAP_SYS_ADMIN )
708 It is not permitted to specify both
712 .\" See https://lwn.net/Articles/543273/
713 in the same clone call.
715 .BR CLONE_NEWPID " (since Linux 2.6.24)"
716 .\" This explanation draws a lot of details from
717 .\" http://lwn.net/Articles/259217/
718 .\" Authors: Pavel Emelyanov <xemul@openvz.org>
719 .\" and Kir Kolyshkin <kir@openvz.org>
721 .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
722 .\" Author: Pavel Emelyanov <xemul@openvz.org>
725 is set, then create the process in a new PID namespace.
726 If this flag is not set, then (as with
728 the process is created in the same PID namespace as
731 For further information on PID namespaces, see
734 .BR pid_namespaces (7).
736 Only a privileged process
737 .RB ( CAP_SYS_ADMIN )
740 This flag can't be specified in conjunction with
744 (This flag first became meaningful for
749 semantics were merged in Linux 3.5,
750 and the final pieces to make the user namespaces completely usable were
751 merged in Linux 3.8.)
755 is set, then create the process in a new user namespace.
756 If this flag is not set, then (as with
758 the process is created in the same user namespace as the calling process.
760 For further information on user namespaces, see
763 .BR user_namespaces (7).
765 Before Linux 3.8, use of
767 required that the caller have three capabilities:
772 .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
773 Starting with Linux 3.8,
774 no privileges are needed to create a user namespace.
776 This flag can't be specified in conjunction with
780 For security reasons,
781 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
782 .\" https://lwn.net/Articles/543273/
783 .\" The fix actually went into Linux 3.9 and into Linux 3.8.3. However, user namespaces
784 .\" were, for practical purposes, unusable in earlier Linux 3.8.x because of the
785 .\" various filesystems that didn't support userns.
787 cannot be specified in conjunction with
790 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
793 is set, then create the process in a new UTS namespace,
794 whose identifiers are initialized by duplicating the identifiers
795 from the UTS namespace of the calling process.
796 If this flag is not set, then (as with
798 the process is created in the same UTS namespace as
801 For further information on UTS namespaces, see
802 .BR uts_namespaces (7).
804 Only a privileged process
805 .RB ( CAP_SYS_ADMIN )
809 .BR CLONE_PARENT " (since Linux 2.3.12)"
812 is set, then the parent of the new child (as returned by
814 will be the same as that of the calling process.
818 is not set, then (as with
820 the child's parent is the calling process.
822 Note that it is the parent process, as returned by
824 which is signaled when the child terminates, so that
827 is set, then the parent of the calling process, rather than the
828 calling process itself, is signaled.
832 flag can't be used in clone calls by the
833 global init process (PID 1 in the initial PID namespace)
834 and init processes in other PID namespaces.
835 This restriction prevents the creation of multi-rooted process trees
836 as well as the creation of unreapable zombies in the initial PID namespace.
838 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
839 Store the child thread ID at the location pointed to by
843 .I cl_args.parent_tid
845 in the parent's memory.
846 (In Linux 2.5.32-2.5.48 there was a flag
849 The store operation completes before the clone call
850 returns control to user space.
852 .BR CLONE_PID " (Linux 2.0 to Linux 2.5.15)"
855 is set, the child process is created with the same process ID as
857 This is good for hacking the system, but otherwise
859 From Linux 2.3.21 onward, this flag could be
860 specified only by the system boot process (PID 0).
861 The flag disappeared completely from the kernel sources in Linux 2.5.16.
862 Subsequently, the kernel silently ignored this bit if it was specified in the
865 Much later, the same bit was recycled for use as the
869 .BR CLONE_PIDFD " (since Linux 5.2)"
870 .\" commit b3e5838252665ee4cfa76b82bdf1198dca81e5be
871 If this flag is specified,
872 a PID file descriptor referring to the child process is allocated
873 and placed at a specified location in the parent's memory.
874 The close-on-exec flag is set on this new file descriptor.
875 PID file descriptors can be used for the purposes described in
881 the PID file descriptor is placed at the location pointed to by
886 the PID file descriptor is placed at the location pointed to by
890 argument is used to return the PID file descriptor,
893 .B CLONE_PARENT_SETTID
898 It is currently not possible to use this flag together with
900 This means that the process identified by the PID file descriptor
901 will always be a thread group leader.
905 flag is specified alongside
909 an error is returned.
910 An error also results if
912 is specified when calling
914 This error behavior ensures that the bit corresponding to
916 can be reused for further PID file descriptor features in the future.
918 .BR CLONE_PTRACE " (since Linux 2.2)"
921 is specified, and the calling process is being traced,
922 then trace the child also (see
925 .BR CLONE_SETTLS " (since Linux 2.5.32)"
926 The TLS (Thread Local Storage) descriptor is set to
929 The interpretation of
931 and the resulting effect is architecture dependent.
935 .I struct user_desc\~*
937 .BR set_thread_area (2)).
938 On x86-64 it is the new value to be set for the %fs base register
943 On architectures with a dedicated TLS register, it is the new value
946 Use of this flag requires detailed knowledge and generally it
947 should not be used except in libraries implementing threading.
949 .BR CLONE_SIGHAND " (since Linux 2.0)"
952 is set, the calling process and the child process share the same table of
954 If the calling process or child process calls
956 to change the behavior associated with a signal, the behavior is
957 changed in the other process as well.
958 However, the calling process and child
959 processes still have distinct signal masks and sets of pending
961 So, one of them may block or unblock signals using
963 without affecting the other process.
967 is not set, the child process inherits a copy of the signal handlers
968 of the calling process at the time of the clone call.
971 performed later by one of the processes have no effect on the other
975 .\" Precisely: Linux 2.6.0-test6
978 mask must also include
984 .BR CLONE_STOPPED " (since Linux 2.6.0)"
985 .\" Precisely: Linux 2.6.0-test2
988 is set, then the child is initially stopped (as though it was sent a
990 signal), and must be resumed by sending it a
996 from Linux 2.6.25 onward,
999 altogether in Linux 2.6.38.
1000 Since then, the kernel silently ignores it without error.
1001 .\" glibc 2.8 removed this defn from bits/sched.h
1002 Starting with Linux 4.6, the same bit was reused for the
1006 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
1009 is set, then the child and the calling process share
1010 a single list of System V semaphore adjustment
1014 In this case, the shared list accumulates
1016 values across all processes sharing the list,
1017 and semaphore adjustments are performed only when the last process
1018 that is sharing the list terminates (or ceases sharing the list using
1020 If this flag is not set, then the child has a separate
1022 list that is initially empty.
1024 .BR CLONE_THREAD " (since Linux 2.4.0)"
1025 .\" Precisely: Linux 2.6.0-test8
1028 is set, the child is placed in the same thread group as the calling process.
1029 To make the remainder of the discussion of
1031 more readable, the term "thread" is used to refer to the
1032 processes within a thread group.
1034 Thread groups were a feature added in Linux 2.4 to support the
1035 POSIX threads notion of a set of threads that share a single PID.
1036 Internally, this shared PID is the so-called
1037 thread group identifier (TGID) for the thread group.
1038 Since Linux 2.4, calls to
1040 return the TGID of the caller.
1042 The threads within a group can be distinguished by their (system-wide)
1043 unique thread IDs (TID).
1044 A new thread's TID is available as the function result
1045 returned to the caller,
1046 and a thread can obtain
1050 When a clone call is made without specifying
1052 then the resulting thread is placed in a new thread group
1053 whose TGID is the same as the thread's TID.
1056 of the new thread group.
1058 A new thread created with
1060 has the same parent process as the process that made the clone call
1065 return the same value for all of the threads in a thread group.
1068 thread terminates, the thread that created it is not sent a
1070 (or other termination) signal;
1071 nor can the status of such a thread be obtained
1074 (The thread is said to be
1077 After all of the threads in a thread group terminate
1078 the parent process of the thread group is sent a
1080 (or other termination) signal.
1082 If any of the threads in a thread group performs an
1084 then all threads other than the thread group leader are terminated,
1085 and the new program is executed in the thread group leader.
1087 If one of the threads in a thread group creates a child using
1089 then any thread in the group can
1093 Since Linux 2.5.35, the
1095 mask must also include
1100 (and note that, since Linux 2.6.0,
1101 .\" Precisely: Linux 2.6.0-test6
1107 Signal dispositions and actions are process-wide:
1108 if an unhandled signal is delivered to a thread, then
1109 it will affect (terminate, stop, continue, be ignored in)
1110 all members of the thread group.
1112 Each thread has its own signal mask, as set by
1113 .BR sigprocmask (2).
1115 A signal may be process-directed or thread-directed.
1116 A process-directed signal is targeted at a thread group (i.e., a TGID),
1117 and is delivered to an arbitrarily selected thread from among those
1118 that are not blocking the signal.
1119 A signal may be process-directed because it was generated by the kernel
1120 for reasons other than a hardware exception, or because it was sent using
1124 A thread-directed signal is targeted at (i.e., delivered to)
1126 A signal may be thread directed because it was sent using
1129 .BR pthread_sigqueue (3),
1130 or because the thread executed a machine language instruction that triggered
1131 a hardware exception
1132 (e.g., invalid memory access triggering
1134 or a floating-point exception triggering
1139 returns a signal set that is the union of the pending process-directed
1140 signals and the signals that are pending for the calling thread.
1142 If a process-directed signal is delivered to a thread group,
1143 and the thread group has installed a handler for the signal, then
1144 the handler is invoked in exactly one, arbitrarily selected
1145 member of the thread group that has not blocked the signal.
1146 If multiple threads in a group are waiting to accept the same signal using
1147 .BR sigwaitinfo (2),
1148 the kernel will arbitrarily select one of these threads
1149 to receive the signal.
1151 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
1154 is specified, then a tracing process cannot force
1156 on this child process.
1158 .BR CLONE_VFORK " (since Linux 2.2)"
1161 is set, the execution of the calling process is suspended
1162 until the child releases its virtual memory
1163 resources via a call to
1172 is not set, then both the calling process and the child are schedulable
1173 after the call, and an application should not rely on execution occurring
1174 in any particular order.
1176 .BR CLONE_VM " (since Linux 2.0)"
1179 is set, the calling process and the child process run in the same memory
1181 In particular, memory writes performed by the calling process
1182 or by the child process are also visible in the other process.
1183 Moreover, any memory mapping or unmapping performed with
1187 by the child or calling process also affects the other process.
1191 is not set, the child process runs in a separate copy of the memory
1192 space of the calling process at the time of the clone call.
1193 Memory writes or file mappings/unmappings performed by one of the
1194 processes do not affect the other, as with
1199 flag is specified and the
1201 flag is not specified,
1202 then any alternate signal stack that was established by
1204 is cleared in the child process.
1206 .\" gettid(2) returns current->pid;
1207 .\" getpid(2) returns current->tgid;
1208 On success, the thread ID of the child process is returned
1209 in the caller's thread of execution.
1210 On failure, \-1 is returned
1211 in the caller's context, no child process is created, and
1213 is set to indicate the error.
1216 .BR EACCES " (" clone3 "() only)"
1217 .B CLONE_INTO_CGROUP
1220 but the restrictions (described in
1222 on placing the child process into the version 2 cgroup referred to by
1227 Too many processes are already running; see
1230 .BR EBUSY " (" clone3 "() only)"
1231 .B CLONE_INTO_CGROUP
1234 but the file descriptor specified in
1236 refers to a version 2 cgroup in which a domain controller is enabled.
1238 .BR EEXIST " (" clone3 "() only)"
1239 One (or more) of the PIDs specified in
1241 already exists in the corresponding PID namespace.
1247 .B CLONE_CLEAR_SIGHAND
1248 were specified in the
1254 was specified in the
1259 (Since Linux 2.6.0.)
1260 .\" Precisely: Linux 2.6.0-test6
1264 was specified in the
1269 (Since Linux 2.5.35.)
1272 .\" Precisely one of
1273 .\" .B CLONE_DETACHED
1277 .\" (Since Linux 2.6.0-test6.)
1281 was specified in the
1283 mask, but the current process previously called
1289 to reassociate itself with a PID namespace.
1292 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
1297 were specified in the
1301 .BR EINVAL " (since Linux 3.9)"
1306 were specified in the
1315 were specified in the
1321 and one (or both) of
1325 were specified in the
1333 were specified in the
1337 .BR EINVAL " (since Linux 2.6.32)"
1338 .\" commit 123be07b0b399670a7cc3d82fef0cb4f93ef885c
1340 was specified, and the caller is an init process.
1343 Returned by the glibc
1345 wrapper function when
1349 is specified as NULL.
1353 was specified in the
1356 but the kernel was not configured with the
1364 was specified in the
1367 but the kernel was not configured with the
1373 was specified in the
1376 but the kernel was not configured with the
1382 was specified in the
1385 but the kernel was not configured with the
1391 was specified in the
1394 but the kernel was not configured with the
1400 is not aligned to a suitable boundary for this architecture.
1401 For example, on aarch64,
1403 must be a multiple of 16.
1405 .BR EINVAL " (" clone3 "() only)"
1407 was specified in the
1411 .BR EINVAL " (" clone "() only)"
1413 was specified together with
1421 was specified together with
1427 .BR "EINVAL " "(" clone "() only)"
1429 was specified together with
1430 .B CLONE_PARENT_SETTID
1435 .BR EINVAL " (" clone3 "() only)"
1437 is greater than the number of nested PID namespaces.
1439 .BR EINVAL " (" clone3 "() only)"
1440 One of the PIDs specified in
1444 .BR EINVAL " (" clone3 "() only)"
1445 .\" commit 7f192e3cd316ba58c88dfa26796cf77789dd9872
1449 was specified in the
1451 mask, but a signal was specified in
1454 .BR EINVAL " (AArch64 only, Linux 4.6 and earlier)"
1456 was not aligned to a 128-bit boundary.
1459 Cannot allocate sufficient memory to allocate a task structure for the
1460 child, or to copy those parts of the caller's context that need to be
1463 .BR ENOSPC " (since Linux 3.7)"
1464 .\" commit f2302505775fd13ba93f034206f1e2a587017929
1466 was specified in the
1469 but the limit on the nesting depth of PID namespaces
1470 would have been exceeded; see
1471 .BR pid_namespaces (7).
1473 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
1475 was specified in the
1477 mask, and the call would cause the limit on the number of
1478 nested user namespaces to be exceeded.
1480 .BR user_namespaces (7).
1482 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
1485 .BR ENOSPC " (since Linux 4.9)"
1486 One of the values in the
1488 mask specified the creation of a new user namespace,
1489 but doing so would have caused the limit defined by the corresponding file in
1492 For further details, see
1495 .BR EOPNOTSUPP " (" clone3 "() only)"
1496 .B CLONE_INTO_CGROUP
1499 but the file descriptor specified in
1501 refers to a version 2 cgroup that is in the
1506 .BR CLONE_NEWCGROUP ,
1513 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1517 was specified by a process other than process 0.
1518 (This error occurs only on Linux 2.5.15 and earlier.)
1522 was specified in the
1525 but either the effective user ID or the effective group ID of the caller
1526 does not have a mapping in the parent namespace (see
1527 .BR user_namespaces (7)).
1529 .BR EPERM " (since Linux 3.9)"
1530 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1532 was specified in the
1534 mask and the caller is in a chroot environment
1535 .\" FIXME What is the rationale for this restriction?
1536 (i.e., the caller's root directory does not match the root directory
1537 of the mount namespace in which it resides).
1539 .BR EPERM " (" clone3 "() only)"
1541 was greater than zero, and the caller lacks the
1543 capability in one or more of the user namespaces that own the
1544 corresponding PID namespaces.
1546 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
1547 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
1548 System call was interrupted by a signal and will be restarted.
1549 (This can be seen only during a trace.)
1551 .BR EUSERS " (Linux 3.11 to Linux 4.8)"
1553 was specified in the
1556 and the limit on the number of nested user namespaces would be exceeded.
1557 See the discussion of the
1563 wrapper function makes some changes
1564 in the memory pointed to by
1566 (changes required to set the stack up correctly for the child)
1573 is used to recursively create children,
1574 do not use the buffer employed for the parent's stack
1575 as the stack of the child.
1579 should not be called through vsyscall, but directly through
1581 .SS C library/kernel differences
1584 system call corresponds more closely to
1586 in that execution in the child continues from the point of the
1594 wrapper function are omitted.
1596 In contrast to the glibc wrapper, the raw
1598 system call accepts NULL as a
1605 In this case, the child uses a duplicate of the parent's stack.
1606 (Copy-on-write semantics ensure that the child gets separate copies
1607 of stack pages when either process modifies the stack.)
1608 In this case, for correct operation, the
1610 option should not be specified.
1613 the parent's memory because of the use of the
1616 then no copy-on-write duplication occurs and chaos is likely to result.)
1618 The order of the arguments also differs in the raw system call,
1619 and there are variations in the arguments across architectures,
1620 as detailed in the following paragraphs.
1622 The raw system call interface on x86-64 and some other architectures
1623 (including sh, tile, and alpha) is:
1627 .BI "long clone(unsigned long " flags ", void *" stack ,
1628 .BI " int *" parent_tid ", int *" child_tid ,
1629 .BI " unsigned long " tls );
1633 On x86-32, and several other common architectures
1634 (including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
1636 .\" CONFIG_CLONE_BACKWARDS
1637 the order of the last two arguments is reversed:
1641 .BI "long clone(unsigned long " flags ", void *" stack ,
1642 .BI " int *" parent_tid ", unsigned long " tls ,
1643 .BI " int *" child_tid );
1647 On the cris and s390 architectures,
1648 .\" CONFIG_CLONE_BACKWARDS2
1649 the order of the first two arguments is reversed:
1653 .BI "long clone(void *" stack ", unsigned long " flags ,
1654 .BI " int *" parent_tid ", int *" child_tid ,
1655 .BI " unsigned long " tls );
1659 On the microblaze architecture,
1660 .\" CONFIG_CLONE_BACKWARDS3
1661 an additional argument is supplied:
1665 .BI "long clone(unsigned long " flags ", void *" stack ,
1666 .BI " int " stack_size , "\fR /* Size of stack */"
1667 .BI " int *" parent_tid ", int *" child_tid ,
1668 .BI " unsigned long " tls );
1672 .SS blackfin, m68k, and sparc
1673 .\" Mike Frysinger noted in a 2013 mail:
1674 .\" these arches don't define __ARCH_WANT_SYS_CLONE:
1675 .\" blackfin ia64 m68k sparc
1676 The argument-passing conventions on
1677 blackfin, m68k, and sparc are different from the descriptions above.
1678 For details, see the kernel (and glibc) source.
1680 On ia64, a different interface is used:
1684 .BI "int __clone2(int (*" "fn" ")(void *),"
1685 .BI " void *" stack_base ", size_t " stack_size ,
1686 .BI " int " flags ", void *" "arg" ", ..."
1687 .BI " /* pid_t *" parent_tid ", struct user_desc *" tls ,
1688 .BI " pid_t *" child_tid " */ );"
1692 The prototype shown above is for the glibc wrapper function;
1693 for the system call itself,
1694 the prototype can be described as follows (it is identical to the
1696 prototype on microblaze):
1700 .BI "long clone2(unsigned long " flags ", void *" stack_base ,
1701 .BI " int " stack_size , "\fR /* Size of stack */"
1702 .BI " int *" parent_tid ", int *" child_tid ,
1703 .BI " unsigned long " tls );
1708 operates in the same way as
1712 points to the lowest address of the child's stack area,
1715 specifies the size of the stack pointed to by
1723 .\" There is no entry for
1728 .\" as described in this manual page.
1729 .SS Linux 2.4 and earlier
1730 In the Linux 2.4.x series,
1732 generally does not make the parent of the new thread the same
1733 as the parent of the calling process.
1734 However, from Linux 2.4.7 to Linux 2.4.18 the
1738 flag (as in Linux 2.6.0 and later).
1740 In Linux 2.4 and earlier,
1742 does not take arguments
1748 One use of these system calls
1749 is to implement threads: multiple flows of control in a program that
1750 run concurrently in a shared address space.
1754 system call can be used to test whether two processes share various
1755 resources such as a file descriptor table,
1756 System V semaphore undo operations, or a virtual address space.
1758 Handlers registered using
1759 .BR pthread_atfork (3)
1760 are not executed during a clone call.
1762 GNU C library versions 2.3.4 up to and including 2.24
1763 contained a wrapper function for
1765 that performed caching of PIDs.
1766 This caching relied on support in the glibc wrapper for
1768 but limitations in the implementation
1769 meant that the cache was not up to date in some circumstances.
1771 if a signal was delivered to the child immediately after the
1773 call, then a call to
1775 in a handler for the signal could return the PID
1776 of the calling process ("the parent"),
1777 if the clone wrapper had not yet had a chance to update the PID
1779 (This discussion ignores the case where the child was created using
1784 return the same value in the child and in the process that called
1786 since the caller and the child are in the same thread group.
1787 The stale-cache problem also does not occur if the
1791 To get the truth, it was sometimes necessary to use code such as the following:
1795 #include <syscall.h>
1799 mypid = syscall(SYS_getpid);
1802 .\" See also the following bug reports
1803 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1804 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1806 Because of the stale-cache problem, as well as other problems noted in
1808 the PID caching feature was removed in glibc 2.25.
1810 The following program demonstrates the use of
1812 to create a child process that executes in a separate UTS namespace.
1813 The child changes the hostname in its UTS namespace.
1814 Both parent and child then display the system hostname,
1815 making it possible to see that the hostname
1816 differs in the UTS namespaces of the parent and child.
1817 For an example of the use of this program, see
1820 Within the sample program, we allocate the memory that is to
1821 be used for the child's stack using
1825 for the following reasons:
1828 allocates a block of memory that starts on a page
1829 boundary and is a multiple of the page size.
1830 This is useful if we want to establish a guard page (a page with protection
1832 at the end of the stack using
1837 flag to request a mapping that is suitable for a stack.
1838 For the moment, this flag is a no-op on Linux,
1839 but it exists and has effect on some other systems,
1840 so we should include it for portability.
1842 .\" SRC BEGIN (clone.c)
1852 #include <sys/mman.h>
1853 #include <sys/utsname.h>
1854 #include <sys/wait.h>
1857 static int /* Start function for cloned child */
1858 childFunc(void *arg)
1862 /* Change hostname in UTS namespace of child. */
1864 if (sethostname(arg, strlen(arg)) == \-1)
1865 err(EXIT_FAILURE, "sethostname");
1867 /* Retrieve and display hostname. */
1869 if (uname(&uts) == \-1)
1870 err(EXIT_FAILURE, "uname");
1871 printf("uts.nodename in child: %s\en", uts.nodename);
1873 /* Keep the namespace open for a while, by sleeping.
1874 This allows some experimentation\-\-for example, another
1875 process might join the namespace. */
1879 return 0; /* Child terminates now */
1882 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1885 main(int argc, char *argv[])
1887 char *stack; /* Start of stack buffer */
1888 char *stackTop; /* End of stack buffer */
1893 fprintf(stderr, "Usage: %s <child\-hostname>\en", argv[0]);
1897 /* Allocate memory to be used for the stack of the child. */
1899 stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
1900 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, \-1, 0);
1901 if (stack == MAP_FAILED)
1902 err(EXIT_FAILURE, "mmap");
1904 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1906 /* Create child that has its own UTS namespace;
1907 child commences execution in childFunc(). */
1909 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1911 err(EXIT_FAILURE, "clone");
1912 printf("clone() returned %jd\en", (intmax_t) pid);
1914 /* Parent falls through to here */
1916 sleep(1); /* Give child time to change its hostname */
1918 /* Display hostname in parent\[aq]s UTS namespace. This will be
1919 different from hostname in child\[aq]s UTS namespace. */
1921 if (uname(&uts) == \-1)
1922 err(EXIT_FAILURE, "uname");
1923 printf("uts.nodename in parent: %s\en", uts.nodename);
1925 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1926 err(EXIT_FAILURE, "waitpid");
1927 printf("child has terminated\en");
1941 .BR set_thread_area (2),
1942 .BR set_tid_address (2),
1947 .BR capabilities (7),