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