1 .\" Copyright (c) 1992 Drew Eckhardt <drew@cs.colorado.edu>, March 28, 1992
2 .\" and Copyright (c) Michael Kerrisk, 2001, 2002, 2005, 2013, 2019
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 2021-03-22 "Linux" "Linux Programmer's Manual"
44 clone, __clone2, clone3 \- create a child process
47 /* Prototype for the glibc wrapper function */
49 .B #define _GNU_SOURCE
52 .BI "int clone(int (*" "fn" ")(void *), void *" stack \
53 ", int " flags ", void *" "arg" ", ..."
54 .BI " /* pid_t *" parent_tid ", void *" tls \
55 ", pid_t *" child_tid " */ );"
57 /* For the prototype of the raw clone() system call, see NOTES */
59 .BR "#include <linux/sched.h>" " /* Definition of " "struct clone_args" " */"
60 .BR "#include <sched.h>" " /* Definition of " CLONE_* " constants */"
61 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
62 .B #include <unistd.h>
64 .BI "long syscall(SYS_clone3, struct clone_args *" cl_args ", size_t " size );
68 glibc provides no wrapper for
70 necessitating the use of
74 create a new ("child") process, in a manner similar to
79 these system calls provide more precise control over what pieces of execution
80 context are shared between the calling process and the child process.
81 For example, using these system calls, the caller can control whether
82 or not the two processes share the virtual address space,
83 the table of file descriptors, and the table of signal handlers.
84 These system calls also allow the new child process to be placed
88 Note that in this manual
89 page, "calling process" normally corresponds to "parent process".
90 But see the descriptions of
96 This page describes the following interfaces:
100 wrapper function and the underlying system call on which it is based.
101 The main text describes the wrapper function;
102 the differences for the raw system call
103 are described toward the end of this page.
109 In the remainder of this page, the terminology "the clone call" is used
110 when noting details that apply to all of these interfaces,
112 .SS The clone() wrapper function
113 When the child process is created with the
116 it commences execution by calling the function pointed to by the argument
120 where execution continues in the child from the point
126 argument is passed as the argument of the function
131 function returns, the child process terminates.
132 The integer returned by
134 is the exit status for the child process.
135 The child process may also terminate explicitly by calling
137 or after receiving a fatal signal.
141 argument specifies the location of the stack used by the child process.
142 Since the child and calling process may share memory,
143 it is not possible for the child process to execute in the
144 same stack as the calling process.
145 The calling process must therefore
146 set up memory space for the child stack and pass a pointer to this
149 Stacks grow downward on all processors that run Linux
150 (except the HP PA processors), so
152 usually points to the topmost address of the memory space set up for
156 does not provide a means whereby the caller can inform the kernel of the
157 size of the stack area.
159 The remaining arguments to
166 system call provides a superset of the functionality of the older
169 It also provides a number of API improvements, including:
170 space for additional flags bits;
171 cleaner separation in the use of various arguments;
172 and the ability to specify the size of the child's stack area.
177 returns in both the parent and the child.
178 It returns 0 in the child process and returns the PID of the child
185 is a structure of the following form:
190 u64 flags; /* Flags bit mask */
191 u64 pidfd; /* Where to store PID file descriptor
193 u64 child_tid; /* Where to store child TID,
194 in child\(aqs memory (\fIpid_t *\fP) */
195 u64 parent_tid; /* Where to store child TID,
196 in parent\(aqs memory (\fIpid_t *\fP) */
197 u64 exit_signal; /* Signal to deliver to parent on
199 u64 stack; /* Pointer to lowest byte of stack */
200 u64 stack_size; /* Size of stack */
201 u64 tls; /* Location of new TLS */
202 u64 set_tid; /* Pointer to a \fIpid_t\fP array
204 u64 set_tid_size; /* Number of elements in \fIset_tid\fP
206 u64 cgroup; /* File descriptor for target cgroup
207 of child (since Linux 5.7) */
214 argument that is supplied to
216 should be initialized to the size of this structure.
217 (The existence of the
219 argument permits future extensions to the
223 The stack for the child process is specified via
225 which points to the lowest byte of the stack area,
227 .IR cl_args.stack_size ,
228 which specifies the size of the stack in bytes.
229 In the case where the
231 flag (see below) is specified, a stack must be explicitly allocated
233 Otherwise, these two fields can be specified as NULL and 0,
234 which causes the child to use the same stack area as the parent
235 (in the child's own virtual address space).
237 The remaining fields in the
239 argument are discussed below.
241 .SS Equivalence between clone() and clone3() arguments
244 interface, where arguments are passed individually, in the newer
246 interface the arguments are packaged into the
248 structure shown above.
249 This structure allows for a superset of the information passed via the
253 The following table shows the equivalence between the arguments of
255 and the fields in the
264 clone() clone3() Notes
266 flags & \(ti0xff flags T{
267 For most flags; details below
269 parent_tid pidfd See CLONE_PIDFD
270 child_tid child_tid See CLONE_CHILD_SETTID
271 parent_tid parent_tid See CLONE_PARENT_SETTID
272 flags & 0xff exit_signal
275 tls tls See CLONE_SETTLS
276 \fP---\fP set_tid See below for details
277 \fP---\fP set_tid_size
278 \fP---\fP cgroup See CLONE_INTO_CGROUP
282 .SS The child termination signal
283 When the child process terminates, a signal may be sent to the parent.
284 The termination signal is specified in the low byte of
288 .I cl_args.exit_signal
290 If this signal is specified as anything other than
292 then the parent process must specify the
296 options when waiting for the child with
298 If no signal (i.e., zero) is specified, then the parent process is not signaled
299 when the child terminates.
301 .SS The set_tid array
302 By default, the kernel chooses the next sequential PID for the new
303 process in each of the PID namespaces where it is present.
304 When creating a process with
308 array (available since Linux 5.5)
309 can be used to select specific PIDs for the process in some
310 or all of the PID namespaces where it is present.
311 If the PID of the newly created process should be set only for the current
312 PID namespace or in the newly created PID namespace (if
316 then the first element in the
318 array has to be the desired PID and
322 If the PID of the newly created process should have a certain value in
323 multiple PID namespaces, then the
325 array can have multiple entries.
326 The first entry defines the PID in the most
327 deeply nested PID namespace and each of the following entries contains
329 corresponding ancestor PID namespace.
330 The number of PID namespaces in which a PID
331 should be set is defined by
333 which cannot be larger than the number of currently nested PID namespaces.
335 To create a process with the following PIDs in a PID namespace hierarchy:
340 PID NS level Requested PID Notes
341 0 31496 Outermost PID namespace
343 2 7 Innermost PID namespace
358 If only the PIDs in the two innermost PID namespaces
359 need to be specified, set the array to:
369 The PID in the PID namespaces outside the two innermost PID namespaces
370 is selected the same way as any other PID is selected.
378 .\" commit 124ea650d3072b005457faed69909221c2905a1f
379 .\" commit 1caef81da05a84a40dbf02110e967ce6d1135ff6
380 .BR CAP_CHECKPOINT_RESTORE
381 in all owning user namespaces of the target PID namespaces.
383 Callers may only choose a PID greater than 1 in a given PID namespace
386 process (i.e., a process with PID 1) already exists in that namespace.
388 entry for this PID namespace must be 1.
395 allow a flags bit mask that modifies their behavior
396 and allows the caller to specify what is shared between the calling process
397 and the child process.
408 mask in the remainder of this page.
412 mask is specified as a bitwise-OR of zero or more of
413 the constants listed below.
414 Except as noted below, these flags are available
415 (and have the same effect) in both
420 .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)"
421 Clear (zero) the child thread ID at the location pointed to by
427 in child memory when the child exits, and do a wakeup on the futex
429 The address involved may be changed by the
430 .BR set_tid_address (2)
432 This is used by threading libraries.
434 .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)"
435 Store the child thread ID at the location pointed to by
441 in the child's memory.
442 The store operation completes before the clone call
443 returns control to user space in the child process.
444 (Note that the store operation may not have completed before the clone call
445 returns in the parent process, which is relevant if the
447 flag is also employed.)
449 .BR CLONE_CLEAR_SIGHAND " (since Linux 5.5)"
450 .\" commit b612e5df4587c934bd056bf05f4a1deca4de4f75
451 By default, signal dispositions in the child thread are the same as
453 If this flag is specified,
454 then all signals that are handled in the parent
455 are reset to their default dispositions
459 Specifying this flag together with
461 is nonsensical and disallowed.
463 .BR CLONE_DETACHED " (historical)"
464 For a while (during the Linux 2.5 development series)
465 .\" added in 2.5.32; removed in 2.6.0-test4
469 which caused the parent not to receive a signal when the child terminated.
470 Ultimately, the effect of this flag was subsumed under the
472 flag and by the time Linux 2.6.0 was released, this flag had no effect.
473 Starting in Linux 2.6.2, the need to give this flag together with
477 This flag is still defined, but it is usually ignored when calling
479 However, see the description of
483 .BR CLONE_FILES " (since Linux 2.0)"
486 is set, the calling process and the child process share the same file
488 Any file descriptor created by the calling process or by the child
489 process is also valid in the other process.
490 Similarly, if one of the processes closes a file descriptor,
491 or changes its associated flags (using the
494 operation), the other process is also affected.
495 If a process sharing a file descriptor table calls
497 its file descriptor table is duplicated (unshared).
501 is not set, the child process inherits a copy of all file descriptors
502 opened in the calling process at the time of the clone call.
503 Subsequent operations that open or close file descriptors,
504 or change file descriptor flags,
505 performed by either the calling
506 process or the child process do not affect the other process.
508 that the duplicated file descriptors in the child refer to the same
509 open file descriptions as the corresponding file descriptors
510 in the calling process,
511 and thus share file offsets and file status flags (see
514 .BR CLONE_FS " (since Linux 2.0)"
517 is set, the caller and the child process share the same filesystem
519 This includes the root of the filesystem, the current
520 working directory, and the umask.
526 performed by the calling process or the child process also affects the
531 is not set, the child process works on a copy of the filesystem
532 information of the calling process at the time of the clone call.
538 performed later by one of the processes do not affect the other process.
540 .BR CLONE_INTO_CGROUP " (since Linux 5.7)"
541 .\" commit ef2c41cf38a7559bbf91af42d5b6a4429db8fc68
542 By default, a child process is placed in the same version 2
543 cgroup as its parent.
546 flag allows the child process to be created in a different version 2 cgroup.
548 .BR CLONE_INTO_CGROUP
549 has effect only for version 2 cgroups.)
551 In order to place the child process in a different cgroup,
553 .BR CLONE_INTO_CGROUP
556 and passes a file descriptor that refers to a version 2 cgroup in the
559 (This file descriptor can be obtained by opening a cgroup v2 directory
565 Note that all of the usual restrictions (described in
567 on placing a process into a version 2 cgroup apply.
569 Among the possible use cases for
570 .BR CLONE_INTO_CGROUP
574 Spawning a process into a cgroup different from the parent's cgroup
575 makes it possible for a service manager to directly spawn new
576 services into dedicated cgroups.
577 This eliminates the accounting
578 jitter that would be caused if the child process was first created in the
579 same cgroup as the parent and then
580 moved into the target cgroup.
581 Furthermore, spawning the child process directly into a target cgroup
582 is significantly cheaper than moving the child process into
583 the target cgroup after it has been created.
586 .BR CLONE_INTO_CGROUP
587 flag also allows the creation of
588 frozen child processes by spawning them into a frozen cgroup.
591 for a description of the freezer controller.)
593 For threaded applications (or even thread implementations which
594 make use of cgroups to limit individual threads), it is possible to
595 establish a fixed cgroup layout before spawning each thread
596 directly into its target cgroup.
599 .BR CLONE_IO " (since Linux 2.6.25)"
602 is set, then the new process shares an I/O context with
604 If this flag is not set, then (as with
606 the new process has its own I/O context.
608 .\" The following based on text from Jens Axboe
609 The I/O context is the I/O scope of the disk scheduler (i.e.,
610 what the I/O scheduler uses to model scheduling of a process's I/O).
611 If processes share the same I/O context,
612 they are treated as one by the I/O scheduler.
613 As a consequence, they get to share disk time.
614 For some I/O schedulers,
615 .\" the anticipatory and CFQ scheduler
616 if two processes share an I/O context,
617 they will be allowed to interleave their disk access.
618 If several threads are doing I/O on behalf of the same process
620 for instance), they should employ
622 to get better I/O performance.
625 If the kernel is not configured with the
627 option, this flag is a no-op.
629 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
630 Create the process in a new cgroup namespace.
631 If this flag is not set, then (as with
633 the process is created in the same cgroup namespaces as the calling process.
635 For further information on cgroup namespaces, see
636 .BR cgroup_namespaces (7).
638 Only a privileged process
639 .RB ( CAP_SYS_ADMIN )
641 .BR CLONE_NEWCGROUP .
644 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
647 is set, then create the process in a new IPC namespace.
648 If this flag is not set, then (as with
650 the process is created in the same IPC namespace as
653 For further information on IPC namespaces, see
654 .BR ipc_namespaces (7).
656 Only a privileged process
657 .RB ( CAP_SYS_ADMIN )
660 This flag can't be specified in conjunction with
663 .BR CLONE_NEWNET " (since Linux 2.6.24)"
664 (The implementation of this flag was completed only
665 by about kernel version 2.6.29.)
669 is set, then create the process in a new network namespace.
670 If this flag is not set, then (as with
672 the process is created in the same network namespace as
675 For further information on network namespaces, see
676 .BR network_namespaces (7).
678 Only a privileged process
679 .RB ( CAP_SYS_ADMIN )
683 .BR CLONE_NEWNS " (since Linux 2.4.19)"
686 is set, the cloned child is started in a new mount namespace,
687 initialized with a copy of the namespace of the parent.
690 is not set, the child lives in the same mount
691 namespace as the parent.
693 For further information on mount namespaces, see
696 .BR mount_namespaces (7).
698 Only a privileged process
699 .RB ( CAP_SYS_ADMIN )
702 It is not permitted to specify both
706 .\" See https://lwn.net/Articles/543273/
707 in the same clone call.
709 .BR CLONE_NEWPID " (since Linux 2.6.24)"
710 .\" This explanation draws a lot of details from
711 .\" http://lwn.net/Articles/259217/
712 .\" Authors: Pavel Emelyanov <xemul@openvz.org>
713 .\" and Kir Kolyshkin <kir@openvz.org>
715 .\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
716 .\" Author: Pavel Emelyanov <xemul@openvz.org>
719 is set, then create the process in a new PID namespace.
720 If this flag is not set, then (as with
722 the process is created in the same PID namespace as
725 For further information on PID namespaces, see
728 .BR pid_namespaces (7).
730 Only a privileged process
731 .RB ( CAP_SYS_ADMIN )
734 This flag can't be specified in conjunction with
740 (This flag first became meaningful for
745 semantics were merged in Linux 3.5,
746 and the final pieces to make the user namespaces completely usable were
747 merged in Linux 3.8.)
751 is set, then create the process in a new user namespace.
752 If this flag is not set, then (as with
754 the process is created in the same user namespace as the calling process.
756 For further information on user namespaces, see
759 .BR user_namespaces (7).
761 Before Linux 3.8, use of
763 required that the caller have three capabilities:
768 .\" Before Linux 2.6.29, it appears that only CAP_SYS_ADMIN was needed
769 Starting with Linux 3.8,
770 no privileges are needed to create a user namespace.
772 This flag can't be specified in conjunction with
776 For security reasons,
777 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
778 .\" https://lwn.net/Articles/543273/
779 .\" The fix actually went into 3.9 and into 3.8.3. However, user namespaces
780 .\" were, for practical purposes, unusable in earlier 3.8.x because of the
781 .\" various filesystems that didn't support userns.
783 cannot be specified in conjunction with
786 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
789 is set, then create the process in a new UTS namespace,
790 whose identifiers are initialized by duplicating the identifiers
791 from the UTS namespace of the calling process.
792 If this flag is not set, then (as with
794 the process is created in the same UTS namespace as
797 For further information on UTS namespaces, see
798 .BR uts_namespaces (7).
800 Only a privileged process
801 .RB ( CAP_SYS_ADMIN )
805 .BR CLONE_PARENT " (since Linux 2.3.12)"
808 is set, then the parent of the new child (as returned by
810 will be the same as that of the calling process.
814 is not set, then (as with
816 the child's parent is the calling process.
818 Note that it is the parent process, as returned by
820 which is signaled when the child terminates, so that
823 is set, then the parent of the calling process, rather than the
824 calling process itself, is signaled.
828 flag can't be used in clone calls by the
829 global init process (PID 1 in the initial PID namespace)
830 and init processes in other PID namespaces.
831 This restriction prevents the creation of multi-rooted process trees
832 as well as the creation of unreapable zombies in the initial PID namespace.
834 .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
835 Store the child thread ID at the location pointed to by
839 .I cl_args.parent_tid
841 in the parent's memory.
842 (In Linux 2.5.32-2.5.48 there was a flag
845 The store operation completes before the clone call
846 returns control to user space.
848 .BR CLONE_PID " (Linux 2.0 to 2.5.15)"
851 is set, the child process is created with the same process ID as
853 This is good for hacking the system, but otherwise
855 From Linux 2.3.21 onward, this flag could be
856 specified only by the system boot process (PID 0).
857 The flag disappeared completely from the kernel sources in Linux 2.5.16.
858 Subsequently, the kernel silently ignored this bit if it was specified in the
861 Much later, the same bit was recycled for use as the
865 .BR CLONE_PIDFD " (since Linux 5.2)"
866 .\" commit b3e5838252665ee4cfa76b82bdf1198dca81e5be
867 If this flag is specified,
868 a PID file descriptor referring to the child process is allocated
869 and placed at a specified location in the parent's memory.
870 The close-on-exec flag is set on this new file descriptor.
871 PID file descriptors can be used for the purposes described in
877 the PID file descriptor is placed at the location pointed to by
882 the PID file descriptor is placed at the location pointed to by
886 argument is used to return the PID file descriptor,
889 .B CLONE_PARENT_SETTID
894 It is currently not possible to use this flag together with
896 This means that the process identified by the PID file descriptor
897 will always be a thread group leader.
901 flag is specified alongside
905 an error is returned.
906 An error also results if
908 is specified when calling
910 This error behavior ensures that the bit corresponding to
912 can be reused for further PID file descriptor features in the future.
914 .BR CLONE_PTRACE " (since Linux 2.2)"
917 is specified, and the calling process is being traced,
918 then trace the child also (see
921 .BR CLONE_SETTLS " (since Linux 2.5.32)"
922 The TLS (Thread Local Storage) descriptor is set to
925 The interpretation of
927 and the resulting effect is architecture dependent.
931 .IR "struct user_desc\ *"
933 .BR set_thread_area (2)).
934 On x86-64 it is the new value to be set for the %fs base register
939 On architectures with a dedicated TLS register, it is the new value
942 Use of this flag requires detailed knowledge and generally it
943 should not be used except in libraries implementing threading.
945 .BR CLONE_SIGHAND " (since Linux 2.0)"
948 is set, the calling process and the child process share the same table of
950 If the calling process or child process calls
952 to change the behavior associated with a signal, the behavior is
953 changed in the other process as well.
954 However, the calling process and child
955 processes still have distinct signal masks and sets of pending
957 So, one of them may block or unblock signals using
959 without affecting the other process.
963 is not set, the child process inherits a copy of the signal handlers
964 of the calling process at the time of the clone call.
967 performed later by one of the processes have no effect on the other
971 .\" Precisely: Linux 2.6.0-test6
974 mask must also include
980 .BR CLONE_STOPPED " (since Linux 2.6.0)"
981 .\" Precisely: Linux 2.6.0-test2
984 is set, then the child is initially stopped (as though it was sent a
986 signal), and must be resumed by sending it a
992 from Linux 2.6.25 onward,
995 altogether in Linux 2.6.38.
996 Since then, the kernel silently ignores it without error.
997 .\" glibc 2.8 removed this defn from bits/sched.h
998 Starting with Linux 4.6, the same bit was reused for the
1002 .BR CLONE_SYSVSEM " (since Linux 2.5.10)"
1005 is set, then the child and the calling process share
1006 a single list of System V semaphore adjustment
1010 In this case, the shared list accumulates
1012 values across all processes sharing the list,
1013 and semaphore adjustments are performed only when the last process
1014 that is sharing the list terminates (or ceases sharing the list using
1016 If this flag is not set, then the child has a separate
1018 list that is initially empty.
1020 .BR CLONE_THREAD " (since Linux 2.4.0)"
1021 .\" Precisely: Linux 2.6.0-test8
1024 is set, the child is placed in the same thread group as the calling process.
1025 To make the remainder of the discussion of
1027 more readable, the term "thread" is used to refer to the
1028 processes within a thread group.
1030 Thread groups were a feature added in Linux 2.4 to support the
1031 POSIX threads notion of a set of threads that share a single PID.
1032 Internally, this shared PID is the so-called
1033 thread group identifier (TGID) for the thread group.
1034 Since Linux 2.4, calls to
1036 return the TGID of the caller.
1038 The threads within a group can be distinguished by their (system-wide)
1039 unique thread IDs (TID).
1040 A new thread's TID is available as the function result
1041 returned to the caller,
1042 and a thread can obtain
1046 When a clone call is made without specifying
1048 then the resulting thread is placed in a new thread group
1049 whose TGID is the same as the thread's TID.
1052 of the new thread group.
1054 A new thread created with
1056 has the same parent process as the process that made the clone call
1061 return the same value for all of the threads in a thread group.
1064 thread terminates, the thread that created it is not sent a
1066 (or other termination) signal;
1067 nor can the status of such a thread be obtained
1070 (The thread is said to be
1073 After all of the threads in a thread group terminate
1074 the parent process of the thread group is sent a
1076 (or other termination) signal.
1078 If any of the threads in a thread group performs an
1080 then all threads other than the thread group leader are terminated,
1081 and the new program is executed in the thread group leader.
1083 If one of the threads in a thread group creates a child using
1085 then any thread in the group can
1089 Since Linux 2.5.35, the
1091 mask must also include
1096 (and note that, since Linux 2.6.0,
1097 .\" Precisely: Linux 2.6.0-test6
1103 Signal dispositions and actions are process-wide:
1104 if an unhandled signal is delivered to a thread, then
1105 it will affect (terminate, stop, continue, be ignored in)
1106 all members of the thread group.
1108 Each thread has its own signal mask, as set by
1109 .BR sigprocmask (2).
1111 A signal may be process-directed or thread-directed.
1112 A process-directed signal is targeted at a thread group (i.e., a TGID),
1113 and is delivered to an arbitrarily selected thread from among those
1114 that are not blocking the signal.
1115 A signal may be process-directed because it was generated by the kernel
1116 for reasons other than a hardware exception, or because it was sent using
1120 A thread-directed signal is targeted at (i.e., delivered to)
1122 A signal may be thread directed because it was sent using
1125 .BR pthread_sigqueue (3),
1126 or because the thread executed a machine language instruction that triggered
1127 a hardware exception
1128 (e.g., invalid memory access triggering
1130 or a floating-point exception triggering
1135 returns a signal set that is the union of the pending process-directed
1136 signals and the signals that are pending for the calling thread.
1138 If a process-directed signal is delivered to a thread group,
1139 and the thread group has installed a handler for the signal, then
1140 the handler is invoked in exactly one, arbitrarily selected
1141 member of the thread group that has not blocked the signal.
1142 If multiple threads in a group are waiting to accept the same signal using
1143 .BR sigwaitinfo (2),
1144 the kernel will arbitrarily select one of these threads
1145 to receive the signal.
1147 .BR CLONE_UNTRACED " (since Linux 2.5.46)"
1150 is specified, then a tracing process cannot force
1152 on this child process.
1154 .BR CLONE_VFORK " (since Linux 2.2)"
1157 is set, the execution of the calling process is suspended
1158 until the child releases its virtual memory
1159 resources via a call to
1168 is not set, then both the calling process and the child are schedulable
1169 after the call, and an application should not rely on execution occurring
1170 in any particular order.
1172 .BR CLONE_VM " (since Linux 2.0)"
1175 is set, the calling process and the child process run in the same memory
1177 In particular, memory writes performed by the calling process
1178 or by the child process are also visible in the other process.
1179 Moreover, any memory mapping or unmapping performed with
1183 by the child or calling process also affects the other process.
1187 is not set, the child process runs in a separate copy of the memory
1188 space of the calling process at the time of the clone call.
1189 Memory writes or file mappings/unmappings performed by one of the
1190 processes do not affect the other, as with
1195 flag is specified and the
1197 flag is not specified,
1198 then any alternate signal stack that was established by
1200 is cleared in the child process.
1202 .\" gettid(2) returns current->pid;
1203 .\" getpid(2) returns current->tgid;
1204 On success, the thread ID of the child process is returned
1205 in the caller's thread of execution.
1206 On failure, \-1 is returned
1207 in the caller's context, no child process is created, and
1209 is set to indicate the error.
1213 Too many processes are already running; see
1216 .BR EBUSY " (" clone3 "() only)"
1217 .B CLONE_INTO_CGROUP
1220 but the file descriptor specified in
1222 refers to a version 2 cgroup in which a domain controller is enabled.
1224 .BR EEXIST " (" clone3 "() only)"
1225 One (or more) of the PIDs specified in
1227 already exists in the corresponding PID namespace.
1233 .B CLONE_CLEAR_SIGHAND
1234 were specified in the
1240 was specified in the
1245 (Since Linux 2.6.0.)
1246 .\" Precisely: Linux 2.6.0-test6
1250 was specified in the
1255 (Since Linux 2.5.35.)
1258 .\" Precisely one of
1259 .\" .B CLONE_DETACHED
1263 .\" (Since Linux 2.6.0-test6.)
1267 was specified in the
1269 mask, but the current process previously called
1275 to reassociate itself with a PID namespace.
1278 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
1283 were specified in the
1287 .BR EINVAL " (since Linux 3.9)"
1292 were specified in the
1301 were specified in the
1310 and one (or both) of
1314 were specified in the
1318 .BR EINVAL " (since Linux 2.6.32)"
1319 .\" commit 123be07b0b399670a7cc3d82fef0cb4f93ef885c
1321 was specified, and the caller is an init process.
1324 Returned by the glibc
1326 wrapper function when
1330 is specified as NULL.
1334 was specified in the
1337 but the kernel was not configured with the
1345 was specified in the
1348 but the kernel was not configured with the
1354 was specified in the
1357 but the kernel was not configured with the
1363 was specified in the
1366 but the kernel was not configured with the
1372 was specified in the
1375 but the kernel was not configured with the
1381 is not aligned to a suitable boundary for this architecture.
1382 For example, on aarch64,
1384 must be a multiple of 16.
1386 .BR EINVAL " (" clone3 "() only)"
1388 was specified in the
1392 .BR EINVAL " (" clone "() only)"
1394 was specified together with
1402 was specified together with
1408 .BR "EINVAL " "(" clone "() only)"
1410 was specified together with
1411 .B CLONE_PARENT_SETTID
1416 .BR EINVAL " (" clone3 "() only)"
1418 is greater than the number of nested PID namespaces.
1420 .BR EINVAL " (" clone3 "() only)"
1421 One of the PIDs specified in
1425 .BR EINVAL " (AArch64 only, Linux 4.6 and earlier)"
1427 was not aligned to a 128-bit boundary.
1430 Cannot allocate sufficient memory to allocate a task structure for the
1431 child, or to copy those parts of the caller's context that need to be
1434 .BR ENOSPC " (since Linux 3.7)"
1435 .\" commit f2302505775fd13ba93f034206f1e2a587017929
1437 was specified in the
1440 but the limit on the nesting depth of PID namespaces
1441 would have been exceeded; see
1442 .BR pid_namespaces (7).
1444 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
1446 was specified in the
1448 mask, and the call would cause the limit on the number of
1449 nested user namespaces to be exceeded.
1451 .BR user_namespaces (7).
1453 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
1456 .BR ENOSPC " (since Linux 4.9)"
1457 One of the values in the
1459 mask specified the creation of a new user namespace,
1460 but doing so would have caused the limit defined by the corresponding file in
1463 For further details, see
1466 .BR EOPNOTSUPP " (" clone3 "() only)"
1467 .B CLONE_INTO_CGROUP
1470 but the file descriptor specified in
1472 refers to a version 2 cgroup that is in the
1473 .IR "domain invalid"
1477 .BR CLONE_NEWCGROUP ,
1484 was specified by an unprivileged process (process without \fBCAP_SYS_ADMIN\fP).
1488 was specified by a process other than process 0.
1489 (This error occurs only on Linux 2.5.15 and earlier.)
1493 was specified in the
1496 but either the effective user ID or the effective group ID of the caller
1497 does not have a mapping in the parent namespace (see
1498 .BR user_namespaces (7)).
1500 .BR EPERM " (since Linux 3.9)"
1501 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
1503 was specified in the
1505 mask and the caller is in a chroot environment
1506 .\" FIXME What is the rationale for this restriction?
1507 (i.e., the caller's root directory does not match the root directory
1508 of the mount namespace in which it resides).
1510 .BR EPERM " (" clone3 "() only)"
1512 was greater than zero, and the caller lacks the
1514 capability in one or more of the user namespaces that own the
1515 corresponding PID namespaces.
1517 .BR ERESTARTNOINTR " (since Linux 2.6.17)"
1518 .\" commit 4a2c7a7837da1b91468e50426066d988050e4d56
1519 System call was interrupted by a signal and will be restarted.
1520 (This can be seen only during a trace.)
1522 .BR EUSERS " (Linux 3.11 to Linux 4.8)"
1524 was specified in the
1527 and the limit on the number of nested user namespaces would be exceeded.
1528 See the discussion of the
1534 system call first appeared in Linux 5.3.
1535 .\" There is no entry for
1540 .\" as described in this manual page.
1543 are Linux-specific and should not be used in programs
1544 intended to be portable.
1546 One use of these systems calls
1547 is to implement threads: multiple flows of control in a program that
1548 run concurrently in a shared address space.
1552 wrapper function makes some changes
1553 in the memory pointed to by
1555 (changes required to set the stack up correctly for the child)
1562 is used to recursively create children,
1563 do not use the buffer employed for the parent's stack
1564 as the stack of the child.
1568 system call can be used to test whether two processes share various
1569 resources such as a file descriptor table,
1570 System V semaphore undo operations, or a virtual address space.
1572 Handlers registered using
1573 .BR pthread_atfork (3)
1574 are not executed during a clone call.
1576 In the Linux 2.4.x series,
1578 generally does not make the parent of the new thread the same
1579 as the parent of the calling process.
1580 However, for kernel versions 2.4.7 to 2.4.18 the
1584 flag (as in Linux 2.6.0 and later).
1588 should not be called through vsyscall, but directly through
1591 .SS C library/kernel differences
1594 system call corresponds more closely to
1596 in that execution in the child continues from the point of the
1604 wrapper function are omitted.
1606 In contrast to the glibc wrapper, the raw
1608 system call accepts NULL as a
1615 In this case, the child uses a duplicate of the parent's stack.
1616 (Copy-on-write semantics ensure that the child gets separate copies
1617 of stack pages when either process modifies the stack.)
1618 In this case, for correct operation, the
1620 option should not be specified.
1623 the parent's memory because of the use of the
1626 then no copy-on-write duplication occurs and chaos is likely to result.)
1628 The order of the arguments also differs in the raw system call,
1629 and there are variations in the arguments across architectures,
1630 as detailed in the following paragraphs.
1632 The raw system call interface on x86-64 and some other architectures
1633 (including sh, tile, and alpha) is:
1637 .BI "long clone(unsigned long " flags ", void *" stack ,
1638 .BI " int *" parent_tid ", int *" child_tid ,
1639 .BI " unsigned long " tls );
1643 On x86-32, and several other common architectures
1644 (including score, ARM, ARM 64, PA-RISC, arc, Power PC, xtensa,
1646 .\" CONFIG_CLONE_BACKWARDS
1647 the order of the last two arguments is reversed:
1651 .BI "long clone(unsigned long " flags ", void *" stack ,
1652 .BI " int *" parent_tid ", unsigned long " tls ,
1653 .BI " int *" child_tid );
1657 On the cris and s390 architectures,
1658 .\" CONFIG_CLONE_BACKWARDS2
1659 the order of the first two arguments is reversed:
1663 .BI "long clone(void *" stack ", unsigned long " flags ,
1664 .BI " int *" parent_tid ", int *" child_tid ,
1665 .BI " unsigned long " tls );
1669 On the microblaze architecture,
1670 .\" CONFIG_CLONE_BACKWARDS3
1671 an additional argument is supplied:
1675 .BI "long clone(unsigned long " flags ", void *" stack ,
1676 .BI " int " stack_size , "\fR /* Size of stack */"
1677 .BI " int *" parent_tid ", int *" child_tid ,
1678 .BI " unsigned long " tls );
1682 .SS blackfin, m68k, and sparc
1683 .\" Mike Frysinger noted in a 2013 mail:
1684 .\" these arches don't define __ARCH_WANT_SYS_CLONE:
1685 .\" blackfin ia64 m68k sparc
1686 The argument-passing conventions on
1687 blackfin, m68k, and sparc are different from the descriptions above.
1688 For details, see the kernel (and glibc) source.
1690 On ia64, a different interface is used:
1694 .BI "int __clone2(int (*" "fn" ")(void *),"
1695 .BI " void *" stack_base ", size_t " stack_size ,
1696 .BI " int " flags ", void *" "arg" ", ..."
1697 .BI " /* pid_t *" parent_tid ", struct user_desc *" tls ,
1698 .BI " pid_t *" child_tid " */ );"
1702 The prototype shown above is for the glibc wrapper function;
1703 for the system call itself,
1704 the prototype can be described as follows (it is identical to the
1706 prototype on microblaze):
1710 .BI "long clone2(unsigned long " flags ", void *" stack_base ,
1711 .BI " int " stack_size , "\fR /* Size of stack */"
1712 .BI " int *" parent_tid ", int *" child_tid ,
1713 .BI " unsigned long " tls );
1718 operates in the same way as
1722 points to the lowest address of the child's stack area,
1725 specifies the size of the stack pointed to by
1727 .SS Linux 2.4 and earlier
1728 In Linux 2.4 and earlier,
1730 does not take arguments
1736 GNU C library versions 2.3.4 up to and including 2.24
1737 contained a wrapper function for
1739 that performed caching of PIDs.
1740 This caching relied on support in the glibc wrapper for
1742 but limitations in the implementation
1743 meant that the cache was not up to date in some circumstances.
1745 if a signal was delivered to the child immediately after the
1747 call, then a call to
1749 in a handler for the signal could return the PID
1750 of the calling process ("the parent"),
1751 if the clone wrapper had not yet had a chance to update the PID
1753 (This discussion ignores the case where the child was created using
1758 return the same value in the child and in the process that called
1760 since the caller and the child are in the same thread group.
1761 The stale-cache problem also does not occur if the
1765 To get the truth, it was sometimes necessary to use code such as the following:
1769 #include <syscall.h>
1773 mypid = syscall(SYS_getpid);
1776 .\" See also the following bug reports
1777 .\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1778 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
1780 Because of the stale-cache problem, as well as other problems noted in
1782 the PID caching feature was removed in glibc 2.25.
1784 The following program demonstrates the use of
1786 to create a child process that executes in a separate UTS namespace.
1787 The child changes the hostname in its UTS namespace.
1788 Both parent and child then display the system hostname,
1789 making it possible to see that the hostname
1790 differs in the UTS namespaces of the parent and child.
1791 For an example of the use of this program, see
1794 Within the sample program, we allocate the memory that is to
1795 be used for the child's stack using
1799 for the following reasons:
1802 allocates a block of memory that starts on a page
1803 boundary and is a multiple of the page size.
1804 This is useful if we want to establish a guard page (a page with protection
1806 at the end of the stack using
1811 flag to request a mapping that is suitable for a stack.
1812 For the moment, this flag is a no-op on Linux,
1813 but it exists and has effect on some other systems,
1814 so we should include it for portability.
1818 #include <sys/wait.h>
1819 #include <sys/utsname.h>
1826 #include <sys/mman.h>
1828 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
1831 static int /* Start function for cloned child */
1832 childFunc(void *arg)
1836 /* Change hostname in UTS namespace of child. */
1838 if (sethostname(arg, strlen(arg)) == \-1)
1839 errExit("sethostname");
1841 /* Retrieve and display hostname. */
1843 if (uname(&uts) == \-1)
1845 printf("uts.nodename in child: %s\en", uts.nodename);
1847 /* Keep the namespace open for a while, by sleeping.
1848 This allows some experimentation\-\-for example, another
1849 process might join the namespace. */
1853 return 0; /* Child terminates now */
1856 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
1859 main(int argc, char *argv[])
1861 char *stack; /* Start of stack buffer */
1862 char *stackTop; /* End of stack buffer */
1867 fprintf(stderr, "Usage: %s <child\-hostname>\en", argv[0]);
1871 /* Allocate memory to be used for the stack of the child. */
1873 stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
1874 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, \-1, 0);
1875 if (stack == MAP_FAILED)
1878 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
1880 /* Create child that has its own UTS namespace;
1881 child commences execution in childFunc(). */
1883 pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]);
1886 printf("clone() returned %jd\en", (intmax_t) pid);
1888 /* Parent falls through to here */
1890 sleep(1); /* Give child time to change its hostname */
1892 /* Display hostname in parent\(aqs UTS namespace. This will be
1893 different from hostname in child\(aqs UTS namespace. */
1895 if (uname(&uts) == \-1)
1897 printf("uts.nodename in parent: %s\en", uts.nodename);
1899 if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */
1901 printf("child has terminated\en");
1914 .BR set_thread_area (2),
1915 .BR set_tid_address (2),
1920 .BR capabilities (7),