Thursday 2 January 2014

Linux process creation

Linux process creation.

You may know in linux, processes are organized as a ‘tree’, that is init process is the root (PID = 0) and every other process should have only one parent.

We need to understand how the process is created by the OS.
There are two major steps for creating a process.
  • fork: create the basic data structure for the process in OS
  • exec: load the binary from excutable files into memory and running.



fork procedure


Basic flow:
Fork() will call sys_fork() which is a system call.
Then it will call do_fork() whose mainly task is to create a PID and set trace status (if the parent is set).
Then it will call copy_process() copy process is main task.
dup_task_struct: create kernel stack, thread_info and task_struct.
Set the process status to uninterruptable.
set the flags: copy open file descriptors. Signals, Housekeeping values.
call wake_up_new_task. And returns the PID.
If the child process want to excute some function, it will call exec() to load the new executable files.

Threading creation in Linux


Threading is a quite similar as thread in linux. It is thought as some special thread which are sharing some shared resource in the process. The difference when creating the thread is the shared resource flags will be passed to clone() function

No comments:

Post a Comment