Reading Time: 2 minutes

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 underlying clone() 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 application fn(arg). The fn argument is a pointer to a function that is called by the child process at the beginning of its execution. The arg argument is passed to the fn function. When the fn(arg) function application returns, the child process terminates. The integer returned by fn 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

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sched.h>
#include <signal.h>

#define STACK 8192

int do_something(){
        printf("Child pid : %d\n", getpid());
        return 0;

int main() {
        void *stack = malloc(STACK);    // Stack for new process

        if(!stack) {
                perror("Malloc Failed");

        if( clone( &do_something, (char *)stack + STACK, CLONE_VM, 0) < 0 ){
                perror("Clone Failed");

        printf("Parent pid : %d\n", getpid());

        sleep(1);       // Add sleep so we can she both processes output


        return 0;