Operating Systems

 

 

Using “fork()” (Creating a Process)
In multitasking OS, processes (running programs) need a way to create new processes to run
other programs. In Unix/Linux OS fork is an operation whereby a process creates a copy of
itself. It is usually a system call, implemented in the kernel. Fork is the primary method of
process creation on Unix-like operating systems.
For a process to start the execution of a different program, it first forks to create a copy of itself.
Then, the copy, called the “child process”, calls the exec system call to overlay itself with the
other program: it ceases execution of its former program in favor of the other.
The fork operation creates a separate address space for the child. The child process has an
exact copy of all the memory segments of the parent process.
The fork function is the primitive for creating a process. It is declared in the header file unistd.h
Function: pid_t fork (void)
http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
A program is an executable file residing in a disk file. An executing instance of a program is
called a process. In Unix/Linux a program is read into memory and executed by the kernel as a
result of one of the six exec functions.
Every Unix/Linux process is guaranteed to have a unique numeric identifier called the process
ID. The process ID (short: PID) is always a nonnegative integer.
Under Unix, the only way (except for some very special processes) to create a new processes
is when an existing process calls the fork function. You will find details about fork and wait in the
manual pages (Unix/Linux command: “man fork”).
Assignment:
Write a simple program that uses the “fork” function. The new process created by fork is called
the “child” process. The function is called once but returns twice. Both the child and the parent
continue executing with the instruction that follows the call to fork. Here is a typical example:
/* parent creates a process */
pid = fork();
/* the return value is a process ID, that ALWAYS needs to be tested */
switch (pid) {
case -1:
/* error: fork was unsuccessful */
break;
case 0:
/* this is the child process */
/* no process ID */
/* … do something … */
break;
default:
/* this is the parent process */
/* pid=process ID of the child */
/* … */
}
/* both processes continue here */
You can list processes by using the ps command or the top command (to see the manual pages
use “man ps” or “man top”). Under Unix/Linux a process that has terminated, but whose
parent has not yet waited for it is called a zombie.
Adjust the sleep calls for parent and child and use the command ps to answer these questions
(add them as comments at the end of your program):
1. What status is being printed when the child terminates before the parent (in another terminal
use: ps ajxf | grep yourProgramName) ?
Note: run the command “man ps” for more options.
2. What is the parent process ID (PPID) of a child process whose parent terminates before
the child?

 

Our customer support team is here to answer your questions. Ask us anything!