]>
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. |
305a0578 | 22 | .\" Modified 26 Jun 2001 by Michael Kerrisk <mtk-manpages@gmx.net> |
d9bfdb9c | 23 | .\" Updated notes for 2.4.7+ behavior of CLONE_THREAD |
305a0578 | 24 | .\" Modified 15 Oct 2002 by Michael Kerrisk <mtk-manpages@gmx.net> |
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 | |
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. | |
fea681da | 34 | .\" |
360ed6b3 | 35 | .\" FIXME Document CLONE_NEWIPC, which is new in 2.6.18 |
185341d4 | 36 | .\" (also supported for unshare()?) |
360ed6b3 | 37 | .\" FIXME Document CLONE_NEWUTS, which is new in 2.6.19 |
185341d4 MK |
38 | .\" (also supported for unshare()?) |
39 | .\" FIXME Document CLONE_NEWUSER, which is new in 2.6.23 | |
40 | .\" (also supported for unshare()?) | |
360ed6b3 | 41 | .\" |
cc4615cc | 42 | .TH CLONE 2 2007-07-26 "Linux" "Linux Programmer's Manual" |
fea681da | 43 | .SH NAME |
9b0e0996 | 44 | clone, __clone2 \- create a child process |
fea681da | 45 | .SH SYNOPSIS |
c10859eb | 46 | .nf |
cc4615cc MK |
47 | .B #define _GNU_SOURCE |
48 | .\" Actually _BSD_SOURCE || _SVID_SOURCE | |
49 | .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749 | |
fea681da | 50 | .B #include <sched.h> |
c10859eb | 51 | |
ff929e3b MK |
52 | .BI "int clone(int (*" "fn" ")(void *), void *" child_stack , |
53 | .BI " int " flags ", void *" "arg" ", ... " | |
54 | .BI " /* pid_t *" pid ", struct user_desc *" tls \ | |
55 | ", pid_t *" ctid " */ );" | |
c10859eb | 56 | .fi |
fea681da | 57 | .SH DESCRIPTION |
edcc65ff MK |
58 | .BR clone () |
59 | creates a new process, in a manner similar to | |
fea681da | 60 | .BR fork (2). |
735f354f | 61 | It is actually a library function layered on top of the underlying |
e511ffb6 | 62 | .BR clone () |
fea681da MK |
63 | system call, hereinafter referred to as |
64 | .BR sys_clone . | |
65 | A description of | |
66 | .BR sys_clone | |
67 | is given towards the end of this page. | |
68 | ||
69 | Unlike | |
70 | .BR fork (2), | |
c13182ef | 71 | these calls |
fea681da MK |
72 | allow the child process to share parts of its execution context with |
73 | the calling process, such as the memory space, the table of file | |
c13182ef MK |
74 | descriptors, and the table of signal handlers. |
75 | (Note that on this manual | |
76 | page, "calling process" normally corresponds to "parent process". | |
77 | But see the description of | |
78 | .B CLONE_PARENT | |
fea681da MK |
79 | below.) |
80 | ||
81 | The main use of | |
edcc65ff | 82 | .BR clone () |
fea681da MK |
83 | is to implement threads: multiple threads of control in a program that |
84 | run concurrently in a shared memory space. | |
85 | ||
86 | When the child process is created with | |
c13182ef | 87 | .BR clone (), |
fea681da MK |
88 | it executes the function |
89 | application | |
c13182ef | 90 | .IR fn ( arg ). |
fea681da | 91 | (This differs from |
c13182ef | 92 | .BR fork (2), |
fea681da | 93 | where execution continues in the child from the point |
c13182ef MK |
94 | of the |
95 | .BR fork (2) | |
fea681da MK |
96 | call.) |
97 | The | |
98 | .I fn | |
99 | argument is a pointer to a function that is called by the child | |
100 | process at the beginning of its execution. | |
101 | The | |
102 | .I arg | |
103 | argument is passed to the | |
104 | .I fn | |
105 | function. | |
106 | ||
c13182ef | 107 | When the |
fea681da | 108 | .IR fn ( arg ) |
c13182ef MK |
109 | function application returns, the child process terminates. |
110 | The integer returned by | |
fea681da | 111 | .I fn |
c13182ef MK |
112 | is the exit code for the child process. |
113 | The child process may also terminate explicitly by calling | |
fea681da MK |
114 | .BR exit (2) |
115 | or after receiving a fatal signal. | |
116 | ||
117 | The | |
118 | .I child_stack | |
c13182ef MK |
119 | argument specifies the location of the stack used by the child process. |
120 | Since the child and calling process may share memory, | |
fea681da | 121 | it is not possible for the child process to execute in the |
c13182ef MK |
122 | same stack as the calling process. |
123 | The calling process must therefore | |
fea681da MK |
124 | set up memory space for the child stack and pass a pointer to this |
125 | space to | |
edcc65ff | 126 | .BR clone (). |
fea681da MK |
127 | Stacks grow downwards on all processors that run Linux |
128 | (except the HP PA processors), so | |
129 | .I child_stack | |
130 | usually points to the topmost address of the memory space set up for | |
131 | the child stack. | |
132 | ||
133 | The low byte of | |
134 | .I flags | |
fd8a5be4 MK |
135 | contains the number of the |
136 | .I "termination signal" | |
137 | sent to the parent when the child dies. | |
138 | If this signal is specified as anything other than | |
fea681da MK |
139 | .BR SIGCHLD , |
140 | then the parent process must specify the | |
c13182ef MK |
141 | .B __WALL |
142 | or | |
fea681da | 143 | .B __WCLONE |
c13182ef MK |
144 | options when waiting for the child with |
145 | .BR wait (2). | |
fea681da MK |
146 | If no signal is specified, then the parent process is not signaled |
147 | when the child terminates. | |
148 | ||
149 | .I flags | |
fd8a5be4 MK |
150 | may also be bitwise-or'ed with zero or more of the following constants, |
151 | in order to specify what is shared between the calling process | |
fea681da | 152 | and the child process: |
fea681da MK |
153 | .TP |
154 | .BR CLONE_PARENT " (since Linux 2.3.12)" | |
155 | If | |
156 | .B CLONE_PARENT | |
157 | is set, then the parent of the new child (as returned by | |
158 | .BR getppid (2)) | |
159 | will be the same as that of the calling process. | |
160 | ||
161 | If | |
162 | .B CLONE_PARENT | |
163 | is not set, then (as with | |
164 | .BR fork (2)) | |
165 | the child's parent is the calling process. | |
166 | ||
c13182ef | 167 | Note that it is the parent process, as returned by |
fea681da MK |
168 | .BR getppid (2), |
169 | which is signaled when the child terminates, so that | |
c13182ef | 170 | if |
fea681da | 171 | .B CLONE_PARENT |
c13182ef | 172 | is set, then the parent of the calling process, rather than the |
fea681da | 173 | calling process itself, will be signaled. |
fea681da MK |
174 | .TP |
175 | .B CLONE_FS | |
176 | If | |
177 | .B CLONE_FS | |
178 | is set, the caller and the child processes share the same file system | |
c13182ef MK |
179 | information. |
180 | This includes the root of the file system, the current | |
181 | working directory, and the umask. | |
182 | Any call to | |
fea681da MK |
183 | .BR chroot (2), |
184 | .BR chdir (2), | |
185 | or | |
186 | .BR umask (2) | |
edcc65ff | 187 | performed by the calling process or the child process also affects the |
fea681da MK |
188 | other process. |
189 | ||
c13182ef | 190 | If |
fea681da MK |
191 | .B CLONE_FS |
192 | is not set, the child process works on a copy of the file system | |
193 | information of the calling process at the time of the | |
edcc65ff | 194 | .BR clone () |
fea681da MK |
195 | call. |
196 | Calls to | |
197 | .BR chroot (2), | |
198 | .BR chdir (2), | |
199 | .BR umask (2) | |
200 | performed later by one of the processes do not affect the other process. | |
fea681da MK |
201 | .TP |
202 | .B CLONE_FILES | |
203 | If | |
204 | .B CLONE_FILES | |
205 | is set, the calling process and the child processes share the same file | |
edcc65ff | 206 | descriptor table. |
c13182ef | 207 | Any file descriptor created by the calling process or by the child |
edcc65ff MK |
208 | process is also valid in the other process. |
209 | Similarly, if one of the processes closes a file descriptor, | |
210 | or changes its associated flags (using the | |
211 | .BR fcntl (2) | |
212 | .B F_SETFD | |
213 | operation), the other process is also affected. | |
fea681da MK |
214 | |
215 | If | |
216 | .B CLONE_FILES | |
217 | is not set, the child process inherits a copy of all file descriptors | |
218 | opened in the calling process at the time of | |
edcc65ff MK |
219 | .BR clone (). |
220 | (The duplicated file descriptors in the child refer to the | |
c13182ef | 221 | same open file descriptions (see |
edcc65ff MK |
222 | .BR open (2)) |
223 | as the corresponding file descriptors in the calling process.) | |
224 | Subsequent operations that open or close file descriptors, | |
225 | or change file descriptor flags, | |
c13182ef | 226 | performed by either the calling |
edcc65ff | 227 | process or the child process do not affect the other process. |
fea681da | 228 | .TP |
c10859eb | 229 | .BR CLONE_NEWNS " (since Linux 2.4.19)" |
fea681da MK |
230 | Start the child in a new namespace. |
231 | ||
c13182ef MK |
232 | Every process lives in a namespace. |
233 | The | |
fea681da MK |
234 | .I namespace |
235 | of a process is the data (the set of mounts) describing the file hierarchy | |
c13182ef MK |
236 | as seen by that process. |
237 | After a | |
fea681da MK |
238 | .BR fork (2) |
239 | or | |
240 | .BR clone (2) | |
241 | where the | |
242 | .B CLONE_NEWNS | |
243 | flag is not set, the child lives in the same namespace as the parent. | |
244 | The system calls | |
245 | .BR mount (2) | |
246 | and | |
247 | .BR umount (2) | |
248 | change the namespace of the calling process, and hence affect | |
249 | all processes that live in the same namespace, but do not affect | |
250 | processes in a different namespace. | |
251 | ||
252 | After a | |
253 | .BR clone (2) | |
254 | where the | |
255 | .B CLONE_NEWNS | |
256 | flag is set, the cloned child is started in a new namespace, | |
257 | initialized with a copy of the namespace of the parent. | |
258 | ||
0b9bdf82 | 259 | Only a privileged process (one having the \fBCAP_SYS_ADMIN\fP capability) |
fea681da MK |
260 | may specify the |
261 | .B CLONE_NEWNS | |
262 | flag. | |
263 | It is not permitted to specify both | |
264 | .B CLONE_NEWNS | |
265 | and | |
266 | .B CLONE_FS | |
267 | in the same | |
e511ffb6 | 268 | .BR clone () |
fea681da | 269 | call. |
fea681da MK |
270 | .TP |
271 | .B CLONE_SIGHAND | |
272 | If | |
273 | .B CLONE_SIGHAND | |
274 | is set, the calling process and the child processes share the same table of | |
c13182ef MK |
275 | signal handlers. |
276 | If the calling process or child process calls | |
fea681da | 277 | .BR sigaction (2) |
c13182ef MK |
278 | to change the behavior associated with a signal, the behavior is |
279 | changed in the other process as well. | |
280 | However, the calling process and child | |
fea681da | 281 | processes still have distinct signal masks and sets of pending |
c13182ef MK |
282 | signals. |
283 | So, one of them may block or unblock some signals using | |
fea681da MK |
284 | .BR sigprocmask (2) |
285 | without affecting the other process. | |
286 | ||
287 | If | |
288 | .B CLONE_SIGHAND | |
289 | is not set, the child process inherits a copy of the signal handlers | |
290 | of the calling process at the time | |
edcc65ff | 291 | .BR clone () |
c13182ef MK |
292 | is called. |
293 | Calls to | |
fea681da MK |
294 | .BR sigaction (2) |
295 | performed later by one of the processes have no effect on the other | |
296 | process. | |
29546c24 MK |
297 | |
298 | Since Linux 2.6.0-test6, | |
299 | .I flags | |
300 | must also include | |
301 | .B CLONE_VM | |
302 | if | |
303 | .B CLONE_SIGHAND | |
304 | is specified | |
fea681da MK |
305 | .TP |
306 | .B CLONE_PTRACE | |
c13182ef | 307 | If |
fea681da | 308 | .B CLONE_PTRACE |
a69b6bda MK |
309 | is specified, and the calling process is being traced, |
310 | then trace the child also (see | |
fea681da | 311 | .BR ptrace (2)). |
a69b6bda MK |
312 | .TP |
313 | .BR CLONE_UNTRACED " (since Linux 2.5.46)" | |
c13182ef | 314 | If |
a69b6bda MK |
315 | .B CLONE_UNTRACED |
316 | is specified, then a tracing process cannot force | |
317 | .B CLONE_PTRACE | |
318 | on this child process. | |
319 | .TP | |
320 | .BR CLONE_STOPPED " (since Linux 2.6.0-test2)" | |
321 | If | |
322 | .B CLONE_STOPPED | |
323 | is set, then the child is initially stopped (as though it was sent a | |
324 | .B SIGSTOP | |
325 | signal), and must be resumed by sending it a | |
326 | .B SIGCONT | |
327 | signal. | |
328 | .TP | |
fea681da MK |
329 | .B CLONE_VFORK |
330 | If | |
331 | .B CLONE_VFORK | |
332 | is set, the execution of the calling process is suspended | |
333 | until the child releases its virtual memory | |
334 | resources via a call to | |
335 | .BR execve (2) | |
336 | or | |
337 | .BR _exit (2) | |
c13182ef | 338 | (as with |
fea681da MK |
339 | .BR vfork (2)). |
340 | ||
c13182ef | 341 | If |
fea681da MK |
342 | .B CLONE_VFORK |
343 | is not set then both the calling process and the child are schedulable | |
344 | after the call, and an application should not rely on execution occurring | |
345 | in any particular order. | |
fea681da MK |
346 | .TP |
347 | .B CLONE_VM | |
348 | If | |
349 | .B CLONE_VM | |
350 | is set, the calling process and the child processes run in the same memory | |
c13182ef MK |
351 | space. |
352 | In particular, memory writes performed by the calling process | |
fea681da MK |
353 | or by the child process are also visible in the other process. |
354 | Moreover, any memory mapping or unmapping performed with | |
355 | .BR mmap (2) | |
356 | or | |
357 | .BR munmap (2) | |
358 | by the child or calling process also affects the other process. | |
359 | ||
360 | If | |
361 | .B CLONE_VM | |
362 | is not set, the child process runs in a separate copy of the memory | |
363 | space of the calling process at the time of | |
edcc65ff | 364 | .BR clone (). |
fea681da MK |
365 | Memory writes or file mappings/unmappings performed by one of the |
366 | processes do not affect the other, as with | |
367 | .BR fork (2). | |
fea681da MK |
368 | .TP |
369 | .BR CLONE_PID " (obsolete)" | |
370 | If | |
371 | .B CLONE_PID | |
c13182ef MK |
372 | is set, the child process is created with the same process ID as |
373 | the calling process. | |
374 | This is good for hacking the system, but otherwise | |
375 | of not much use. | |
376 | Since 2.3.21 this flag can be | |
fea681da MK |
377 | specified only by the system boot process (PID 0). |
378 | It disappeared in Linux 2.5.16. | |
fea681da MK |
379 | .TP |
380 | .BR CLONE_THREAD " (since Linux 2.4.0-test8)" | |
381 | If | |
382 | .B CLONE_THREAD | |
383 | is set, the child is placed in the same thread group as the calling process. | |
fd8a5be4 MK |
384 | To make the remainder of the discussion of |
385 | .B CLONE_THREAD | |
386 | more readable, the term "thread" is used to refer to the | |
387 | processes within a thread group. | |
fea681da | 388 | |
fd8a5be4 MK |
389 | Thread groups were a feature added in Linux 2.4 to support the |
390 | POSIX threads notion of a set of threads that share a single PID. | |
391 | Internally, this shared PID is the so-called | |
392 | thread group identifier (TGID) for the thread group. | |
c13182ef | 393 | Since Linux 2.4, calls to |
fea681da | 394 | .BR getpid (2) |
fd8a5be4 MK |
395 | return the TGID of the caller. |
396 | ||
397 | The threads within a group can be distinguished by their (system-wide) | |
398 | unique thread IDs (TID). | |
399 | A new thread's TID is available as the function result | |
400 | returned to the caller of | |
401 | .BR clone (), | |
402 | and a thread can obtain | |
403 | its own TID using | |
404 | .BR gettid (2). | |
405 | ||
c13182ef | 406 | When a call is made to |
fd8a5be4 MK |
407 | .BR clone () |
408 | without specifying | |
409 | .BR CLONE_THREAD , | |
410 | then the resulting thread is placed in a new thread group | |
411 | whose TGID is the same as the thread's TID. | |
412 | This thread is the | |
413 | .I leader | |
414 | of the new thread group. | |
415 | ||
416 | A new thread created with | |
417 | .B CLONE_THREAD | |
418 | has the same parent process as the caller of | |
419 | .BR clone () | |
c13182ef | 420 | (i.e., like |
fd8a5be4 MK |
421 | .BR CLONE_PARENT ), |
422 | so that calls to | |
423 | .BR getppid (2) | |
424 | return the same value for all of the threads in a thread group. | |
425 | When a | |
c13182ef | 426 | .B CLONE_THREAD |
fd8a5be4 MK |
427 | thread terminates, the thread that created it using |
428 | .BR clone () | |
429 | is not sent a | |
430 | .B SIGCHLD | |
431 | (or other termination) signal; | |
432 | nor can the status of such a thread be obtained | |
433 | using | |
434 | .BR wait (2). | |
435 | (The thread is said to be | |
436 | .IR detached .) | |
437 | ||
e2fbf61d MK |
438 | After all of the threads in a thread group terminate |
439 | the parent process of the thread group is sent a | |
fd8a5be4 MK |
440 | .B SIGCHLD |
441 | (or other termination) signal. | |
442 | ||
443 | If any of the threads in a thread group performs an | |
444 | .BR execve (2), | |
445 | then all threads other than the thread group leader are terminated, | |
446 | and the new program is executed in the thread group leader. | |
447 | ||
f7110f60 MK |
448 | If one of the threads in a thread group creates a child using |
449 | .BR fork (2), | |
450 | then any thread in the group can | |
451 | .BR wait (2) | |
452 | for that child. | |
453 | ||
edcc65ff | 454 | Since Linux 2.5.35, |
fd8a5be4 MK |
455 | .I flags |
456 | must also include | |
457 | .B CLONE_SIGHAND | |
458 | if | |
459 | .B CLONE_THREAD | |
460 | is specified. | |
e2fbf61d MK |
461 | |
462 | Signals may be sent to a thread group as a whole (i.e., a TGID) using | |
463 | .BR kill (2), | |
464 | or to a specific thread (i.e., TID) using | |
465 | .BR tgkill (2). | |
466 | ||
467 | Signal dispositions and actions are process-wide: | |
468 | if an unhandled signal is delivered to a thread, then | |
469 | it will affect (terminate, stop, continue, be ignored in) | |
470 | all members of the thread group. | |
471 | ||
99408a60 | 472 | Each thread has its own signal mask, as set by |
e2fbf61d | 473 | .BR sigprocmask (2), |
82a06020 | 474 | but signals can be pending either: for the whole process |
e2fbf61d MK |
475 | (i.e., deliverable to any member of the thread group), |
476 | when sent with | |
82a06020 | 477 | .BR kill (2); |
e2fbf61d MK |
478 | or for an individual thread, when sent with |
479 | .BR tgkill (2). | |
99408a60 MK |
480 | A call to |
481 | .BR sigpending (2) | |
482 | returns a signal set that is the union of the signals pending for the | |
483 | whole process and the signals that are pending for the calling thread. | |
e2fbf61d | 484 | |
c13182ef | 485 | If |
e2fbf61d MK |
486 | .BR kill (2) |
487 | is used to send a signal to a thread group, | |
488 | and the thread group has installed a handler for the signal, then | |
489 | the handler will be invoked in exactly one, arbitrarily selected | |
490 | member of the thread group that has not blocked the signal. | |
c13182ef | 491 | If multiple threads in a group are waiting to accept the same signal using |
e2fbf61d MK |
492 | .BR sigwaitinfo (2), |
493 | the kernel will arbitrarily select one of these threads | |
c13182ef | 494 | to receive a signal sent using |
e2fbf61d | 495 | .BR kill (2). |
a69b6bda MK |
496 | .TP |
497 | .BR CLONE_SYSVSEM " (since Linux 2.5.10)" | |
498 | If | |
499 | .B CLONE_SYSVSEM | |
500 | is set, then the child and the calling process share | |
501 | a single list of System V semaphore undo values (see | |
502 | .BR semop (2)). | |
503 | If this flag is not set, then the child has a separate undo list, | |
504 | which is initially empty. | |
fea681da MK |
505 | .TP |
506 | .BR CLONE_SETTLS " (since Linux 2.5.32)" | |
507 | The | |
508 | .I newtls | |
509 | parameter is the new TLS (Thread Local Storage) descriptor. | |
510 | (See | |
511 | .BR set_thread_area (2).) | |
fea681da MK |
512 | .TP |
513 | .BR CLONE_PARENT_SETTID " (since Linux 2.5.49)" | |
514 | Store child thread ID at location | |
515 | .I parent_tidptr | |
516 | in parent and child memory. | |
682edefb MK |
517 | (In Linux 2.5.32-2.5.48 there was a flag |
518 | .B CLONE_SETTID | |
519 | that did this.) | |
fea681da MK |
520 | .TP |
521 | .BR CLONE_CHILD_SETTID " (since Linux 2.5.49)" | |
522 | Store child thread ID at location | |
523 | .I child_tidptr | |
524 | in child memory. | |
fea681da MK |
525 | .TP |
526 | .BR CLONE_CHILD_CLEARTID " (since Linux 2.5.49)" | |
527 | Erase child thread ID at location | |
528 | .I child_tidptr | |
529 | in child memory when the child exits, and do a wakeup on the futex | |
530 | at that address. | |
531 | The address involved may be changed by the | |
532 | .BR set_tid_address (2) | |
c13182ef MK |
533 | system call. |
534 | This is used by threading libraries. | |
fea681da MK |
535 | .SS "sys_clone" |
536 | The | |
537 | .B sys_clone | |
538 | system call corresponds more closely to | |
539 | .BR fork (2) | |
540 | in that execution in the child continues from the point of the | |
c13182ef MK |
541 | call. |
542 | Thus, | |
fea681da MK |
543 | .B sys_clone |
544 | only requires the | |
545 | .I flags | |
c13182ef | 546 | and |
fea681da | 547 | .I child_stack |
c13182ef MK |
548 | arguments, which have the same meaning as for |
549 | .BR clone (). | |
fea681da | 550 | (Note that the order of these arguments differs from |
c13182ef | 551 | .BR clone ().) |
fea681da | 552 | |
c13182ef | 553 | Another difference for |
fea681da MK |
554 | .B sys_clone |
555 | is that the | |
556 | .I child_stack | |
c13182ef | 557 | argument may be zero, in which case copy-on-write semantics ensure that the |
fea681da | 558 | child gets separate copies of stack pages when either process modifies |
c13182ef MK |
559 | the stack. |
560 | In this case, for correct operation, the | |
fea681da MK |
561 | .B CLONE_VM |
562 | option should not be specified. | |
563 | ||
564 | Since Linux 2.5.49 the system call has five parameters. | |
565 | The two new parameters are | |
566 | .I parent_tidptr | |
567 | which points to the location (in parent and child memory) where | |
682edefb MK |
568 | the child thread ID will be written in case |
569 | .B CLONE_PARENT_SETTID | |
fea681da MK |
570 | was specified, and |
571 | .I child_tidptr | |
572 | which points to the location (in child memory) where the child thread ID | |
682edefb MK |
573 | will be written in case |
574 | .B CLONE_CHILD_SETTID | |
575 | was specified. | |
fea681da | 576 | .SH "RETURN VALUE" |
0bfa087b MK |
577 | .\" gettid(2) returns current->pid; |
578 | .\" getpid(2) returns current->tgid; | |
fea681da | 579 | On success, the thread ID of the child process is returned |
c13182ef MK |
580 | in the caller's thread of execution. |
581 | On failure, a \-1 will be returned | |
fea681da MK |
582 | in the caller's context, no child process will be created, and |
583 | .I errno | |
584 | will be set appropriately. | |
fea681da MK |
585 | .SH ERRORS |
586 | .TP | |
587 | .B EAGAIN | |
588 | Too many processes are already running. | |
589 | .TP | |
590 | .B EINVAL | |
591 | .B CLONE_SIGHAND | |
592 | was specified, but | |
593 | .B CLONE_VM | |
2e8a7fb3 MK |
594 | was not. |
595 | (Since Linux 2.6.0-test6.) | |
fea681da MK |
596 | .TP |
597 | .B EINVAL | |
598 | .B CLONE_THREAD | |
599 | was specified, but | |
600 | .B CLONE_SIGHAND | |
601 | was not. (Since Linux 2.5.35.) | |
29546c24 MK |
602 | .\" .TP |
603 | .\" .B EINVAL | |
604 | .\" Precisely one of | |
605 | .\" .B CLONE_DETACHED | |
606 | .\" and | |
607 | .\" .B CLONE_THREAD | |
608 | .\" was specified. (Since Linux 2.6.0-test6.) | |
fea681da MK |
609 | .TP |
610 | .B EINVAL | |
611 | Both | |
612 | .B CLONE_FS | |
613 | and | |
614 | .B CLONE_NEWNS | |
615 | were specified in | |
616 | .IR flags . | |
617 | .TP | |
618 | .B EINVAL | |
c13182ef | 619 | Returned by |
edcc65ff | 620 | .BR clone () |
c13182ef | 621 | when a zero value is specified for |
fea681da MK |
622 | .IR child_stack . |
623 | .TP | |
624 | .B ENOMEM | |
625 | Cannot allocate sufficient memory to allocate a task structure for the | |
626 | child, or to copy those parts of the caller's context that need to be | |
627 | copied. | |
628 | .TP | |
629 | .B EPERM | |
630 | .B CLONE_NEWNS | |
0b9bdf82 | 631 | was specified by a non-root process (process without \fBCAP_SYS_ADMIN\fP). |
fea681da MK |
632 | .TP |
633 | .B EPERM | |
634 | .B CLONE_PID | |
635 | was specified by a process other than process 0. | |
a759cc87 | 636 | .SH VERSIONS |
fea681da | 637 | There is no entry for |
edcc65ff | 638 | .BR clone () |
a759cc87 MK |
639 | in libc5. |
640 | glibc2 provides | |
edcc65ff | 641 | .BR clone () |
fea681da | 642 | as described in this manual page. |
a1d5f77c MK |
643 | .SH "CONFORMING TO" |
644 | The | |
645 | .BR clone () | |
646 | and | |
647 | .B sys_clone | |
648 | calls are Linux specific and should not be used in programs | |
649 | intended to be portable. | |
fea681da | 650 | .SH NOTES |
fd8a5be4 MK |
651 | In the kernel 2.4.x series, |
652 | .B CLONE_THREAD | |
653 | generally does not make the parent of the new thread the same | |
654 | as the parent of the calling process. | |
655 | However, for kernel versions 2.4.7 to 2.4.18 the | |
656 | .B CLONE_THREAD | |
657 | flag implied the | |
c13182ef | 658 | .B CLONE_PARENT |
fd8a5be4 | 659 | flag (as in kernel 2.6). |
fea681da | 660 | |
c13182ef MK |
661 | For a while there was |
662 | .B CLONE_DETACHED | |
a5053dcb | 663 | (introduced in 2.5.32): |
c13182ef | 664 | parent wants no child-exit signal. |
a5053dcb | 665 | In 2.6.2 the need to give this |
c13182ef MK |
666 | together with |
667 | .B CLONE_THREAD | |
a5053dcb MK |
668 | disappeared. |
669 | This flag is still defined, but has no effect. | |
670 | ||
a5a997ca MK |
671 | On x86, |
672 | .BR clone () | |
673 | should not be called through vsyscall, but directly through | |
674 | .IR "int $0x80" . | |
ff929e3b | 675 | |
22399250 | 676 | On ia64, a different system call is used: |
ff929e3b MK |
677 | .nf |
678 | ||
9b0e0996 MK |
679 | .BI "int __clone2(int (*" "fn" ")(void *), " |
680 | .BI " void *" child_stack_base ", size_t " stack_size , | |
681 | .BI " int " flags ", void *" "arg" ", ... " | |
682 | .BI " /* pid_t *" pid ", struct user_desc *" tls \ | |
ff929e3b MK |
683 | ", pid_t *" ctid " */ );" |
684 | .fi | |
685 | .PP | |
686 | The | |
9b0e0996 | 687 | .BR __clone2 () |
c13182ef | 688 | system call operates in the same way as |
ff929e3b MK |
689 | .BR clone (), |
690 | except that | |
691 | .I child_stack_base | |
692 | points to the lowest address of the child's stack area, | |
693 | and | |
694 | .I stack_size | |
695 | specifies the size of the stack pointed to by | |
696 | .IR child_stack_base . | |
31830ef0 MK |
697 | .SH BUGS |
698 | Versions of the GNU C library that include the NPTL threading library | |
c13182ef | 699 | contain a wrapper function for |
0bfa087b | 700 | .BR getpid (2) |
31830ef0 | 701 | that performs caching of PIDs. |
c13182ef | 702 | In programs linked against such libraries, calls to |
0bfa087b | 703 | .BR getpid (2) |
31830ef0 MK |
704 | may return the same value, even when the threads were not created using |
705 | .B CLONE_THREAD | |
706 | (and thus are not in the same thread group). | |
707 | To get the truth, it may be necessary to use code such as the following | |
708 | .nf | |
709 | ||
710 | #include <syscall.h> | |
711 | ||
712 | pid_t mypid; | |
713 | ||
714 | mypid = syscall(SYS_getpid); | |
715 | .fi | |
fea681da MK |
716 | .SH "SEE ALSO" |
717 | .BR fork (2), | |
2b44301c | 718 | .BR futex (2), |
fea681da MK |
719 | .BR getpid (2), |
720 | .BR gettid (2), | |
f2d0bbf1 | 721 | .BR set_thread_area (2), |
2b44301c | 722 | .BR set_tid_address (2), |
f2d0bbf1 | 723 | .BR tkill (2), |
5cc01e9c | 724 | .BR unshare (2), |
fea681da | 725 | .BR wait (2), |
3616b7c0 MK |
726 | .BR capabilities (7), |
727 | .BR pthreads (7) |