]>
Commit | Line | Data |
---|---|---|
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 | 47 | clone, __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 () |
62 | creates a new process, in a manner similar to | |
fea681da | 63 | .BR fork (2). |
735f354f | 64 | It is actually a library function layered on top of the underlying |
e511ffb6 | 65 | .BR clone () |
fea681da MK |
66 | system call, hereinafter referred to as |
67 | .BR sys_clone . | |
68 | A description of | |
0daa9e92 | 69 | .B sys_clone |
fea681da MK |
70 | is given towards the end of this page. |
71 | ||
72 | Unlike | |
73 | .BR fork (2), | |
c13182ef | 74 | these calls |
fea681da MK |
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 | |
c13182ef MK |
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 | |
fea681da MK |
82 | below.) |
83 | ||
84 | The main use of | |
edcc65ff | 85 | .BR clone () |
fea681da MK |
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 | |
c13182ef | 90 | .BR clone (), |
fea681da MK |
91 | it executes the function |
92 | application | |
c13182ef | 93 | .IR fn ( arg ). |
fea681da | 94 | (This differs from |
c13182ef | 95 | .BR fork (2), |
fea681da | 96 | where execution continues in the child from the point |
c13182ef MK |
97 | of the |
98 | .BR fork (2) | |
fea681da MK |
99 | call.) |
100 | The | |
101 | .I fn | |
102 | argument is a pointer to a function that is called by the child | |
103 | process at the beginning of its execution. | |
104 | The | |
105 | .I arg | |
106 | argument is passed to the | |
107 | .I fn | |
108 | function. | |
109 | ||
c13182ef | 110 | When the |
fea681da | 111 | .IR fn ( arg ) |
c13182ef MK |
112 | function application returns, the child process terminates. |
113 | The integer returned by | |
fea681da | 114 | .I fn |
c13182ef MK |
115 | is the exit code for the child process. |
116 | The child process may also terminate explicitly by calling | |
fea681da MK |
117 | .BR exit (2) |
118 | or after receiving a fatal signal. | |
119 | ||
120 | The | |
121 | .I child_stack | |
c13182ef MK |
122 | argument specifies the location of the stack used by the child process. |
123 | Since the child and calling process may share memory, | |
fea681da | 124 | it is not possible for the child process to execute in the |
c13182ef MK |
125 | same stack as the calling process. |
126 | The calling process must therefore | |
fea681da MK |
127 | set up memory space for the child stack and pass a pointer to this |
128 | space to | |
edcc65ff | 129 | .BR clone (). |
fea681da MK |
130 | Stacks grow downwards on all processors that run Linux |
131 | (except the HP PA processors), so | |
132 | .I child_stack | |
133 | usually points to the topmost address of the memory space set up for | |
134 | the child stack. | |
135 | ||
136 | The low byte of | |
137 | .I flags | |
fd8a5be4 MK |
138 | contains the number of the |
139 | .I "termination signal" | |
140 | sent to the parent when the child dies. | |
141 | If this signal is specified as anything other than | |
fea681da MK |
142 | .BR SIGCHLD , |
143 | then the parent process must specify the | |
c13182ef MK |
144 | .B __WALL |
145 | or | |
fea681da | 146 | .B __WCLONE |
c13182ef MK |
147 | options when waiting for the child with |
148 | .BR wait (2). | |
fea681da MK |
149 | If no signal is specified, then the parent process is not signaled |
150 | when the child terminates. | |
151 | ||
152 | .I flags | |
fd8a5be4 MK |
153 | may also be bitwise-or'ed with zero or more of the following constants, |
154 | in order to specify what is shared between the calling process | |
fea681da | 155 | and the child process: |
fea681da | 156 | .TP |
f5dbc7c8 MK |
157 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" |
158 | Erase child thread ID at location | |
159 | .I child_tidptr | |
160 | in child memory when the child exits, and do a wakeup on the futex | |
161 | at that address. | |
162 | The address involved may be changed by the | |
163 | .BR set_tid_address (2) | |
164 | system call. | |
165 | This is used by threading libraries. | |
166 | .TP | |
167 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
168 | Store child thread ID at location | |
169 | .I child_tidptr | |
170 | in child memory. | |
171 | .TP | |
172 | .B CLONE_FILES | |
fea681da | 173 | If |
f5dbc7c8 MK |
174 | .B CLONE_FILES |
175 | is set, the calling process and the child process share the same file | |
176 | descriptor table. | |
177 | Any file descriptor created by the calling process or by the child | |
178 | process is also valid in the other process. | |
179 | Similarly, if one of the processes closes a file descriptor, | |
180 | or changes its associated flags (using the | |
181 | .BR fcntl (2) | |
182 | .B F_SETFD | |
183 | operation), the other process is also affected. | |
fea681da MK |
184 | |
185 | If | |
f5dbc7c8 MK |
186 | .B CLONE_FILES |
187 | is not set, the child process inherits a copy of all file descriptors | |
188 | opened in the calling process at the time of | |
189 | .BR clone (). | |
190 | (The duplicated file descriptors in the child refer to the | |
191 | same open file descriptions (see | |
192 | .BR open (2)) | |
193 | as the corresponding file descriptors in the calling process.) | |
194 | Subsequent operations that open or close file descriptors, | |
195 | or change file descriptor flags, | |
196 | performed by either the calling | |
197 | process or the child process do not affect the other process. | |
fea681da MK |
198 | .TP |
199 | .B CLONE_FS | |
200 | If | |
201 | .B CLONE_FS | |
314c8ff4 | 202 | is set, the caller and the child process share the same file system |
c13182ef MK |
203 | information. |
204 | This includes the root of the file system, the current | |
205 | working directory, and the umask. | |
206 | Any call to | |
fea681da MK |
207 | .BR chroot (2), |
208 | .BR chdir (2), | |
209 | or | |
210 | .BR umask (2) | |
edcc65ff | 211 | performed by the calling process or the child process also affects the |
fea681da MK |
212 | other process. |
213 | ||
c13182ef | 214 | If |
fea681da MK |
215 | .B CLONE_FS |
216 | is not set, the child process works on a copy of the file system | |
217 | information of the calling process at the time of the | |
edcc65ff | 218 | .BR clone () |
fea681da MK |
219 | call. |
220 | Calls to | |
221 | .BR chroot (2), | |
222 | .BR chdir (2), | |
223 | .BR umask (2) | |
224 | performed 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)" |
227 | If | |
228 | .B CLONE_IO | |
229 | is set, then the new process shares an I/O context with | |
230 | the calling process. | |
231 | If this flag is not set, then (as with | |
232 | .BR fork (2)) | |
233 | the new process has its own I/O context. | |
234 | ||
235 | .\" The following based on text from Jens Axboe | |
236 | The I/O context is the I/O scope of the disk scheduler (i.e, | |
237 | what the I/O scheduler uses to model scheduling of a process's I/O). | |
238 | If processes share the same I/O context, | |
239 | they are treated as one by the I/O scheduler. | |
240 | As a consequence, they get to share disk time. | |
241 | For some I/O schedulers, | |
242 | .\" the anticipatory and CFQ scheduler | |
243 | if two processes share an I/O context, | |
244 | they will be allowed to interleave their disk access. | |
245 | If several threads are doing I/O on behalf of the same process | |
246 | .RB ( aio_read (3), | |
247 | for instance), they should employ | |
248 | .BR CLONE_IO | |
249 | to get better I/O performance. | |
250 | .\" with CFQ and AS. | |
251 | ||
252 | If the kernel is not configured with the | |
253 | .B CONFIG_BLOCK | |
254 | option, this flag is a no-op. | |
255 | .TP | |
667417b3 MK |
256 | .BR CLONE_NEWIPC " (since Linux 2.4.19)" |
257 | If | |
258 | .B CLONE_NEWIPC | |
259 | is set, then create the process in a new IPC namespace. | |
260 | If this flag is not set, then (as with | |
261 | .BR fork (2)), | |
262 | the process is created in the same IPC namespace as | |
263 | the calling process. | |
264 | This flag is intended for the implementation of control groups. | |
265 | ||
266 | An IPC namespace consists of the set of identifiers for | |
267 | System V IPC objects. | |
268 | (These objects are created using | |
269 | .BR msgctl (2), | |
270 | .BR semctl (2), | |
271 | and | |
272 | .BR shmctl (2)). | |
273 | Objects created in an IPC namespace are visible to other processes | |
274 | that are members of that namespace, | |
275 | but are not visible to processes in other IPC namespaces. | |
276 | ||
277 | Use of this flag requires: a kernel configured with the | |
278 | .B CONFIG_SYSVIPC | |
279 | and | |
280 | .B CONFIG_IPC_NS | |
281 | configuration options and that the process be privileged | |
282 | .RB ( CAP_SYS_ADMIN ). | |
283 | This 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 | 287 | Start the child in a new mount-point namespace. |
fea681da | 288 | |
4df2eb09 | 289 | Every process lives in a mount-point namespace. |
c13182ef | 290 | The |
fea681da MK |
291 | .I namespace |
292 | of a process is the data (the set of mounts) describing the file hierarchy | |
c13182ef MK |
293 | as seen by that process. |
294 | After a | |
fea681da MK |
295 | .BR fork (2) |
296 | or | |
2777b1ca | 297 | .BR clone () |
fea681da MK |
298 | where the |
299 | .B CLONE_NEWNS | |
4df2eb09 MK |
300 | flag is not set, the child lives in the same mount-point |
301 | namespace as the parent. | |
fea681da MK |
302 | The system calls |
303 | .BR mount (2) | |
304 | and | |
305 | .BR umount (2) | |
4df2eb09 | 306 | change the mount-point namespace of the calling process, and hence affect |
fea681da | 307 | all processes that live in the same namespace, but do not affect |
4df2eb09 | 308 | processes in a different mount-point namespace. |
fea681da MK |
309 | |
310 | After a | |
2777b1ca | 311 | .BR clone () |
fea681da MK |
312 | where the |
313 | .B CLONE_NEWNS | |
4df2eb09 | 314 | flag is set, the cloned child is started in a new mount-point namespace, |
fea681da MK |
315 | initialized with a copy of the namespace of the parent. |
316 | ||
0b9bdf82 | 317 | Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability) |
fea681da MK |
318 | may specify the |
319 | .B CLONE_NEWNS | |
320 | flag. | |
321 | It is not permitted to specify both | |
322 | .B CLONE_NEWNS | |
323 | and | |
324 | .B CLONE_FS | |
325 | in the same | |
e511ffb6 | 326 | .BR clone () |
fea681da | 327 | call. |
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> | |
337 | If | |
5c95e5e8 | 338 | .B CLONE_NEWPID |
82ee147a MK |
339 | is set, then create the process in a new PID namespace. |
340 | If this flag is not set, then (as with | |
341 | .BR fork (2)), | |
342 | the process is created in the same PID namespace as | |
343 | the calling process. | |
344 | This flag is intended for the implementation of control groups. | |
345 | ||
346 | A PID namespace provides an isolated environment for PIDs: | |
347 | PIDs in a new namespace start at 1, | |
348 | somewhat like a standalone system, and calls to | |
349 | .BR fork (2), | |
350 | .BR vfork (2), | |
351 | or | |
352 | .BR clone (2) | |
353 | will produce processes whose PIDs within the namespace | |
354 | are only guaranteed to be unique within that namespace. | |
355 | ||
356 | The first process created in a new namespace | |
357 | (i.e., the process created using the | |
358 | .BR CLONE_NEWPID | |
359 | flag) has the PID 1, and is the "init" process for the namespace. | |
360 | Children that are orphaned within the namespace will be reparented | |
361 | to this process rather than | |
362 | .BR init (8). | |
363 | Unlike the traditional | |
364 | .B init | |
365 | process, the "init" process of a PID namespace can terminate, | |
366 | and if it does, all of the processes in the namespace are terminated. | |
367 | ||
368 | PID namespaces form a hierarchy. | |
369 | When a PID new namespace is created, | |
370 | the PIDs of the processes in that namespace are visible | |
371 | in the PID namespace of the process that created the new namespace; | |
372 | analogously, if the parent PID namespace is itself | |
373 | the child of another PID namespace, | |
374 | then PIDs of the child and parent PID namespaces will both be | |
375 | visible in the grandparent PID namespace. | |
376 | Conversely, the processes in the "child" PID namespace do not see | |
377 | the PIDs of the processes in the parent namespace. | |
378 | The existence of a namespace hierarchy means that each process | |
379 | may now have multiple PIDs: | |
380 | one for each namespace in which it is visible. | |
381 | (A call to | |
382 | .BR getpid (2) | |
383 | always returns the PID associated with the namespace in which | |
384 | the process was created.) | |
385 | ||
386 | After creating the new namespace, | |
387 | it is useful for the child to change its root directory | |
388 | and mount a new procfs instance at | |
389 | .I /proc | |
390 | so that tools such as | |
391 | .BR ps (1) | |
392 | work correctly. | |
393 | .\" mount -t proc proc /proc | |
394 | ||
395 | Use of this flag requires: a kernel configured with the | |
396 | .B CONFIG_PID_NS | |
397 | configuration option and that the process be privileged | |
af3c728e | 398 | .RB ( CAP_SYS_ADMIN ). |
82ee147a MK |
399 | This 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)" |
403 | If | |
404 | .B CLONE_NEWUTS | |
405 | is set, then create the process in a new UTS namespace. | |
406 | If this flag is not set, then (as with | |
407 | .BR fork (2)), | |
408 | the process is created in the same UTS namespace as | |
409 | the calling process. | |
410 | This flag is intended for the implementation of control groups. | |
411 | ||
412 | A UTS namespace is the set of identifiers returned by | |
413 | .BR uname (2); | |
414 | among these, the domain name and the host name can be modified by | |
415 | .BR setdomainname (2) | |
416 | and | |
417 | .BR | |
418 | .BR sethostname (2), | |
419 | respectively. | |
420 | Changes made to these identifiers in one UTS namespace | |
421 | are visible to other processes in the same namespace, | |
422 | but are not visible to processes in other UTS namespaces. | |
423 | ||
424 | Use of this flag requires: a kernel configured with the | |
425 | .B CONFIG_UTS_NS | |
426 | configuration 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)" |
430 | If | |
431 | .B CLONE_PARENT | |
432 | is set, then the parent of the new child (as returned by | |
433 | .BR getppid (2)) | |
434 | will be the same as that of the calling process. | |
435 | ||
436 | If | |
437 | .B CLONE_PARENT | |
438 | is not set, then (as with | |
439 | .BR fork (2)) | |
440 | the child's parent is the calling process. | |
441 | ||
442 | Note that it is the parent process, as returned by | |
443 | .BR getppid (2), | |
444 | which is signaled when the child terminates, so that | |
445 | if | |
446 | .B CLONE_PARENT | |
447 | is set, then the parent of the calling process, rather than the | |
448 | calling process itself, will be signaled. | |
449 | .TP | |
450 | .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)" | |
451 | Store child thread ID at location | |
452 | .I parent_tidptr | |
453 | in parent and child memory. | |
454 | (In Linux 2.5.32-2.5.48 there was a flag | |
455 | .B CLONE_SETTID | |
456 | that did this.) | |
457 | .TP | |
458 | .BR CLONE_PID " (obsolete)" | |
459 | If | |
460 | .B CLONE_PID | |
461 | is set, the child process is created with the same process ID as | |
462 | the calling process. | |
463 | This is good for hacking the system, but otherwise | |
464 | of not much use. | |
465 | Since 2.3.21 this flag can be | |
466 | specified only by the system boot process (PID 0). | |
467 | It disappeared in Linux 2.5.16. | |
468 | .TP | |
469 | .B CLONE_PTRACE | |
470 | If | |
471 | .B CLONE_PTRACE | |
472 | is specified, and the calling process is being traced, | |
473 | then trace the child also (see | |
474 | .BR ptrace (2)). | |
475 | .TP | |
476 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
477 | The | |
478 | .I newtls | |
479 | argument is the new TLS (Thread Local Storage) descriptor. | |
480 | (See | |
481 | .BR set_thread_area (2).) | |
482 | .TP | |
fea681da MK |
483 | .B CLONE_SIGHAND |
484 | If | |
485 | .B CLONE_SIGHAND | |
314c8ff4 | 486 | is set, the calling process and the child process share the same table of |
c13182ef MK |
487 | signal handlers. |
488 | If the calling process or child process calls | |
fea681da | 489 | .BR sigaction (2) |
c13182ef MK |
490 | to change the behavior associated with a signal, the behavior is |
491 | changed in the other process as well. | |
492 | However, the calling process and child | |
fea681da | 493 | processes still have distinct signal masks and sets of pending |
c13182ef MK |
494 | signals. |
495 | So, one of them may block or unblock some signals using | |
fea681da MK |
496 | .BR sigprocmask (2) |
497 | without affecting the other process. | |
498 | ||
499 | If | |
500 | .B CLONE_SIGHAND | |
501 | is not set, the child process inherits a copy of the signal handlers | |
502 | of the calling process at the time | |
edcc65ff | 503 | .BR clone () |
c13182ef MK |
504 | is called. |
505 | Calls to | |
fea681da MK |
506 | .BR sigaction (2) |
507 | performed later by one of the processes have no effect on the other | |
508 | process. | |
29546c24 MK |
509 | |
510 | Since Linux 2.6.0-test6, | |
511 | .I flags | |
512 | must also include | |
513 | .B CLONE_VM | |
514 | if | |
515 | .B CLONE_SIGHAND | |
516 | is specified | |
fea681da | 517 | .TP |
a69b6bda MK |
518 | .BR CLONE_STOPPED " (since Linux 2.6.0-test2)" |
519 | If | |
520 | .B CLONE_STOPPED | |
521 | is set, then the child is initially stopped (as though it was sent a | |
522 | .B SIGSTOP | |
523 | signal), and must be resumed by sending it a | |
524 | .B SIGCONT | |
525 | signal. | |
ef37eaf2 MK |
526 | |
527 | .I "From Linux 2.6.25 this flag is deprecated." | |
528 | You probably never wanted to use it, | |
53c94269 | 529 | you 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 | 533 | If |
f5dbc7c8 MK |
534 | .B CLONE_SYSVSEM |
535 | is set, then the child and the calling process share | |
536 | a single list of System V semaphore undo values (see | |
537 | .BR semop (2)). | |
538 | If this flag is not set, then the child has a separate undo list, | |
539 | which is initially empty. | |
fea681da MK |
540 | .TP |
541 | .BR CLONE_THREAD " (since Linux 2.4.0-test8)" | |
542 | If | |
543 | .B CLONE_THREAD | |
544 | is set, the child is placed in the same thread group as the calling process. | |
fd8a5be4 MK |
545 | To make the remainder of the discussion of |
546 | .B CLONE_THREAD | |
547 | more readable, the term "thread" is used to refer to the | |
548 | processes within a thread group. | |
fea681da | 549 | |
fd8a5be4 MK |
550 | Thread groups were a feature added in Linux 2.4 to support the |
551 | POSIX threads notion of a set of threads that share a single PID. | |
552 | Internally, this shared PID is the so-called | |
553 | thread group identifier (TGID) for the thread group. | |
c13182ef | 554 | Since Linux 2.4, calls to |
fea681da | 555 | .BR getpid (2) |
fd8a5be4 MK |
556 | return the TGID of the caller. |
557 | ||
558 | The threads within a group can be distinguished by their (system-wide) | |
559 | unique thread IDs (TID). | |
560 | A new thread's TID is available as the function result | |
561 | returned to the caller of | |
562 | .BR clone (), | |
563 | and a thread can obtain | |
564 | its own TID using | |
565 | .BR gettid (2). | |
566 | ||
c13182ef | 567 | When a call is made to |
fd8a5be4 MK |
568 | .BR clone () |
569 | without specifying | |
570 | .BR CLONE_THREAD , | |
571 | then the resulting thread is placed in a new thread group | |
572 | whose TGID is the same as the thread's TID. | |
573 | This thread is the | |
574 | .I leader | |
575 | of the new thread group. | |
576 | ||
577 | A new thread created with | |
578 | .B CLONE_THREAD | |
579 | has the same parent process as the caller of | |
580 | .BR clone () | |
c13182ef | 581 | (i.e., like |
fd8a5be4 MK |
582 | .BR CLONE_PARENT ), |
583 | so that calls to | |
584 | .BR getppid (2) | |
585 | return the same value for all of the threads in a thread group. | |
586 | When a | |
c13182ef | 587 | .B CLONE_THREAD |
fd8a5be4 MK |
588 | thread terminates, the thread that created it using |
589 | .BR clone () | |
590 | is not sent a | |
591 | .B SIGCHLD | |
592 | (or other termination) signal; | |
593 | nor can the status of such a thread be obtained | |
594 | using | |
595 | .BR wait (2). | |
596 | (The thread is said to be | |
597 | .IR detached .) | |
598 | ||
e2fbf61d MK |
599 | After all of the threads in a thread group terminate |
600 | the parent process of the thread group is sent a | |
fd8a5be4 MK |
601 | .B SIGCHLD |
602 | (or other termination) signal. | |
603 | ||
604 | If any of the threads in a thread group performs an | |
605 | .BR execve (2), | |
606 | then all threads other than the thread group leader are terminated, | |
607 | and the new program is executed in the thread group leader. | |
608 | ||
f7110f60 MK |
609 | If one of the threads in a thread group creates a child using |
610 | .BR fork (2), | |
611 | then any thread in the group can | |
612 | .BR wait (2) | |
613 | for that child. | |
614 | ||
edcc65ff | 615 | Since Linux 2.5.35, |
fd8a5be4 MK |
616 | .I flags |
617 | must also include | |
618 | .B CLONE_SIGHAND | |
619 | if | |
620 | .B CLONE_THREAD | |
621 | is specified. | |
e2fbf61d MK |
622 | |
623 | Signals may be sent to a thread group as a whole (i.e., a TGID) using | |
624 | .BR kill (2), | |
625 | or to a specific thread (i.e., TID) using | |
626 | .BR tgkill (2). | |
627 | ||
628 | Signal dispositions and actions are process-wide: | |
629 | if an unhandled signal is delivered to a thread, then | |
630 | it will affect (terminate, stop, continue, be ignored in) | |
631 | all members of the thread group. | |
632 | ||
99408a60 | 633 | Each thread has its own signal mask, as set by |
e2fbf61d | 634 | .BR sigprocmask (2), |
82a06020 | 635 | but signals can be pending either: for the whole process |
e2fbf61d MK |
636 | (i.e., deliverable to any member of the thread group), |
637 | when sent with | |
82a06020 | 638 | .BR kill (2); |
e2fbf61d MK |
639 | or for an individual thread, when sent with |
640 | .BR tgkill (2). | |
99408a60 MK |
641 | A call to |
642 | .BR sigpending (2) | |
643 | returns a signal set that is the union of the signals pending for the | |
644 | whole process and the signals that are pending for the calling thread. | |
e2fbf61d | 645 | |
c13182ef | 646 | If |
e2fbf61d MK |
647 | .BR kill (2) |
648 | is used to send a signal to a thread group, | |
649 | and the thread group has installed a handler for the signal, then | |
650 | the handler will be invoked in exactly one, arbitrarily selected | |
651 | member of the thread group that has not blocked the signal. | |
c13182ef | 652 | If multiple threads in a group are waiting to accept the same signal using |
e2fbf61d MK |
653 | .BR sigwaitinfo (2), |
654 | the kernel will arbitrarily select one of these threads | |
c13182ef | 655 | to receive a signal sent using |
e2fbf61d | 656 | .BR kill (2). |
a69b6bda | 657 | .TP |
f5dbc7c8 | 658 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" |
a69b6bda | 659 | If |
f5dbc7c8 MK |
660 | .B CLONE_UNTRACED |
661 | is specified, then a tracing process cannot force | |
662 | .B CLONE_PTRACE | |
663 | on this child process. | |
fea681da | 664 | .TP |
f5dbc7c8 MK |
665 | .B CLONE_VFORK |
666 | If | |
667 | .B CLONE_VFORK | |
668 | is set, the execution of the calling process is suspended | |
669 | until the child releases its virtual memory | |
670 | resources via a call to | |
671 | .BR execve (2) | |
672 | or | |
673 | .BR _exit (2) | |
674 | (as with | |
675 | .BR vfork (2)). | |
676 | ||
677 | If | |
678 | .B CLONE_VFORK | |
679 | is not set then both the calling process and the child are schedulable | |
680 | after the call, and an application should not rely on execution occurring | |
681 | in any particular order. | |
fea681da | 682 | .TP |
f5dbc7c8 MK |
683 | .B CLONE_VM |
684 | If | |
685 | .B CLONE_VM | |
686 | is set, the calling process and the child process run in the same memory | |
687 | space. | |
688 | In particular, memory writes performed by the calling process | |
689 | or by the child process are also visible in the other process. | |
690 | Moreover, any memory mapping or unmapping performed with | |
691 | .BR mmap (2) | |
692 | or | |
693 | .BR munmap (2) | |
694 | by the child or calling process also affects the other process. | |
695 | ||
696 | If | |
697 | .B CLONE_VM | |
698 | is not set, the child process runs in a separate copy of the memory | |
699 | space of the calling process at the time of | |
700 | .BR clone (). | |
701 | Memory writes or file mappings/unmappings performed by one of the | |
702 | processes do not affect the other, as with | |
703 | .BR fork (2). | |
fea681da MK |
704 | .SS "sys_clone" |
705 | The | |
706 | .B sys_clone | |
707 | system call corresponds more closely to | |
708 | .BR fork (2) | |
709 | in that execution in the child continues from the point of the | |
c13182ef MK |
710 | call. |
711 | Thus, | |
fea681da MK |
712 | .B sys_clone |
713 | only requires the | |
714 | .I flags | |
c13182ef | 715 | and |
fea681da | 716 | .I child_stack |
c13182ef MK |
717 | arguments, 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 | 722 | Another difference for |
fea681da MK |
723 | .B sys_clone |
724 | is that the | |
725 | .I child_stack | |
c13182ef | 726 | argument may be zero, in which case copy-on-write semantics ensure that the |
fea681da | 727 | child gets separate copies of stack pages when either process modifies |
c13182ef MK |
728 | the stack. |
729 | In this case, for correct operation, the | |
fea681da MK |
730 | .B CLONE_VM |
731 | option should not be specified. | |
732 | ||
c4bb193f MK |
733 | Since Linux 2.5.49 the system call has five arguments. |
734 | The two new arguments are | |
fea681da MK |
735 | .I parent_tidptr |
736 | which points to the location (in parent and child memory) where | |
682edefb MK |
737 | the child thread ID will be written in case |
738 | .B CLONE_PARENT_SETTID | |
fea681da MK |
739 | was specified, and |
740 | .I child_tidptr | |
741 | which points to the location (in child memory) where the child thread ID | |
682edefb MK |
742 | will be written in case |
743 | .B CLONE_CHILD_SETTID | |
744 | was specified. | |
fea681da | 745 | .SH "RETURN VALUE" |
0bfa087b MK |
746 | .\" gettid(2) returns current->pid; |
747 | .\" getpid(2) returns current->tgid; | |
fea681da | 748 | On success, the thread ID of the child process is returned |
c13182ef | 749 | in the caller's thread of execution. |
84811e86 | 750 | On failure, \-1 is returned |
fea681da MK |
751 | in the caller's context, no child process will be created, and |
752 | .I errno | |
753 | will be set appropriately. | |
fea681da MK |
754 | .SH ERRORS |
755 | .TP | |
756 | .B EAGAIN | |
757 | Too many processes are already running. | |
758 | .TP | |
759 | .B EINVAL | |
760 | .B CLONE_SIGHAND | |
761 | was specified, but | |
762 | .B CLONE_VM | |
2e8a7fb3 MK |
763 | was not. |
764 | (Since Linux 2.6.0-test6.) | |
fea681da MK |
765 | .TP |
766 | .B EINVAL | |
767 | .B CLONE_THREAD | |
768 | was specified, but | |
769 | .B CLONE_SIGHAND | |
6387216b MK |
770 | was 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 | |
782 | Both | |
783 | .B CLONE_FS | |
784 | and | |
785 | .B CLONE_NEWNS | |
786 | were specified in | |
787 | .IR flags . | |
788 | .TP | |
789 | .B EINVAL | |
82ee147a | 790 | Both |
667417b3 MK |
791 | .B CLONE_NEWIPC |
792 | and | |
793 | .B CLONE_SYSVSEM | |
794 | were specified in | |
795 | .IR flags . | |
796 | .TP | |
797 | .B EINVAL | |
798 | Both | |
82ee147a MK |
799 | .BR CLONE_NEWPID |
800 | and | |
801 | .BR CLONE_THREAD | |
802 | were specified in | |
803 | .IR flags . | |
804 | .TP | |
805 | .B EINVAL | |
c13182ef | 806 | Returned by |
edcc65ff | 807 | .BR clone () |
c13182ef | 808 | when a zero value is specified for |
fea681da MK |
809 | .IR child_stack . |
810 | .TP | |
28cad2c1 | 811 | .B EINVAL |
667417b3 MK |
812 | .BR CLONE_NEWIPC |
813 | was specified in | |
814 | .IR flags , | |
815 | but the kernel was not configured with the | |
816 | .B CONFIG_SYSVIPC | |
817 | and | |
818 | .BR CONFIG_IPC_NS | |
819 | options. | |
820 | .TP | |
821 | .B EINVAL | |
28cad2c1 MK |
822 | .BR CLONE_NEWPID |
823 | was specified in | |
824 | .IR flags , | |
825 | but the kernel was not configured with the | |
826 | .B CONFIG_PID_NS | |
827 | option. | |
828 | .TP | |
43ce9dda MK |
829 | .B EINVAL |
830 | .BR CLONE_NEWUTS | |
831 | was specified in | |
832 | .IR flags , | |
833 | but the kernel was not configured with the | |
834 | .B CONFIG_UTS | |
835 | option. | |
836 | .TP | |
fea681da MK |
837 | .B ENOMEM |
838 | Cannot allocate sufficient memory to allocate a task structure for the | |
839 | child, or to copy those parts of the caller's context that need to be | |
840 | copied. | |
841 | .TP | |
842 | .B EPERM | |
667417b3 | 843 | .BR CLONE_NEWIPC , |
43ce9dda MK |
844 | .BR CLONE_NEWNS , |
845 | .BR CLONE_NEWPID , | |
82ee147a | 846 | or |
43ce9dda | 847 | .BR CLONE_NEWUTS |
0b9bdf82 | 848 | was specified by a non-root process (process without \fBCAP_SYS_ADMIN\fP). |
fea681da MK |
849 | .TP |
850 | .B EPERM | |
851 | .B CLONE_PID | |
852 | was specified by a process other than process 0. | |
a759cc87 | 853 | .SH VERSIONS |
fea681da | 854 | There is no entry for |
edcc65ff | 855 | .BR clone () |
a759cc87 MK |
856 | in libc5. |
857 | glibc2 provides | |
edcc65ff | 858 | .BR clone () |
fea681da | 859 | as described in this manual page. |
a1d5f77c MK |
860 | .SH "CONFORMING TO" |
861 | The | |
862 | .BR clone () | |
863 | and | |
864 | .B sys_clone | |
8382f16d | 865 | calls are Linux-specific and should not be used in programs |
a1d5f77c | 866 | intended to be portable. |
fea681da | 867 | .SH NOTES |
fd8a5be4 MK |
868 | In the kernel 2.4.x series, |
869 | .B CLONE_THREAD | |
870 | generally does not make the parent of the new thread the same | |
871 | as the parent of the calling process. | |
872 | However, for kernel versions 2.4.7 to 2.4.18 the | |
873 | .B CLONE_THREAD | |
874 | flag implied the | |
c13182ef | 875 | .B CLONE_PARENT |
fd8a5be4 | 876 | flag (as in kernel 2.6). |
fea681da | 877 | |
c13182ef MK |
878 | For a while there was |
879 | .B CLONE_DETACHED | |
a5053dcb | 880 | (introduced in 2.5.32): |
c13182ef | 881 | parent wants no child-exit signal. |
a5053dcb | 882 | In 2.6.2 the need to give this |
c13182ef MK |
883 | together with |
884 | .B CLONE_THREAD | |
a5053dcb MK |
885 | disappeared. |
886 | This flag is still defined, but has no effect. | |
887 | ||
34ccb744 | 888 | On i386, |
a5a997ca MK |
889 | .BR clone () |
890 | should not be called through vsyscall, but directly through | |
891 | .IR "int $0x80" . | |
ff929e3b | 892 | |
22399250 | 893 | On 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 | |
903 | The | |
9b0e0996 | 904 | .BR __clone2 () |
c13182ef | 905 | system call operates in the same way as |
ff929e3b MK |
906 | .BR clone (), |
907 | except that | |
908 | .I child_stack_base | |
909 | points to the lowest address of the child's stack area, | |
910 | and | |
911 | .I stack_size | |
912 | specifies the size of the stack pointed to by | |
913 | .IR child_stack_base . | |
31830ef0 MK |
914 | .SH BUGS |
915 | Versions of the GNU C library that include the NPTL threading library | |
c13182ef | 916 | contain a wrapper function for |
0bfa087b | 917 | .BR getpid (2) |
31830ef0 | 918 | that performs caching of PIDs. |
c60237c9 MK |
919 | This caching relies on support in the glibc wrapper for |
920 | .BR clone (), | |
921 | but as currently implemented, | |
922 | the cache may not be up to date in some circumstances. | |
923 | In particular, | |
924 | if a signal is delivered to the child immediately after the | |
925 | .BR clone () | |
926 | call, then a call to | |
927 | .BR getpid () | |
928 | in a handler for the signal may return the PID | |
929 | of the calling process ("the parent"), | |
88619baf | 930 | if the clone wrapper has not yet had a chance to update the PID |
c60237c9 MK |
931 | cache in the child. |
932 | (This discussion ignores the case where the child was created using | |
9291ce36 | 933 | .BR CLONE_THREAD , |
c60237c9 MK |
934 | when |
935 | .BR getpid () | |
936 | .I should | |
937 | return the same value in the child and in the process that called | |
938 | .BR clone (), | |
a1d48abb | 939 | since the caller and the child are in the same thread group. |
e7d807b7 | 940 | The stale-cache problem also does not occur if the |
a1d48abb JR |
941 | .I flags |
942 | argument includes | |
943 | .BR CLONE_VM .) | |
c60237c9 | 944 | To 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) |