Process Creation And Termination-Operating Systems-Lecture Notes, Study notes for Operating System. Cochin University of Science and Technology

Operating System

Description: Operating Systems is necessary course in Computer Science. Its about threading, process scheduling, deadlocks, memory management etc. This lecture includes: Operating, Process, System, Creation, Operations, Managment, Linux, Unix, Execution, Children, Dynamically
Showing pages  1  -  4  of  4
Operating [CS-604]Systems Lecture No. 6
Operating Systems
Lecture No. 6
Reading Material
Operating Systems Concepts, Chapter 4
UNIX/Linux manual pages for the fork()system call
Process creation and termination
Process management in UNIX/Linux— system calls: fork, exec, wait, exit
Sample codes
Operations on Processes
The processes in the system execute concurrently and they must be created and deleted
dynamically thus the operating system must provide the mechanism for the creation and
deletion of processes.
Process Creation
A process may create several new processes via a create-process system call during the
course of its execution. The creating process is called a parent process while the new
processes are called the children of that process. Each of these new processes may in
turn create other processes, forming a tree of processes. Figure 6.1 shows partially the
process tree in a UNIX/Linux system.
Figure 6.1 Process tree in UNIX/Linux
In general, a process will need certain resources (such as CPU time, memory files,
I/O devices) to accomplish its task. When a process creates a sub process, also known as
a child, that sub process may be able to obtain its resources directly from the operating
system or may be constrained to a subset of the resources of the parent process. The
parent may have to partition its resources among several of its children. Restricting a
process to a subset of the parent’s resources prevents a process from overloading the
system by creating too many sub processes.
When a process is created it obtains in addition to various physical and logical
resources, initialization data that may be passed along from the parent process to the child
process. When a process creates a new process, two possibilities exist in terms of
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
There are also two possibilities in terms of the address space of the new process:
1. The child process is a duplicate of the parent process.
2. The child process has a program loaded into it.
In order to consider these different implementations let us consider the UNIX
operating system. In UNIX its process identifier identifies a process, which is a unique
integer. A new process is created by the fork system call. The new process consists of a
copy of the address space of the parent. This mechanism allows the parent process to
communicate easily with the child process. Both processes continue execution at the
instruction after the fork call, with one difference, the return code for the fork system
call is zero for the child process, while the process identifier of the child is returned to the
parent process.
Typically the execlp system call is used after a fork system call by one of the
two processes to replace the process’ memory space with a new program. The execlp
system call loads a binary file in memory –destroying the memory image of the program
containing the execlp system call.—and starts its execution. In this manner, the two
processes are able to communicate and then go their separate ways. The parent can then
create more children, or if it has nothing else to do while the child runs, it can issue a
wait system call to move itself off the ready queue until the termination of the child.
The parent waits for the child process to terminate, and then it resumes from the call to
wait where it completes using the exit system call.
Process termination
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by calling the exit system call. At that point, the process may return
data to its parent process (via the wait system call). All the resources of the process
including physical and virtual memory, open the files and I/O buffers – are de allocated
by the operating system.
Termination occurs under additional circumstances. A process can cause the
termination of another via an appropriate system call (such as abort). Usually only the
parent of the process that is to be terminated can invoke this system call. Therefore
parents need to know the identities of its children, and thus when one process creates
another process, the identity of the newly created process is passed to the parent.
A parent may terminate the execution of one of its children for a variety of reasons,
such as:
The child has exceeded its usage of some of the resources that it has been
allocated. This requires the parent to have a mechanism to inspect the state of its
The task assigned to the child is no longer required.
The parent is exiting, and the operating system does not allow a child to continue
if its parent terminates. On such a system, if a process terminates either normally
or abnormally, then all its children must also be terminated. This phenomenon
referred to as cascading termination, is normally initiated by the operating system.
Considering an example from UNIX, we can terminate a process by using the exit
system call, its parent process may wait for the termination of a child process by using
the wait system call. The wait system call returns the process identifier of a terminated
child, so that the parent can tell which of its possibly many children has terminated. If the
parent terminates however all its children have assigned as their new parent, the init
process. Thus the children still have a parent to collect their status and execution
The fork() system call
When the fork system call is executed, a new process is created. The original process is
called the parent process whereas the process is called the child process. The new process
consists of a copy of the address space of the parent. This mechanism allows the parent
process to communicate easily with the child process. On success, both processes
continue execution at the instruction after the fork call, with one difference, the return
code for the fork system call is zero for the child process, while the process identifier
of the child is returned to the parent process. On failure, a -1 will be returned in the
parent's context, no child process will be created, and an error number will be set
The synopsis of the fork system call is as follows:
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
int pid;
pid = fork();
if (pid == 0) {
/* Code for child */
else {
/* Code for parent */
Figure 6.2 Sample code showing use of the fork() system call
Figure 6.2 shows sample code, showing the use of the fork() system call and
Figure 6.3 shows the semantics of the fork system call. As shown in Figure 6.3, fork()
creates an exact memory image of the parent process and returns 0 to the child process
and the process ID of the child process to the parent process.
Figure 6.3 Semantics of the fork system call
After the fork() system call the parent and the child share the following:
Open file descriptor table
Signal handling settings
Nice value
Current working directory
Root directory
File mode creation mask (umask)
The following things are different in the parent and the child:
Different process ID (PID)
Different parent process ID (PPID)
Child has its own copy of parent’s file descriptors
The fork() system may fail due to a number of reasons. One reason maybe that the
maximum number of processes allowed to execute under one user has exceeded, another
could be that the maximum number of processes allowed on the system has exceeded.
Yet another reason could be that there is not enough swap space.
Kernel Space
Parent Process
Child Process pid = 0
pid = 1234
pid = 12345
pid = 0
The preview of this document ends here! Please or to read the full document or to download it.
Document information
Uploaded by: sarang
Views: 1325
Downloads : 0
University: Cochin University of Science and Technology
Upload date: 06/08/2012
Embed this document:
Docsity is not optimized for the browser you're using. In order to have a better experience please switch to Google Chrome, Firefox, Internet Explorer 9+ or Safari! Download Google Chrome