clone, __clone2 - 创建一个子进程
内容简介
#includeint clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ... /* pid_t *pid, struct user_desc *tls ", pid_t *" ctid " */ )" int __clone2(int (*fn)(void *), void *child_stack_base, size_t stack_size, int flags, void *arg, ... /* pid_t *pid, struct user_desc *tls ", pid_t *" ctid " */ )" #include <sched.h>
描述
clone() creates a new process, in a manner similar to%uA0fork(2). It is actually a library function layered on top of the underlying%uA0clone() system call, hereinafter referred to assys_clone. A description of%uA0sys_clone%uA0is given towards the end of this page.
Unlike%uA0fork(2), these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. (Note that on this manual page, "calling process" normally corresponds to "parent process". But see the description of%uA0CLONE_PARENTꂾlow.)
The main use of%uA0clone() is to implement threads: multiple threads of control in a program that run concurrently in a shared memory space.
When the child process is created with%uA0clone(), it executes the function applicationfn(arg). (This differs from%uA0fork(2), where execution continues in the child from the point of the%uA0fork(2) call.) The%uA0fn%uA0argument is a pointer to a function that is called by the child process at the beginning of its execution. The%uA0arg%uA0argument is passed to the%uA0fn%uA0function.
When the%uA0fn(arg) function application returns, the child process terminates. The integer returned by%uA0fn%uA0is the exit code for the child process. The child process may also terminate explicitly by calling%uA0exit(2) or after receiving a fatal signal.
The%uA0child_stack%uA0argument specifies the location of the stack used by the child process. Since the child and calling process may share memory, it is not possible for the child process to execute in the same stack as the calling process. The calling process must therefore set up memory space for the child stack and pass a pointer to this space toclone(). Stacks grow downwards on all processors that run Linux (except the HP PA processors), so%uA0child_stack%uA0usually points to the topmost address of the memory space set up for the child stack.
The low byte of%uA0flags%uA0contains the number of the%uA0termination signal%uA0sent to the parent when the child dies. If this signal is specified as anything other than%uA0SIGCHLD, then the parent process must specify the%uA0__WALL%uA0or%uA0__WCLONE%uA0options when waiting for the child with%uA0wait(2). If no signal is specified, then the parent process is not signaled when the child terminates.
.flags%uA0may also be bitwise-or’ed with zero or more of the following constants, in order to specify what is shared between the calling process and the child process:
标签 | 描述 |
---|---|
CLONE_PARENT%uA0(since Linux 2.3.12) |
If%uA0CLONE_PARENT%uA0is set, then the parent of the new child (as returned by%uA0getppid(2)) will be the same as that of the calling process.
If%uA0CLONE_PARENT%uA0is not set, then (as with%uA0fork(2)) the child’s parent is the calling process. Note that it is the parent process, as returned bygetppid(2), which is signaled when the child terminates, so that if%uA0CLONE_PARENT%uA0is set, then the parent of the calling process, rather than the calling process itself, will be signaled. |
CLONE_FS |
If%uA0CLONE_FS%uA0is set, the caller and the child processes share the same file system information. This includes the root of the file system, the current working directory, and the umask. Any call to%uA0chroot(2),%uA0chdir(2), or%uA0umask(2) performed by the calling process or the child process also affects the other process.
If%uA0CLONE_FS%uA0is not set, the child process works on a copy of the file system information of the calling process at the time of the%uA0clone() call. Calls to%uA0chroot(2),chdir(2),%uA0umask(2) performed later by one of the processes do not affect the other process. |
CLONE_FILES |
If%uA0CLONE_FILES%uA0is set, the calling process and the child processes share the same file descriptor table. Any file descriptor created by the calling process or by the child process is also valid in the other process. Similarly, if one of the processes closes a file descriptor, or changes its associated flags (using the%uA0fcntl(2)%uA0F_SETFD%uA0operation), the other process is also affected.
If%uA0CLONE_FILES%uA0is not set, the child process inherits a copy of all file descriptors opened in the calling process at the time of%uA0clone(). (The duplicated file descriptors in the child refer to the same open file descriptions (seeopen(2)) as the corresponding file descriptors in the calling process.) Subsequent operations that open or close file descriptors, or change file descriptor flags, performed by either the calling process or the child process do not affect the other process. |
CLONE_NEWNS%uA0(since Linux 2.4.19) |
Start the child in a new namespace.
Every process lives in a namespace. The%uA0namespace%uA0of a process is the data (the set of mounts) describing the file hierarchy as seen by that process. After a%uA0fork(2) orclone(2) where the%uA0CLONE_NEWNS%uA0flag is not set, the child lives in the same namespace as the parent. The system calls%uA0mount(2) and%uA0umount(2) change the namespace of the calling process, and hence affect all processes that live in the same namespace, but do not affect processes in a different namespace. After a%uA0clone(2) where the%uA0CLONE_NEWNS%uA0flag is set, the cloned child is started in a new namespace, initialized with a copy of the namespace of the parent. Only a privileged process (one having the CAP_SYS_ADMIN capability) may specify theCLONE_NEWNS%uA0flag. It is not permitted to specify bothCLONE_NEWNS%uA0and%uA0CLONE_FS%uA0in the same%uA0clone() call. |
CLONE_SIGHAND |
If%uA0CLONE_SIGHAND%uA0is set, the calling process and the child processes share the same table of signal handlers. If the calling process or child process calls%uA0sigaction(2) to change the behavior associated with a signal, the behavior is changed in the other process as well. However, the calling process and child processes still have distinct signal masks and sets of pending signals. So, one of them may block or unblock some signals usingsigprocmask(2) without affecting the other process.
If%uA0CLONE_SIGHAND%uA0is not set, the child process inherits a copy of the signal handlers of the calling process at the time%uA0clone() is called. Calls to%uA0sigaction(2) performed later by one of the processes have no effect on the other process. Since Linux 2.6.0-test6,%uA0flags%uA0must also includeCLONE_VM%uA0if%uA0CLONE_SIGHAND%uA0is specified |
CLONE_PTRACE | If%uA0CLONE_PTRACE%uA0is specified, and the calling process is being traced, then trace the child also (see%uA0ptrace(2)). |
CLONE_UNTRACED%uA0(since Linux 2.5.46) | If%uA0CLONE_UNTRACED%uA0is specified, then a tracing process cannot force%uA0CLONE_PTRACE%uA0on this child process. |
CLONE_STOPPED%uA0(since Linux 2.6.0-test2) | If%uA0CLONE_STOPPED%uA0is set, then the child is initially stopped (as though it was sent a%uA0SIGSTOP%uA0signal), and must be resumed by sending it a%uA0SIGCONT%uA0signal. |
CLONE_VFORK |
If%uA0CLONE_VFORK%uA0is set, the execution of the calling process is suspended until the child releases its virtual memory resources via a call to%uA0execve(2) or%uA0_exit(2) (as with%uA0vfork(2)).
If%uA0CLONE_VFORK%uA0is not set then both the calling process and the child are schedulable after the call, and an application should not rely on execution occurring in any particular order. |
CLONE_VM |
If%uA0CLONE_VM%uA0is set, the calling process and the child processes run in the same memory space. In particular, memory writes performed by the calling process or by the child process are also visible in the other process. Moreover, any memory mapping or unmapping performed with%uA0mmap(2) or%uA0munmap(2) by the child or calling process also affects the other process.
If%uA0CLONE_VM%uA0is not set, the child process runs in a separate copy of the memory space of the calling process at the time of%uA0clone(). Memory writes or file mappings/unmappings performed by one of the processes do not affect the other, as with%uA0fork(2). |
CLONE_PID%uA0(obsolete) | If%uA0CLONE_PID%uA0is set, the child process is created with the same process ID as the calling process. This is good for hacking the system, but otherwise of not much use. Since 2.3.21 this flag can be specified only by the system boot process (PID 0). It disappeared in Linux 2.5.16. |
CLONE_THREAD%uA0(since Linux 2.4.0-test8) |
If%uA0CLONE_THREAD%uA0is set, the child is placed in the same thread group as the calling process. To make the remainder of the discussion of%uA0CLONE_THREAD%uA0more readable, the term "thread" is used to refer to the processes within a thread group.
Thread groups were a feature added in Linux 2.4 to support the POSIX threads notion of a set of threads that share a single PID. Internally, this shared PID is the so-called thread group identifier (TGID) for the thread group. Since Linux 2.4, calls to%uA0getpid(2) return the TGID of the caller. The threads within a group can be distinguished by their (system-wide) unique thread IDs (TID). A new thread’s TID is available as the function result returned to the caller of%uA0clone(), and a thread can obtain its own TID using%uA0gettid(2). When a call is made to%uA0clone() without specifyingCLONE_THREAD, then the resulting thread is placed in a new thread group whose TGID is the same as the thread’s TID. This thread is the%uA0leader%uA0of the new thread group. A new thread created with%uA0CLONE_THREAD%uA0has the same parent process as the caller of%uA0clone() (i.e., likeCLONE_PARENT), so that calls to%uA0getppid(2) return the same value for all of the threads in a thread group. When a%uA0CLONE_THREAD%uA0thread terminates, the thread that created it using%uA0clone() is not sent a%uA0SIGCHLD%uA0(or other termination) signal nor can the status of such a thread be obtained using%uA0wait(2). (The thread is said to be%uA0detached.) After all of the threads in a thread group terminate the parent process of the thread group is sent a%uA0SIGCHLD(or other termination) signal. If any of the threads in a thread group performs anexecve(2), then all threads other than the thread group leader are terminated, and the new program is executed in the thread group leader. If one of the threads in a thread group creates a child using%uA0fork(2), then any thread in the group can%uA0wait(2) for that child. Since Linux 2.5.35,%uA0flags%uA0must also includeCLONE_SIGHAND%uA0if%uA0CLONE_THREAD%uA0is specified. Signals may be sent to a thread group as a whole (i.e., a TGID) using%uA0kill(2), or to a specific thread (i.e., TID) usingtgkill(2). Signal dispositions and actions are process-wide: if an unhandled signal is delivered to a thread, then it will affect (terminate, stop, continue, be ignored in) all members of the thread group. Each thread has its own signal mask, as set bysigprocmask(2), but signals can be pending either: for the whole process (i.e., deliverable to any member of the thread group), when sent with%uA0kill(2) or for an individual thread, when sent with%uA0tgkill(2). A call tosigpending(2) returns a signal set that is the union of the signals pending for the whole process and the signals that are pending for the calling thread. If%uA0kill(2) is used to send a signal to a thread group, and the thread group has installed a handler for the signal, then the handler will be invoked in exactly one, arbitrarily selected member of the thread group that has not blocked the signal. If multiple threads in a group are waiting to accept the same signal using%uA0sigwaitinfo(2), the kernel will arbitrarily select one of these threads to receive a signal sent using%uA0kill(2). |
CLONE_SYSVSEM%uA0(since Linux 2.5.10) | If%uA0CLONE_SYSVSEM%uA0is set, then the child and the calling process share a single list of System V semaphore undo values (see%uA0semop(2)). If this flag is not set, then the child has a separate undo list, which is initially empty. |
CLONE_SETTLS%uA0(since Linux 2.5.32) | The%uA0newtls%uA0parameter is the new TLS (Thread Local Storage) descriptor. (See%uA0set_thread_area(2).) |
CLONE_PARENT_SETTID(since Linux 2.5.49) | Store child thread ID at location%uA0parent_tidptr%uA0in parent and child memory. (In Linux 2.5.32-2.5.48 there was a flag CLONE_SETTID that did this.) |
CLONE_CHILD_SETTID(since Linux 2.5.49) | Store child thread ID at location%uA0child_tidptr%uA0in child memory. |
CLONE_CHILD_CLEARTID(since Linux 2.5.49) | Erase child thread ID at location%uA0child_tidptr%uA0in child memory when the child exits, and do a wakeup on the futex at that address. The address involved may be changed by the%uA0set_tid_address(2) system call. This is used by threading libraries. |
sys_clone
The%uA0sys_clone%uA0system call corresponds more closely to%uA0fork(2) in that execution in the child continues from the point of the call. Thus,%uA0sys_clone%uA0only requires the%uA0flags%uA0andchild_stack%uA0arguments, which have the same meaning as for%uA0clone(). (Note that the order of these arguments differs from%uA0clone().)Another difference for%uA0sys_clone%uA0is that the%uA0child_stack%uA0argument may be zero, in which case copy-on-write semantics ensure that the child gets separate copies of stack pages when either process modifies the stack. In this case, for correct operation, theCLONE_VM%uA0option should not be specified.
Since Linux 2.5.49 the system call has five parameters. The two new parameters areparent_tidptr%uA0which points to the location (in parent and child memory) where the child thread ID will be written in case CLONE_PARENT_SETTID was specified, and%uA0child_tidptrwhich points to the location (in child memory) where the child thread ID will be written in case CLONE_CHILD_SETTID was specified.
返回值
On success, the thread ID of the child process is returned in the caller’s thread of execution. On failure, a -1 will be returned in the caller’s context, no child process will be created, and%uA0errno%uA0will be set appropriately.错误
标签 | 描述 |
---|---|
EAGAIN | Too many processes are already running. |
EINVAL | CLONE_SIGHAND%uA0was specified, but%uA0CLONE_VM%uA0was not. (Since Linux 2.6.0-test6.) |
EINVAL | CLONE_THREAD%uA0was specified, but%uA0CLONE_SIGHAND%uA0was not. (Since Linux 2.5.35.) |
EINVAL | Both%uA0CLONE_FS%uA0and%uA0CLONE_NEWNS%uA0were specified in%uA0flags. |
EINVAL | Returned by%uA0clone() when a zero value is specified forchild_stack. |
ENOMEM | Cannot allocate sufficient memory to allocate a task structure for the child, or to copy those parts of the caller’s context that need to be copied. |
EPERM | CLONE_NEWNS%uA0was specified by a non-root process (process without CAP_SYS_ADMIN). |
EPERM | CLONE_PID%uA0was specified by a process other than process 0. |
VERSIONS
There is no entry for%uA0clone() in libc5. glibc2 provides%uA0clone() as described in this manual page.
遵循于
The%uA0clone() and%uA0sys_cloneꃊlls are Linux specific and should not be used in programs intended to be portable.
注意
In the kernel 2.4.x series,%uA0CLONE_THREAD%uA0generally does not make the parent of the new thread the same as the parent of the calling process. However, for kernel versions 2.4.7 to 2.4.18 the%uA0CLONE_THREAD%uA0flag implied the%uA0CLONE_PARENT%uA0flag (as in kernel 2.6).
For a while there was%uA0CLONE_DETACHED%uA0(introduced in 2.5.32): parent wants no child-exit signal. In 2.6.2 the need to give this together with%uA0CLONE_THREADdisappeared. This flag is still defined, but has no effect. On x86,%uA0clone() should not be called through vsyscall, but directly through%uA0int $0x80. On IA-64, a different system call is used:
int __clone2(int (*fn)(void *), void *child_stack_base, size_t stack_size, int flags, void *arg, ... /* pid_t *pid, struct user_desc *tls ", pid_t *" ctid " */ )"
The%uA0__clone2() system call operates in the same way as%uA0clone(), except thatchild_stack_base%uA0points to the lowest address of the child’s stack area, and%uA0stack_sizespecifies the size of the stack pointed to by%uA0child_stack_base.
BUGS
Versions of the GNU C library that include the NPTL threading library contain a wrapper function for%uA0getpid(2) that performs caching of PIDs. In programs linked against such libraries, calls to%uA0getpid(2) may return the same value, even when the threads were not created using%uA0CLONE_THREAD%uA0(and thus are not in the same thread group). To get the truth, it may be necessary to use code such as the following
#include <syscall.h> pid_t mypid mypid = syscall(SYS_getpid)