Clone (Linux system call)
Encyclopedia
clone
is a system callSystem call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...
on the Linux kernel
Linux kernel
The Linux kernel is an operating system kernel used by the Linux family of Unix-like operating systems. It is one of the most prominent examples of free and open source software....
related to multithreading
Thread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...
. In practice, one should try to avoid calling clone directly, but instead use a threading library (such as pthreads
POSIX Threads
POSIX Threads, usually referred to as Pthreads, is a POSIX standard for threads. The standard, POSIX.1c, Threads extensions , defines an API for creating and manipulating threads....
) which use clone when starting a thread (such as during a call to pthread_create).
The syntax for calling
clone
under a Linux program is:clone
creates a new thread that starts with the function pointed to by the fn
argument (as opposed to forkFork (operating system)
In computing, when a process forks, it creates a copy of itself. More generally, a fork in a multithreading environment means that a thread of execution is duplicated, creating a child thread from the parent thread....
which continues with the next command after fork.) The
child_stack
argument is a pointer to a memory space to be used as the stackCall stack
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack"...
for the new thread (which must be malloc
Malloc
C dynamic memory allocation refers to performing dynamic memory allocation in the C via a group of functions in the C standard library, namely malloc, realloc, calloc and free....
'ed before that; on most architectures stack grows down, so the pointer should point at the end of the space),
flags
specify what gets inherited from the parent process, and arg
is the argument passed to the function. It returns the process ID of the child processChild process
A child process in computing is a process created by another process .A child process inherits most of its attributes, such as open files, from its parent. In UNIX, a child process is in fact created as a copy of the parent...
or -1 on failure.
See also
- Native POSIX Thread LibraryNative POSIX Thread LibraryThe Native POSIX Thread Library is a software feature that enables the Linux kernel to run programs written to use POSIX Threads efficiently.-History:...
– POSIXPOSIXPOSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...
implementation of thread functions for Linux - POSIX ThreadsPOSIX ThreadsPOSIX Threads, usually referred to as Pthreads, is a POSIX standard for threads. The standard, POSIX.1c, Threads extensions , defines an API for creating and manipulating threads....