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

NAME

6       clone - create a child process
7

SYNOPSIS

9       #include <sched.h>
10
11       int clone(int (*fn)(void *), void *child_stack,
12                 int flags, void *arg, ...
13                 /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );
14
15       _syscall2(int, clone, int, flags, void *, child_stack)
16
17       _syscall5(int, clone, int, flags, void *, child_stack,
18            int *, parent_tidptr, struct user_desc *, newtls,
19            int *, child_tidptr)
20               /* Using syscall(2) may be preferable; see intro(2) */
21

DESCRIPTION

23       clone() creates a new process, in a manner similar to fork(2).  clone()
24       is a library function layered on top of the underlying  clone()  system
25       call, hereinafter referred to as sys_clone.  A description of sys_clone
26       is given towards the end of this page.
27
28       Unlike fork(2), these calls allow the child process to share  parts  of
29       its  execution  context  with  the  calling process, such as the memory
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       The main use of clone() is to implement threads:  multiple  threads  of
35       control in a program that run concurrently in a shared memory space.
36
37       When  the  child process is created with clone(), it executes the func‐
38       tion application fn(arg).  (This differs from fork(2), where  execution
39       continues  in  the  child  from the point of the fork(2) call.)  The fn
40       argument is a pointer to a function that is called by the child process
41       at  the  beginning of its execution.  The arg argument is passed to the
42       fn function.
43
44       When the fn(arg) function application returns, the child process termi‐
45       nates.   The  integer  returned  by  fn  is the exit code for the child
46       process.  The child process may also terminate  explicitly  by  calling
47       exit(2) or after receiving a fatal signal.
48
49       The  child_stack  argument  specifies the location of the stack used by
50       the child process.  Since the child and calling process may share  mem‐
51       ory,  it  is  not possible for the child process to execute in the same
52       stack as the calling process.  The calling process must  therefore  set
53       up memory space for the child stack and pass a pointer to this space to
54       clone().  Stacks grow  downwards  on  all  processors  that  run  Linux
55       (except  the  HP  PA  processors), so child_stack usually points to the
56       topmost address of the memory space set up for the child stack.
57
58       The low byte of flags contains the number  of  the  termination  signal
59       sent to the parent when the child dies.  If this signal is specified as
60       anything other than SIGCHLD, then the parent process must  specify  the
61       __WALL or __WCLONE options when waiting for the child with wait(2).  If
62       no signal is specified, then the parent process is  not  signaled  when
63       the child terminates.
64
65       flags may also be bitwise-or'ed with zero or more of the following con‐
66       stants, in order to specify what is shared between the calling  process
67       and the child process:
68
69       CLONE_PARENT (since Linux 2.3.12)
70              If  CLONE_PARENT  is  set,  then the parent of the new child (as
71              returned by getppid(2)) will be the same as that of the  calling
72              process.
73
74              If  CLONE_PARENT  is not set, then (as with fork(2)) the child's
75              parent is the calling process.
76
77              Note that it is the parent process, as returned  by  getppid(2),
78              which  is  signaled  when  the  child  terminates,  so  that  if
79              CLONE_PARENT is set, then the parent  of  the  calling  process,
80              rather than the calling process itself, will be signaled.
81
82       CLONE_FS
83              If CLONE_FS is set, the caller and the child processes share the
84              same file system information.  This includes  the  root  of  the
85              file  system, the current working directory, and the umask.  Any
86              call to chroot(2), chdir(2), or umask(2) performed by the  call‐
87              ing process or the child process also affects the other process.
88
89              If CLONE_FS is not set, the child process works on a copy of the
90              file system information of the calling process at  the  time  of
91              the  clone()  call.  Calls to chroot(2), chdir(2), umask(2) per‐
92              formed later by one of the processes do  not  affect  the  other
93              process.
94
95       CLONE_FILES
96              If  CLONE_FILES  is  set, the calling process and the child pro‐
97              cesses share the same file descriptor table.  Any file  descrip‐
98              tor  created  by  the calling process or by the child process is
99              also valid in the other process.  Similarly, if one of the  pro‐
100              cesses closes a file descriptor, or changes its associated flags
101              (using the fcntl(2) F_SETFD operation),  the  other  process  is
102              also affected.
103
104              If  CLONE_FILES is not set, the child process inherits a copy of
105              all file descriptors opened in the calling process at  the  time
106              of clone().  (The duplicated file descriptors in the child refer
107              to the same open file descriptions (see open(2)) as  the  corre‐
108              sponding  file  descriptors in the calling process.)  Subsequent
109              operations that open or close file descriptors, or  change  file
110              descriptor flags, performed by either the calling process or the
111              child process do not affect the other process.
112
113       CLONE_NEWNS (since Linux 2.4.19)
114              Start the child in a new namespace.
115
116              Every process lives in a namespace. The namespace of  a  process
117              is the data (the set of mounts) describing the file hierarchy as
118              seen by that process. After a  fork(2)  or  clone(2)  where  the
119              CLONE_NEWNS  flag is not set, the child lives in the same names‐
120              pace as the parent.  The system  calls  mount(2)  and  umount(2)
121              change  the  namespace  of the calling process, and hence affect
122              all processes that live in the same namespace, but do not affect
123              processes in a different namespace.
124
125              After  a  clone(2) where the CLONE_NEWNS flag is set, the cloned
126              child is started in a new namespace, initialized with a copy  of
127              the namespace of the parent.
128
129              Only a privileged process (one having the CAP_SYS_ADMIN capabil‐
130              ity) may specify the CLONE_NEWNS flag.  It is not  permitted  to
131              specify both CLONE_NEWNS and CLONE_FS in the same clone() call.
132
133       CLONE_SIGHAND
134              If  CLONE_SIGHAND is set, the calling process and the child pro‐
135              cesses share the same table of signal handlers.  If the  calling
136              process or child process calls sigaction(2) to change the behav‐
137              ior associated with a signal, the behavior  is  changed  in  the
138              other  process  as well.  However, the calling process and child
139              processes still have distinct signal masks and sets  of  pending
140              signals.   So,  one  of  them  may block or unblock some signals
141              using sigprocmask(2) without affecting the other process.
142
143              If CLONE_SIGHAND is not set, the child process inherits  a  copy
144              of  the  signal  handlers  of  the  calling  process at the time
145              clone() is called.  Calls to sigaction(2) performed later by one
146              of the processes have no effect on the other process.
147
148              Since  Linux  2.6.0-test6,  flags  must also include CLONE_VM if
149              CLONE_SIGHAND is specified
150
151       CLONE_PTRACE
152              If CLONE_PTRACE is specified, and the calling process  is  being
153              traced, then trace the child also (see ptrace(2)).
154
155       CLONE_UNTRACED (since Linux 2.5.46)
156              If  CLONE_UNTRACED  is  specified, then a tracing process cannot
157              force CLONE_PTRACE on this child process.
158
159       CLONE_STOPPED (since Linux 2.6.0-test2)
160              If CLONE_STOPPED is set, then the child is initially stopped (as
161              though  it  was  sent  a SIGSTOP signal), and must be resumed by
162              sending it a SIGCONT signal.
163
164       CLONE_VFORK
165              If CLONE_VFORK is set, the execution of the calling  process  is
166              suspended  until the child releases its virtual memory resources
167              via a call to execve(2) or _exit(2) (as with vfork(2)).
168
169              If CLONE_VFORK is not set then both the calling process and  the
170              child  are schedulable after the call, and an application should
171              not rely on execution occurring in any particular order.
172
173       CLONE_VM
174              If CLONE_VM is set, the calling process and the child  processes
175              run in the same memory space.  In particular, memory writes per‐
176              formed by the calling process or by the child process  are  also
177              visible  in  the other process.  Moreover, any memory mapping or
178              unmapping performed with mmap(2) or munmap(2) by  the  child  or
179              calling process also affects the other process.
180
181              If  CLONE_VM  is  not  set, the child process runs in a separate
182              copy of the memory space of the calling process at the  time  of
183              clone().  Memory writes or file mappings/unmappings performed by
184              one of the processes do not affect the other, as with fork(2).
185
186       CLONE_PID (obsolete)
187              If CLONE_PID is set, the child process is created with the  same
188              process  ID as the calling process. This is good for hacking the
189              system, but otherwise of not much use. Since  2.3.21  this  flag
190              can  be  specified  only by the system boot process (PID 0).  It
191              disappeared in Linux 2.5.16.
192
193       CLONE_THREAD (since Linux 2.4.0-test8)
194              If CLONE_THREAD is set, the child is placed in the  same  thread
195              group as the calling process.  To make the remainder of the dis‐
196              cussion of CLONE_THREAD more readable, the term "thread" is used
197              to refer to the processes within a thread group.
198
199              Thread  groups  were a feature added in Linux 2.4 to support the
200              POSIX threads notion of a set of threads  that  share  a  single
201              PID.   Internally, this shared PID is the so-called thread group
202              identifier (TGID) for the thread group.  Since Linux 2.4,  calls
203              to getpid(2) return the TGID of the caller.
204
205              The  threads  within a group can be distinguished by their (sys‐
206              tem-wide) unique thread IDs (TID).  A new thread's TID is avail‐
207              able  as  the function result returned to the caller of clone(),
208              and a thread can obtain its own TID using gettid(2).
209
210              When a call is made to clone() without specifying  CLONE_THREAD,
211              then  the resulting thread is placed in a new thread group whose
212              TGID is the same as the thread's TID.  This thread is the leader
213              of the new thread group.
214
215              A  new  thread  created  with  CLONE_THREAD  has the same parent
216              process as the caller of clone() (i.e., like  CLONE_PARENT),  so
217              that  calls  to  getppid(2) return the same value for all of the
218              threads in a thread group.  When a  CLONE_THREAD  thread  termi‐
219              nates,  the  thread  that created it using clone() is not sent a
220              SIGCHLD (or other termination) signal; nor  can  the  status  of
221              such a thread be obtained using wait(2).  (The thread is said to
222              be detached.)
223
224              After all of the threads in a thread group terminate the  parent
225              process of the thread group is sent a SIGCHLD (or other termina‐
226              tion) signal.
227
228              If any of the threads in a thread group performs  an  execve(2),
229              then  all  threads other than the thread group leader are termi‐
230              nated, and the new program  is  executed  in  the  thread  group
231              leader.
232
233              If  one  of  the threads in a thread group creates a child using
234              fork(2), then any thread in  the  group  can  wait(2)  for  that
235              child.
236
237              Since  Linux  2.5.35,  flags  must also include CLONE_SIGHAND if
238              CLONE_THREAD is specified.
239
240              Signals may be sent to a thread group as a whole (i.e., a  TGID)
241              using  kill(2),  or  to  a  specific  thread  (i.e.,  TID) using
242              tgkill(2).
243
244              Signal dispositions and actions are process-wide: if  an  unhan‐
245              dled  signal is delivered to a thread, then it will affect (ter‐
246              minate, stop, continue, be ignored in) all members of the thread
247              group.
248
249              Each  thread  has its own signal mask, as set by sigprocmask(2),
250              but signals can be pending either: for the whole process  (i.e.,
251              deliverable  to  any member of the thread group), when sent with
252              kill(2); or for an individual thread, when sent with  tgkill(2).
253              A  call  to sigpending(2) returns a signal set that is the union
254              of the signals pending for the whole  process  and  the  signals
255              that are pending for the calling thread.
256
257              If  kill(2)  is used to send a signal to a thread group, and the
258              thread group has installed a handler for the  signal,  then  the
259              handler  will  be  invoked  in exactly one, arbitrarily selected
260              member of the thread group that has not blocked the signal.   If
261              multiple  threads in a group are waiting to accept the same sig‐
262              nal using sigwaitinfo(2), the kernel will arbitrarily select one
263              of these threads to receive a signal sent using kill(2).
264
265       CLONE_SYSVSEM (since Linux 2.5.10)
266              If  CLONE_SYSVSEM is set, then the child and the calling process
267              share a single list of  System  V  semaphore  undo  values  (see
268              semop(2)).   If this flag is not set, then the child has a sepa‐
269              rate undo list, which is initially empty.
270
271       CLONE_SETTLS (since Linux 2.5.32)
272              The newtls parameter is  the  new  TLS  (Thread  Local  Storage)
273              descriptor.  (See set_thread_area(2).)
274
275       CLONE_PARENT_SETTID (since Linux 2.5.49)
276              Store  child  thread  ID at location parent_tidptr in parent and
277              child  memory.   (In  Linux  2.5.32-2.5.48  there  was  a   flag
278              CLONE_SETTID that did this.)
279
280       CLONE_CHILD_SETTID (since Linux 2.5.49)
281              Store child thread ID at location child_tidptr in child memory.
282
283       CLONE_CHILD_CLEARTID (since Linux 2.5.49)
284              Erase  child  thread ID at location child_tidptr in child memory
285              when the child exits, and do a  wakeup  on  the  futex  at  that
286              address.    The   address   involved   may  be  changed  by  the
287              set_tid_address(2)  system  call.  This  is  used  by  threading
288              libraries.
289
290
291   sys_clone
292       The  sys_clone  system call corresponds more closely to fork(2) in that
293       execution in the child continues from the point  of  the  call.   Thus,
294       sys_clone only requires the flags and child_stack arguments, which have
295       the same meaning as for clone().  (Note that the order of  these  argu‐
296       ments differs from clone().)
297
298       Another  difference  for sys_clone is that the child_stack argument may
299       be zero, in which case copy-on-write semantics ensure  that  the  child
300       gets  separate  copies  of stack pages when either process modifies the
301       stack.  In this case, for correct operation, the CLONE_VM option should
302       not be specified.
303
304       Since  Linux  2.5.49  the system call has five parameters.  The two new
305       parameters are parent_tidptr which points to the  location  (in  parent
306       and  child  memory)  where  the child thread ID will be written in case
307       CLONE_PARENT_SETTID was specified, and child_tidptr which points to the
308       location (in child memory) where the child thread ID will be written in
309       case CLONE_CHILD_SETTID was specified.
310

