1CLONE(2)                   Linux Programmer's Manual                  CLONE(2)
2
3
4

NAME

6       clone, __clone2 - create a child process
7

SYNOPSIS

9       #define _GNU_SOURCE
10       #include <sched.h>
11
12       int clone(int (*fn)(void *), void *child_stack,
13                 int flags, void *arg, ...
14                 /* pid_t *ptid, struct user_desc *tls, pid_t *ctid */ );
15

DESCRIPTION

17       clone()  creates  a new process, in a manner similar to fork(2).  It is
18       actually a library function layered on top of  the  underlying  clone()
19       system  call,  hereinafter  referred to as sys_clone.  A description of
20       sys_clone is given towards the end of this page.
21
22       Unlike fork(2), these calls allow the child process to share  parts  of
23       its  execution  context  with  the  calling process, such as the memory
24       space, the table of file descriptors, and the table of signal handlers.
25       (Note  that on this manual page, "calling process" normally corresponds
26       to "parent process".  But see the description of CLONE_PARENT below.)
27
28       The main use of clone() is to implement threads:  multiple  threads  of
29       control in a program that run concurrently in a shared memory space.
30
31       When  the  child process is created with clone(), it executes the func‐
32       tion application fn(arg).  (This differs from fork(2), where  execution
33       continues  in  the  child  from the point of the fork(2) call.)  The fn
34       argument is a pointer to a function that is called by the child process
35       at  the  beginning of its execution.  The arg argument is passed to the
36       fn function.
37
38       When the fn(arg) function application returns, the child process termi‐
39       nates.   The  integer  returned  by  fn  is the exit code for the child
40       process.  The child process may also terminate  explicitly  by  calling
41       exit(2) or after receiving a fatal signal.
42
43       The  child_stack  argument  specifies the location of the stack used by
44       the child process.  Since the child and calling process may share  mem‐
45       ory,  it  is  not possible for the child process to execute in the same
46       stack as the calling process.  The calling process must  therefore  set
47       up memory space for the child stack and pass a pointer to this space to
48       clone().  Stacks grow  downwards  on  all  processors  that  run  Linux
49       (except  the  HP  PA  processors), so child_stack usually points to the
50       topmost address of the memory space set up for the child stack.
51
52       The low byte of flags contains the number  of  the  termination  signal
53       sent to the parent when the child dies.  If this signal is specified as
54       anything other than SIGCHLD, then the parent process must  specify  the
55       __WALL or __WCLONE options when waiting for the child with wait(2).  If
56       no signal is specified, then the parent process is  not  signaled  when
57       the child terminates.
58
59       flags may also be bitwise-or'ed with zero or more of the following con‐