0% found this document useful (0 votes)
82 views2 pages

U2 - Mechanism of Process Creation - 37 - 01-10-20

The document discusses the process creation mechanism in Unix systems. It involves three main system calls: 1) fork() creates a child process that is an exact copy of the parent process, but with a different PID. 2) exec() overwrites the child process' address space with a new program, keeping the same PID. 3) wait() allows the parent to wait for the child process to complete before continuing. When a process dies, it becomes a zombie until its parent retrieves its exit status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views2 pages

U2 - Mechanism of Process Creation - 37 - 01-10-20

The document discusses the process creation mechanism in Unix systems. It involves three main system calls: 1) fork() creates a child process that is an exact copy of the parent process, but with a different PID. 2) exec() overwrites the child process' address space with a new program, keeping the same PID. 3) wait() allows the parent to wait for the child process to complete before continuing. When a process dies, it becomes a zombie until its parent retrieves its exit status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Mechanism of process creation

A process is simply a running program. A process is said to be born when the program starts execution and remains
alive till the process is active. After the execution the process is said to be died.

 
A process in a Unix system is created by fork system call. Every process except process 0 is created. The process
that invokes the fork system call is parent process and the newly created process is the child process. 
Every process has one parent process but a parent can have many child process. The kernel identifies it process by
its process identification number (PID). Process 0 is a special process created "by hand" when the system boots.
There are 3 distinct phase in mechanism of process creation and uses 3 system calls: fork(), exec() and wait(). 

fork(): Creates a child process. A new process is created because an existing process creates an exact copy of itself.
This child process has the same environment as its parent but only the PID is different. This procedure is known as
forking. 

exec(): After forking the process, the address space of the child process is overwritten by the new process data. This
is done through exec call to the system. No new process is created over here. The PID & PPID remains unchanged. 

wait(): The parent then executes wait system call to wait for the child process to complete its execution. 

The important attributes that are inherited by the child process from its parents are: Real UID and GID, PGID, Nice
value, Environment setting, Current working directory, memory segments etc. 

When the process dies it immediately moves to the zombie state and remains in that state until the parent picks it up
to exit status. A zombie is the harmless dead child. 

When a child dies kernel send SIGCHLD signal to parent for information. It is also possible for the parent to die
before child dies. The child then becomes an orphan process and kernel makes 'init' parent of all the orphans. When
this adopted child dies 'init' picks up it to exit status
Process States & Transition Diagram
The life time of a process is divided into the states that describe the process as per the following:
1) Process is executing in the user mode.
2) Process is executing in the kernel mode. 
3) Process is not executing but its ready to run as kernel schedules it.
4) Process is sleeping & reside in memory
5) Process is ready to run but swapper must swap the process into the memory.
6) Process is sleeping & the swapper swap the process into secondary storage.
7) Process is returning from kernel to user mode.
8) Process is newly created & runs in a transition state.
9) Process executes the exit system call & moves to zombie state.
Zombie state is the final state of the process.

Process creation in Unix is unique. Most operating systems implement a spawn mechanism to create a new process in a new
address space, read in an executable, and begin executing it. Unix takes the unusual approach of separating these steps into two
distinct functions: fork() and exec()8. The first, fork(), creates a child process that is a copy of the current task. It differs from
the parent only in its PID (which is unique), its PPID (parent's PID, which is set to the original process), and certain resources and
statistics, such as pending signals, which are not inherited. The second function, exec(), loads a new executable into the address
space and begins executing it. The combination of fork() followed by exec() is similar to the single function most operating
systems provide.

fork()
Linux implements fork() via the clone() system call. This call takes a series of flags that specify which resources, if any, the
parent and child process should share (see the section on "The Linux Implementation of Threads" later in this chapter for more
about the flags). The fork(), vfork(), and __clone() library calls all invoke the clone() system call with the requisite flags.
The clone() system call, in turn, calls do_fork().
The bulk of the work in forking is handled by do_fork(), which is defined in kernel/fork.c. This function
calls copy_process(), and then starts the process running. The interesting work is done by copy_process():
● It calls dup_task_struct(), which creates a new kernel stack, thread_info structure, and task_struct for the new
process. The new values are identical to those of the current task. At this point, the child and parent process descriptors are
identical.
● It then checks that the new child will not exceed the resource limits on the number of processes for the current user.

● Now the child needs to differentiate itself from its parent. Various members of the process descriptor are cleared or set to
initial values. Members of the process descriptor that are not inherited are primarily statistically information. The bulk of the
data in the process descriptor is shared.

● Next, the child's state is set to TASK_UNINTERRUPTIBLE, to ensure that it does not yet run.
● Now, copy_process() calls copy_flags() to update the flags member of the task_struct.
The PF_SUPERPRIV flag, which denotes whether a task used super-user privileges, is cleared. The PF_FORKNOEXEC flag,
which denotes a process that has not called exec(), is set.
● Next, it calls get_pid() to assign an available PID to the new task.
● Depending on the flags passed to clone(), copy_process() then either duplicates or shares open files, filesystem
information, signal handlers, process address space, and namespace. These resources are typically shared between threads
in a given process; otherwise they are unique and thus copied here.
● Next, the remaining timeslice between the parent and its child is split between the two (this is discussed in Chapter 4).

● Finally, copy_process() cleans up and returns to the caller a pointer to the new child.


Back in do_fork(), if copy_process() returns successfully, the new child is woken up and run. Deliberately, the kernel runs the
child process first . In the common case of the child simply calling exec() immediately, this eliminates any copy-on-write overhead
9

that would occur if the parent ran first and began writing to the address space.

You might also like