RETURN VALUE

312       On success, the thread ID of the child process is returned in the call‐
313       er's  thread  of  execution.   On failure, a -1 will be returned in the
314       caller's context, no child process will be created, and errno  will  be
315       set appropriately.
316

ERRORS

318       EAGAIN Too many processes are already running.
319
320       EINVAL CLONE_SIGHAND  was specified, but CLONE_VM was not. (Since Linux
321              2.6.0-test6.)
322
323       EINVAL CLONE_THREAD was specified, but CLONE_SIGHAND  was  not.  (Since
324              Linux 2.5.35.)
325
326       EINVAL Both CLONE_FS and CLONE_NEWNS were specified in flags.
327
328       EINVAL Returned   by  clone()  when  a  zero  value  is  specified  for
329              child_stack.
330
331       ENOMEM Cannot allocate sufficient memory to allocate a  task  structure
332              for  the  child,  or to copy those parts of the caller's context
333              that need to be copied.
334
335       EPERM  CLONE_NEWNS was specified by a non-root process (process without
336              CAP_SYS_ADMIN).
337
338       EPERM  CLONE_PID was specified by a process other than process 0.
339

AVAILABILITY

341       There  is  no  entry  for clone() in libc5.  glibc2 provides clone() as
342       described in this manual page.
343

