clone2 (2) - Linux Manuals
clone2: create a child process
NAME
clone, __clone2, clone3 - create a child process
SYNOPSIS
/* Prototype for the glibc wrapper function */ #define _GNU_SOURCE #include <sched.h> int clone(int (*fn)(void *), void *stack, int flags, void *arg, ... /* pid_t *parent_tid, void *tls, pid_t *child_tid */ ); /* For the prototype of the raw clone() system call, see NOTES */ long clone3(struct clone_args *cl_args, size_t size);
Note: There is not yet a glibc wrapper for clone3(); see NOTES.
DESCRIPTION
These system calls create a new ("child") process, in a manner similar to fork(2).By contrast with fork(2), these system calls provide more precise control over what pieces of execution context are shared between the calling process and the child process. For example, using these system calls, the caller can control whether or not the two processes share the virtual address space, the table of file descriptors, and the table of signal handlers. These system calls also allow the new child process to be placed in separate namespaces(7).
Note that in this manual page, "calling process" normally corresponds to "parent process". But see the descriptions of CLONE_PARENT and CLONE_THREAD below.
This page describes the following interfaces:
- *
- The glibc clone() wrapper function and the underlying system call on which it is based. The main text describes the wrapper function; the differences for the raw system call are described toward the end of this page.
- *
- The newer clone3() system call.
In the remainder of this page, the terminology "the clone call" is used when noting details that apply to all of these interfaces,
The clone() wrapper function
When the child process is created with the clone() wrapper function, it commences execution by calling the function pointed to by the argument fn. (This differs from fork(2), where execution continues in the child from the point of the fork(2) call.) The arg argument is passed as the argument of the function fn.When the fn(arg) function returns, the child process terminates. The integer returned by fn is the exit status for the child process. The child process may also terminate explicitly by calling exit(2) or after receiving a fatal signal.
The stack argument 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 to clone(). Stacks grow downward on all processors that run Linux (except the HP PA processors), so stack usually points to the topmost address of the memory space set up for the child stack. Note that clone() does not provide a means whereby the caller can inform the kernel of the size of the stack area.
The remaining arguments to clone() are discussed below.
clone3()
The clone3() system call provides a superset of the functionality of the older clone() interface. It also provides a number of API improvements, including: space for additional flags bits; cleaner separation in the use of various arguments; and the ability to specify the size of the child's stack area.As with fork(2), clone3() returns in both the parent and the child. It returns 0 in the child process and returns the PID of the child in the parent.
The cl_args argument of clone3() is a structure of the following form:
struct clone_args {
The
size
argument that is supplied to
clone3()
should be initialized to the size of this structure.
(The existence of the
size
argument permits future extensions to the
clone_args
structure.)
The stack for the child process is specified via
cl_args.stack,
which points to the lowest byte of the stack area,
and
cl_args.stack_size,
which specifies the size of the stack in bytes.
In the case where the
CLONE_VM
flag (see below) is specified, a stack must be explicitly allocated
and specified.
Otherwise, these two fields can be specified as NULL and 0,
which causes the child to use the same stack area as the parent
(in the child's own virtual address space).
The remaining fields in the
cl_args
argument are discussed below.
The following table shows the equivalence between the arguments of
clone()
and the fields in the
clone_args
argument supplied to
clone3():
Equivalence between clone() and clone3() arguments
Unlike the older
clone()
interface, where arguments are passed individually, in the newer
clone3()
interface the arguments are packaged into the
clone_args
structure shown above.
This structure allows for a superset of the information passed via the
clone()
arguments.