OSDN Git Service

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