1CLONE(2) Linux Programmer's Manual CLONE(2)
2
3
4
6 clone, __clone2 - create a child process
7
9 /* Prototype for the glibc wrapper function */
10
11 #define _GNU_SOURCE
12 #include <sched.h>
13
14 int clone(int (*fn)(void *), void *child_stack,
15 int flags, void *arg, ...
16 /* pid_t *ptid, void *newtls, pid_t *ctid */ );
17
18 /* For the prototype of the raw system call, see NOTES */
19
21 clone() creates a new process, in a manner similar to fork(2).
22
23 This page describes both the glibc clone() wrapper function and the
24 underlying system call on which it is based. The main text describes
25 the wrapper function; the differences for the raw system call are
26 described toward the end of this page.
27
28 Unlike fork(2), clone() allows the child process to share parts of its
29 execution context with the calling process, such as the virtual address
30 space, the table of file descriptors, and the table of signal handlers.
31 (Note that on this manual page, "calling process" normally corresponds
32 to "parent process". But see the description of CLONE_PARENT below.)
33
34 One use of clone() is to implement threads: multiple flows of control
35 in a program that run concurrently in a shared address space.
36
37 When the child process is created with clone(), it commences execution
38 by calling the function pointed to by the argument fn. (This differs
39 from fork(2), where execution continues in the child from the point of
40 the fork(2) call.) The arg argument is passed as the argument of the
41 function fn.
42
43 When the fn(arg) function returns, the child process terminates. The
44 integer returned by fn is the exit status for the child process. The
45 child process may also terminate explicitly by calling exit(2) or after
46 receiving a fatal signal.
47
48 The child_stack argument specifies the location of the stack used by
49 the child process. Since the child and calling process may share mem‐
50 ory, it is not possible for the child process to execute in the same
51 stack as the calling process. The calling process must therefore set
52 up memory space for the child stack and pass a pointer to this space to
53 clone(). Stacks grow downward on all processors that run Linux (except
54 the HP PA processors), so child_stack usually points to the topmost
55 address of the memory space set up for the child stack.
56
57 The low byte of flags contains the number of the termination signal
58 sent to the parent when the child dies. If this signal is specified as
59 anything other than SIGCHLD, then the parent process must specify the
60 __WALL or __WCLONE options when waiting for the child with wait(2). If
61 no signal is specified, then the parent process is not signaled whe