Sunday, November 8, 2015

How to create a Process in Linux System?

To perform a certain task we required an application so it will act on given command and lifetime set by program. Whatever it needs in application but eventually we need a process to perform a task.
There are several ways in Linux where we can create a process through a program otherwise opening a shell creates a process and running command over there creates another process.

But running command and using pre-installed application is already designed to their job. what will do if we have to create a separate process for our own need. Linux provides several system calls to create process, duplicate processes.


clone() fork() and vfork()

clone() is a wrapper function defined in the C library, which set up the
new light weight process(LWP), clone() system call hidden to the programmer. The
sys_clone() service routine that implements the clone() system call
does not have the fn and arg parameters.

    fork() system call creates a new process which is duplicate of its
parent process. The child process creates a new entry in process table with
many of the same attributes as the current process. it is almost identical to
the original process, execute the same code but with its own data space,
environment and file descriptors.
    the fork() system call is implemented by Linux as a clone() system call
whose flag parameter specifies both a SIGCHILD signal and all the clone flag
cleared, and whose child_stack parameter is the current parent stack pointer.
Therefore, the parent and child temporarily share the same User Mode stack. But
by using Copy On Write mechanism, they usually get separate copies of the User
Mode Stack as soon as one tries to change the stack.

    vfork() system call creates a new process which shares memory address space
of its parent. there may a chance of parents and data overlapping during
concureent execution, so need to make an arrangement to block either of one
during concurrent execution.
    vfork() is also implemented as a clone() system call whose flags parameter
specifies both a SIGCHLD signal and flags CLONE_VM and CLONE_VFORK, and whose
child_stack parameter is equal to the current stack pointer.

Application of all system calls

No comments:

Post a Comment