This is a quick article on Clone system call example without talking shit. So let’s see some pointers for the same :
clone()
creates a new process, in a manner similar to fork. It is actually a library function layered on top of the underlyingclone
()
system call.- Unlike fork , 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.
- The main use of
clone()
is to implement threads: multiple threads of control in a program that run concurrently in shared memory space. - When the child process is created with
clone()
it executes the function applicationfn
(
arg
)
. Thefn
argument is a pointer to a function that is called by the child process at the beginning of its execution. Thearg
argument is passed to thefn
function. When thefn
(
arg
)
function application returns, the child process terminates. The integer returned byfn
is the exit code for the child process. The child process may also terminate explicitly by calling exit or after receiving a fatal signal. - The child_stack argument specifies the location of the stack used by the child process. Since the child and calling process may share a 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 downwards on all processors that run Linux (except the HP PA processors). So child_stack usually points to the topmost address of the memory space set up for the child stack. - There are some flags related to child/parent control, memory, tracing signals, etc which you can find on man page.
Clone system call example
|
|