NOTES

345       In the kernel 2.4.x series, CLONE_THREAD generally does  not  make  the
346       parent of the new thread the same as the parent of the calling process.
347       However, for kernel versions 2.4.7  to  2.4.18  the  CLONE_THREAD  flag
348       implied the CLONE_PARENT flag (as in kernel 2.6).
349
350       For  a  while  there  was CLONE_DETACHED (introduced in 2.5.32): parent
351       wants no child-exit signal.  In 2.6.2 the need to  give  this  together
352       with  CLONE_THREAD disappeared.  This flag is still defined, but has no
353       effect.
354
355       On x86, clone() should not be called  through  vsyscall,  but  directly
356       through int $0x80.
357
358       On IA-64, a different system call is used:
359
360       int clone2(int (*fn)(void *),
361                  void *child_stack_base, size_t stack_size,
362                  int flags, void *arg, ...
363                  /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );
364
365       The  clone2()  system  call operates in the same way as clone(), except
366       that child_stack_base points to the lowest address of the child's stack
367       area,  and  stack_size  specifies  the  size of the stack pointed to by
368       child_stack_base.
369

CONFORMING TO

371       The clone() and sys_clone calls are Linux specific and  should  not  be
372       used in programs intended to be portable.
373

BUGS

375       Versions  of  the GNU C library that include the NPTL threading library
376       contain a wrapper function for getpid() that performs caching of  PIDs.
377       In programs linked against such libraries, calls to getpid() may return
378       the  same  value,  even  when  the  threads  were  not  created   using
379       CLONE_THREAD  (and  thus are not in the same thread group).  To get the
380       truth, it may be necessary to use code such as the following
381
382           #include <syscall.h>
383
384           pid_t mypid;
385
386           mypid = syscall(SYS_getpid);
387

SEE ALSO

389       fork(2),   futex(2),    getpid(2),    gettid(2),    set_thread_area(2),
390       set_tid_address(2),  tkill(2),  unshare(2),  wait(2),  capabilities(7),
391       pthreads(7)
392
393
394
395Linux 2.6                         2005-05-17                          CLONE(2)
Impressum