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