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