]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/clone.2
clone.2: Changes after review by Serge Hallyn
[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.\"
732e54dd 45.TH CLONE 2 2008-11-20 "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
317Use of this flag requires: a kernel configured with the
318.B CONFIG_NET_NS
319option and that the process be privileged
cae2ec15 320.RB ( CAP_SYS_ADMIN ).
163bf178 321.TP
c10859eb 322.BR CLONE_NEWNS " (since Linux 2.4.19)"
732e54dd 323Start the child in a new mount namespace.
fea681da 324
732e54dd 325Every process lives in a mount namespace.
c13182ef 326The
fea681da
MK
327.I namespace
328of a process is the data (the set of mounts) describing the file hierarchy
c13182ef
MK
329as seen by that process.
330After a
fea681da
MK
331.BR fork (2)
332or
2777b1ca 333.BR clone ()
fea681da
MK
334where the
335.B CLONE_NEWNS
732e54dd 336flag is not set, the child lives in the same mount
4df2eb09 337namespace as the parent.
fea681da
MK
338The system calls
339.BR mount (2)
340and
341.BR umount (2)
732e54dd 342change the mount namespace of the calling process, and hence affect
fea681da 343all processes that live in the same namespace, but do not affect
732e54dd 344processes in a different mount namespace.
fea681da
MK
345
346After a
2777b1ca 347.BR clone ()
fea681da
MK
348where the
349.B CLONE_NEWNS
732e54dd 350flag is set, the cloned child is started in a new mount namespace,
fea681da
MK
351initialized with a copy of the namespace of the parent.
352
0b9bdf82 353Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability)
fea681da
MK
354may specify the
355.B CLONE_NEWNS
356flag.
357It is not permitted to specify both
358.B CLONE_NEWNS
359and
360.B CLONE_FS
361in the same
e511ffb6 362.BR clone ()
fea681da 363call.
fea681da 364.TP
82ee147a
MK
365.BR CLONE_NEWPID " (since Linux 2.6.24)"
366.\" This explanation draws a lot of details from
367.\" http://lwn.net/Articles/259217/
368.\" Authors: Pavel Emelyanov <xemul@openvz.org>
369.\" and Kir Kolyshkin <kir@openvz.org>
370.\"
371.\" The primary kernel commit is 30e49c263e36341b60b735cbef5ca37912549264
372.\" Author: Pavel Emelyanov <xemul@openvz.org>
373If
5c95e5e8 374.B CLONE_NEWPID
82ee147a
MK
375is set, then create the process in a new PID namespace.
376If this flag is not set, then (as with
377.BR fork (2)),
378the process is created in the same PID namespace as
379the calling process.
0236bea9 380This flag is intended for the implementation of containers.
82ee147a
MK
381
382A PID namespace provides an isolated environment for PIDs:
383PIDs in a new namespace start at 1,
384somewhat like a standalone system, and calls to
385.BR fork (2),
386.BR vfork (2),
387or
388.BR clone (2)
389will produce processes whose PIDs within the namespace
390are only guaranteed to be unique within that namespace.
391
392The first process created in a new namespace
393(i.e., the process created using the
394.BR CLONE_NEWPID
395flag) has the PID 1, and is the "init" process for the namespace.
396Children that are orphaned within the namespace will be reparented
397to this process rather than
398.BR init (8).
399Unlike the traditional
400.B init
401process, the "init" process of a PID namespace can terminate,
402and if it does, all of the processes in the namespace are terminated.
403
404PID namespaces form a hierarchy.
405When a PID new namespace is created,
406the PIDs of the processes in that namespace are visible
407in the PID namespace of the process that created the new namespace;
408analogously, if the parent PID namespace is itself
409the child of another PID namespace,
410then PIDs of the child and parent PID namespaces will both be
411visible in the grandparent PID namespace.
412Conversely, the processes in the "child" PID namespace do not see
413the PIDs of the processes in the parent namespace.
414The existence of a namespace hierarchy means that each process
415may now have multiple PIDs:
416one for each namespace in which it is visible.
417(A call to
418.BR getpid (2)
419always returns the PID associated with the namespace in which
420the process was created.)
421
422After creating the new namespace,
423it is useful for the child to change its root directory
424and mount a new procfs instance at
425.I /proc
426so that tools such as
427.BR ps (1)
428work correctly.
429.\" mount -t proc proc /proc
430
431Use of this flag requires: a kernel configured with the
432.B CONFIG_PID_NS
c8e18bd1 433option and that the process be privileged
af3c728e 434.RB ( CAP_SYS_ADMIN ).
82ee147a
MK
435This flag can't be specified in conjunction with
436.BR CLONE_THREAD .
437.TP
43ce9dda
MK
438.BR CLONE_NEWUTS " (since Linux 2.6.19)"
439If
440.B CLONE_NEWUTS
e1b11906
MK
441is set, then create the process in a new UTS namespace,
442whose identifiers are initialized by duplicating the identifiers
443from the UTS namespace of the calling process.
43ce9dda
MK
444If this flag is not set, then (as with
445.BR fork (2)),
446the process is created in the same UTS namespace as
447the calling process.
0236bea9 448This flag is intended for the implementation of containers.
43ce9dda
MK
449
450A UTS namespace is the set of identifiers returned by
451.BR uname (2);
452among these, the domain name and the host name can be modified by
453.BR setdomainname (2)
454and
455.BR
456.BR sethostname (2),
457respectively.
458Changes made to these identifiers in one UTS namespace
459are visible to other processes in the same namespace,
460but are not visible to processes in other UTS namespaces.
461
462Use of this flag requires: a kernel configured with the
463.B CONFIG_UTS_NS
c8e18bd1 464option and that the process be privileged
43ce9dda
MK
465.RB ( CAP_SYS_ADMIN ).
466.TP
f5dbc7c8
MK
467.BR CLONE_PARENT " (since Linux 2.3.12)"
468If
469.B CLONE_PARENT
470is set, then the parent of the new child (as returned by
471.BR getppid (2))
472will be the same as that of the calling process.
473
474If
475.B CLONE_PARENT
476is not set, then (as with
477.BR fork (2))
478the child's parent is the calling process.
479
480Note that it is the parent process, as returned by
481.BR getppid (2),
482which is signaled when the child terminates, so that
483if
484.B CLONE_PARENT
485is set, then the parent of the calling process, rather than the
486calling process itself, will be signaled.
487.TP
488.BR CLONE_PARENT_SETTID " (since Linux 2.5.49)"
489Store child thread ID at location
490.I parent_tidptr
491in parent and child memory.
492(In Linux 2.5.32-2.5.48 there was a flag
493.B CLONE_SETTID
494that did this.)
495.TP
496.BR CLONE_PID " (obsolete)"
497If
498.B CLONE_PID
499is set, the child process is created with the same process ID as
500the calling process.
501This is good for hacking the system, but otherwise
502of not much use.
503Since 2.3.21 this flag can be
504specified only by the system boot process (PID 0).
505It disappeared in Linux 2.5.16.
506.TP
507.B CLONE_PTRACE
508If
509.B CLONE_PTRACE
510is specified, and the calling process is being traced,
511then trace the child also (see
512.BR ptrace (2)).
513.TP
514.BR CLONE_SETTLS " (since Linux 2.5.32)"
515The
516.I newtls
517argument is the new TLS (Thread Local Storage) descriptor.
518(See
519.BR set_thread_area (2).)
520.TP
fea681da
MK
521.B CLONE_SIGHAND
522If
523.B CLONE_SIGHAND
314c8ff4 524is set, the calling process and the child process share the same table of
c13182ef
MK
525signal handlers.
526If the calling process or child process calls
fea681da 527.BR sigaction (2)
c13182ef
MK
528to change the behavior associated with a signal, the behavior is
529changed in the other process as well.
530However, the calling process and child
fea681da 531processes still have distinct signal masks and sets of pending
c13182ef
MK
532signals.
533So, one of them may block or unblock some signals using
fea681da
MK
534.BR sigprocmask (2)
535without affecting the other process.
536
537If
538.B CLONE_SIGHAND
539is not set, the child process inherits a copy of the signal handlers
540of the calling process at the time
edcc65ff 541.BR clone ()
c13182ef
MK
542is called.
543Calls to
fea681da
MK
544.BR sigaction (2)
545performed later by one of the processes have no effect on the other
546process.
29546c24
MK
547
548Since Linux 2.6.0-test6,
549.I flags
550must also include
551.B CLONE_VM
552if
553.B CLONE_SIGHAND
554is specified
fea681da 555.TP
a69b6bda
MK
556.BR CLONE_STOPPED " (since Linux 2.6.0-test2)"
557If
558.B CLONE_STOPPED
559is set, then the child is initially stopped (as though it was sent a
560.B SIGSTOP
561signal), and must be resumed by sending it a
562.B SIGCONT
563signal.
ef37eaf2
MK
564
565.I "From Linux 2.6.25 this flag is deprecated."
566You probably never wanted to use it,
53c94269 567you certainly shouldn't be using it, and soon it will go away.
a5a061ee 568.\" glibc 2.8 removed this defn from bits/sched.h
a69b6bda 569.TP
f5dbc7c8 570.BR CLONE_SYSVSEM " (since Linux 2.5.10)"
fea681da 571If
f5dbc7c8
MK
572.B CLONE_SYSVSEM
573is set, then the child and the calling process share
574a single list of System V semaphore undo values (see
575.BR semop (2)).
576If this flag is not set, then the child has a separate undo list,
577which is initially empty.
fea681da
MK
578.TP
579.BR CLONE_THREAD " (since Linux 2.4.0-test8)"
580If
581.B CLONE_THREAD
582is set, the child is placed in the same thread group as the calling process.
fd8a5be4
MK
583To make the remainder of the discussion of
584.B CLONE_THREAD
585more readable, the term "thread" is used to refer to the
586processes within a thread group.
fea681da 587
fd8a5be4
MK
588Thread groups were a feature added in Linux 2.4 to support the
589POSIX threads notion of a set of threads that share a single PID.
590Internally, this shared PID is the so-called
591thread group identifier (TGID) for the thread group.
c13182ef 592Since Linux 2.4, calls to
fea681da 593.BR getpid (2)
fd8a5be4
MK
594return the TGID of the caller.
595
596The threads within a group can be distinguished by their (system-wide)
597unique thread IDs (TID).
598A new thread's TID is available as the function result
599returned to the caller of
600.BR clone (),
601and a thread can obtain
602its own TID using
603.BR gettid (2).
604
c13182ef 605When a call is made to
fd8a5be4
MK
606.BR clone ()
607without specifying
608.BR CLONE_THREAD ,
609then the resulting thread is placed in a new thread group
610whose TGID is the same as the thread's TID.
611This thread is the
612.I leader
613of the new thread group.
614
615A new thread created with
616.B CLONE_THREAD
617has the same parent process as the caller of
618.BR clone ()
c13182ef 619(i.e., like
fd8a5be4
MK
620.BR CLONE_PARENT ),
621so that calls to
622.BR getppid (2)
623return the same value for all of the threads in a thread group.
624When a
c13182ef 625.B CLONE_THREAD
fd8a5be4
MK
626thread terminates, the thread that created it using
627.BR clone ()
628is not sent a
629.B SIGCHLD
630(or other termination) signal;
631nor can the status of such a thread be obtained
632using
633.BR wait (2).
634(The thread is said to be
635.IR detached .)
636
e2fbf61d
MK
637After all of the threads in a thread group terminate
638the parent process of the thread group is sent a
fd8a5be4
MK
639.B SIGCHLD
640(or other termination) signal.
641
642If any of the threads in a thread group performs an
643.BR execve (2),
644then all threads other than the thread group leader are terminated,
645and the new program is executed in the thread group leader.
646
f7110f60
MK
647If one of the threads in a thread group creates a child using
648.BR fork (2),
649then any thread in the group can
650.BR wait (2)
651for that child.
652
edcc65ff 653Since Linux 2.5.35,
fd8a5be4
MK
654.I flags
655must also include
656.B CLONE_SIGHAND
657if
658.B CLONE_THREAD
659is specified.
e2fbf61d
MK
660
661Signals may be sent to a thread group as a whole (i.e., a TGID) using
662.BR kill (2),
663or to a specific thread (i.e., TID) using
664.BR tgkill (2).
665
666Signal dispositions and actions are process-wide:
667if an unhandled signal is delivered to a thread, then
668it will affect (terminate, stop, continue, be ignored in)
669all members of the thread group.
670
99408a60 671Each thread has its own signal mask, as set by
e2fbf61d 672.BR sigprocmask (2),
82a06020 673but signals can be pending either: for the whole process
e2fbf61d
MK
674(i.e., deliverable to any member of the thread group),
675when sent with
82a06020 676.BR kill (2);
e2fbf61d
MK
677or for an individual thread, when sent with
678.BR tgkill (2).
99408a60
MK
679A call to
680.BR sigpending (2)
681returns a signal set that is the union of the signals pending for the
682whole process and the signals that are pending for the calling thread.
e2fbf61d 683
c13182ef 684If
e2fbf61d
MK
685.BR kill (2)
686is used to send a signal to a thread group,
687and the thread group has installed a handler for the signal, then
688the handler will be invoked in exactly one, arbitrarily selected
689member of the thread group that has not blocked the signal.
c13182ef 690If multiple threads in a group are waiting to accept the same signal using
e2fbf61d
MK
691.BR sigwaitinfo (2),
692the kernel will arbitrarily select one of these threads
c13182ef 693to receive a signal sent using
e2fbf61d 694.BR kill (2).
a69b6bda 695.TP
f5dbc7c8 696.BR CLONE_UNTRACED " (since Linux 2.5.46)"
a69b6bda 697If
f5dbc7c8
MK
698.B CLONE_UNTRACED
699is specified, then a tracing process cannot force
700.B CLONE_PTRACE
701on this child process.
fea681da 702.TP
f5dbc7c8
MK
703.B CLONE_VFORK
704If
705.B CLONE_VFORK
706is set, the execution of the calling process is suspended
707until the child releases its virtual memory
708resources via a call to
709.BR execve (2)
710or
711.BR _exit (2)
712(as with
713.BR vfork (2)).
714
715If
716.B CLONE_VFORK
717is not set then both the calling process and the child are schedulable
718after the call, and an application should not rely on execution occurring
719in any particular order.
fea681da 720.TP
f5dbc7c8
MK
721.B CLONE_VM
722If
723.B CLONE_VM
724is set, the calling process and the child process run in the same memory
725space.
726In particular, memory writes performed by the calling process
727or by the child process are also visible in the other process.
728Moreover, any memory mapping or unmapping performed with
729.BR mmap (2)
730or
731.BR munmap (2)
732by the child or calling process also affects the other process.
733
734If
735.B CLONE_VM
736is not set, the child process runs in a separate copy of the memory
737space of the calling process at the time of
738.BR clone ().
739Memory writes or file mappings/unmappings performed by one of the
740processes do not affect the other, as with
741.BR fork (2).
fea681da
MK
742.SS "sys_clone"
743The
744.B sys_clone
745system call corresponds more closely to
746.BR fork (2)
747in that execution in the child continues from the point of the
c13182ef
MK
748call.
749Thus,
fea681da
MK
750.B sys_clone
751only requires the
752.I flags
c13182ef 753and
fea681da 754.I child_stack
c13182ef
MK
755arguments, which have the same meaning as for
756.BR clone ().
fea681da 757(Note that the order of these arguments differs from
c13182ef 758.BR clone ().)
fea681da 759
c13182ef 760Another difference for
fea681da
MK
761.B sys_clone
762is that the
763.I child_stack
c13182ef 764argument may be zero, in which case copy-on-write semantics ensure that the
fea681da 765child gets separate copies of stack pages when either process modifies
c13182ef
MK
766the stack.
767In this case, for correct operation, the
fea681da
MK
768.B CLONE_VM
769option should not be specified.
770
c4bb193f
MK
771Since Linux 2.5.49 the system call has five arguments.
772The two new arguments are
fea681da
MK
773.I parent_tidptr
774which points to the location (in parent and child memory) where
682edefb
MK
775the child thread ID will be written in case
776.B CLONE_PARENT_SETTID
fea681da
MK
777was specified, and
778.I child_tidptr
779which points to the location (in child memory) where the child thread ID
682edefb
MK
780will be written in case
781.B CLONE_CHILD_SETTID
782was specified.
fea681da 783.SH "RETURN VALUE"
0bfa087b
MK
784.\" gettid(2) returns current->pid;
785.\" getpid(2) returns current->tgid;
fea681da 786On success, the thread ID of the child process is returned
c13182ef 787in the caller's thread of execution.
84811e86 788On failure, \-1 is returned
fea681da
MK
789in the caller's context, no child process will be created, and
790.I errno
791will be set appropriately.
fea681da
MK
792.SH ERRORS
793.TP
794.B EAGAIN
795Too many processes are already running.
796.TP
797.B EINVAL
798.B CLONE_SIGHAND
799was specified, but
800.B CLONE_VM
2e8a7fb3
MK
801was not.
802(Since Linux 2.6.0-test6.)
fea681da
MK
803.TP
804.B EINVAL
805.B CLONE_THREAD
806was specified, but
807.B CLONE_SIGHAND
6387216b
MK
808was not.
809(Since Linux 2.5.35.)
29546c24
MK
810.\" .TP
811.\" .B EINVAL
812.\" Precisely one of
813.\" .B CLONE_DETACHED
814.\" and
815.\" .B CLONE_THREAD
6387216b
MK
816.\" was specified.
817.\" (Since Linux 2.6.0-test6.)
fea681da
MK
818.TP
819.B EINVAL
820Both
821.B CLONE_FS
822and
823.B CLONE_NEWNS
824were specified in
825.IR flags .
826.TP
827.B EINVAL
82ee147a 828Both
667417b3
MK
829.B CLONE_NEWIPC
830and
831.B CLONE_SYSVSEM
832were specified in
833.IR flags .
834.TP
835.B EINVAL
836Both
82ee147a
MK
837.BR CLONE_NEWPID
838and
839.BR CLONE_THREAD
840were specified in
841.IR flags .
842.TP
843.B EINVAL
c13182ef 844Returned by
edcc65ff 845.BR clone ()
c13182ef 846when a zero value is specified for
fea681da
MK
847.IR child_stack .
848.TP
28cad2c1 849.B EINVAL
667417b3
MK
850.BR CLONE_NEWIPC
851was specified in
852.IR flags ,
853but the kernel was not configured with the
854.B CONFIG_SYSVIPC
855and
856.BR CONFIG_IPC_NS
857options.
858.TP
859.B EINVAL
163bf178
MK
860.BR CLONE_NEWNET
861was specified in
862.IR flags ,
863but the kernel was not configured with the
864.B CONFIG_NET_NS
865option.
866.TP
867.B EINVAL
28cad2c1
MK
868.BR CLONE_NEWPID
869was specified in
870.IR flags ,
871but the kernel was not configured with the
872.B CONFIG_PID_NS
873option.
874.TP
43ce9dda
MK
875.B EINVAL
876.BR CLONE_NEWUTS
877was specified in
878.IR flags ,
879but the kernel was not configured with the
880.B CONFIG_UTS
881option.
882.TP
fea681da
MK
883.B ENOMEM
884Cannot allocate sufficient memory to allocate a task structure for the
885child, or to copy those parts of the caller's context that need to be
886copied.
887.TP
888.B EPERM
667417b3 889.BR CLONE_NEWIPC ,
163bf178 890.BR CLONE_NEWNET ,
43ce9dda
MK
891.BR CLONE_NEWNS ,
892.BR CLONE_NEWPID ,
82ee147a 893or
43ce9dda 894.BR CLONE_NEWUTS
0b9bdf82 895was specified by a non-root process (process without \fBCAP_SYS_ADMIN\fP).
fea681da
MK
896.TP
897.B EPERM
898.B CLONE_PID
899was specified by a process other than process 0.
a759cc87 900.SH VERSIONS
fea681da 901There is no entry for
edcc65ff 902.BR clone ()
a759cc87
MK
903in libc5.
904glibc2 provides
edcc65ff 905.BR clone ()
fea681da 906as described in this manual page.
a1d5f77c
MK
907.SH "CONFORMING TO"
908The
909.BR clone ()
910and
911.B sys_clone
8382f16d 912calls are Linux-specific and should not be used in programs
a1d5f77c 913intended to be portable.
fea681da 914.SH NOTES
fd8a5be4
MK
915In the kernel 2.4.x series,
916.B CLONE_THREAD
917generally does not make the parent of the new thread the same
918as the parent of the calling process.
919However, for kernel versions 2.4.7 to 2.4.18 the
920.B CLONE_THREAD
921flag implied the
c13182ef 922.B CLONE_PARENT
fd8a5be4 923flag (as in kernel 2.6).
fea681da 924
c13182ef
MK
925For a while there was
926.B CLONE_DETACHED
a5053dcb 927(introduced in 2.5.32):
c13182ef 928parent wants no child-exit signal.
a5053dcb 929In 2.6.2 the need to give this
c13182ef
MK
930together with
931.B CLONE_THREAD
a5053dcb
MK
932disappeared.
933This flag is still defined, but has no effect.
934
34ccb744 935On i386,
a5a997ca
MK
936.BR clone ()
937should not be called through vsyscall, but directly through
938.IR "int $0x80" .
ff929e3b 939
22399250 940On ia64, a different system call is used:
ff929e3b
MK
941.nf
942
9b0e0996
MK
943.BI "int __clone2(int (*" "fn" ")(void *), "
944.BI " void *" child_stack_base ", size_t " stack_size ,
945.BI " int " flags ", void *" "arg" ", ... "
946.BI " /* pid_t *" pid ", struct user_desc *" tls \
ff929e3b
MK
947", pid_t *" ctid " */ );"
948.fi
949.PP
950The
9b0e0996 951.BR __clone2 ()
c13182ef 952system call operates in the same way as
ff929e3b
MK
953.BR clone (),
954except that
955.I child_stack_base
956points to the lowest address of the child's stack area,
957and
958.I stack_size
959specifies the size of the stack pointed to by
960.IR child_stack_base .
31830ef0
MK
961.SH BUGS
962Versions of the GNU C library that include the NPTL threading library
c13182ef 963contain a wrapper function for
0bfa087b 964.BR getpid (2)
31830ef0 965that performs caching of PIDs.
c60237c9
MK
966This caching relies on support in the glibc wrapper for
967.BR clone (),
968but as currently implemented,
969the cache may not be up to date in some circumstances.
970In particular,
971if a signal is delivered to the child immediately after the
972.BR clone ()
973call, then a call to
974.BR getpid ()
975in a handler for the signal may return the PID
976of the calling process ("the parent"),
88619baf 977if the clone wrapper has not yet had a chance to update the PID
c60237c9
MK
978cache in the child.
979(This discussion ignores the case where the child was created using
9291ce36 980.BR CLONE_THREAD ,
c60237c9
MK
981when
982.BR getpid ()
983.I should
984return the same value in the child and in the process that called
985.BR clone (),
a1d48abb 986since the caller and the child are in the same thread group.
e7d807b7 987The stale-cache problem also does not occur if the
a1d48abb
JR
988.I flags
989argument includes
990.BR CLONE_VM .)
c60237c9 991To get the truth, it may be necessary to use code such as the following:
31830ef0
MK
992.nf
993
994 #include <syscall.h>
995
996 pid_t mypid;
997
998 mypid = syscall(SYS_getpid);
999.fi
c60237c9
MK
1000.\" See also the following bug reports
1001.\" https://bugzilla.redhat.com/show_bug.cgi?id=417521
1002.\" http://sourceware.org/bugzilla/show_bug.cgi?id=6910
fea681da
MK
1003.SH "SEE ALSO"
1004.BR fork (2),
2b44301c 1005.BR futex (2),
fea681da
MK
1006.BR getpid (2),
1007.BR gettid (2),
f2d0bbf1 1008.BR set_thread_area (2),
2b44301c 1009.BR set_tid_address (2),
f2d0bbf1 1010.BR tkill (2),
5cc01e9c 1011.BR unshare (2),
fea681da 1012.BR wait (2),
3616b7c0
MK
1013.BR capabilities (7),
1014.BR pthreads (7)