OS Unit 2
OS Unit 2
UNIT 2
Process Management
A process is a program in execution. It is the unit of work in modern time sharing
system. When the program is loaded into the memory it becomes process and it is
divided into four section: stack, heap, text and data. Although its main concern is to
execute user program, it also need to take care of various system tasks. A system
therefore consist of a collection of processes like operating system process executing
system code and user processes executing user code. All the process can execute
concurrently with the CPU multiplexed among them.
A process is more than a program code which is sometimes known as a text section.
Text section also includes the current activity represented by the value of program
counter and the content of processor’s register. Process generally also includes the
process stack which contains temporary data like function, parameter, return
address and local variables. It also contains data section which contains global
variable and includes heap also which is a memory that is dynamically allocated
during process run time. The structure of process is shown below:
The architecture of PCB completely depends on operating system used and may
contains different information in different operating system. The structure of PCB is
shown below:
Process scheduling:
The objective of multiprogramming is to have some process running at all times such
that CPU utilization is maximized. Similarly, the objective of time sharing system is
to switch CPU among the process so frequently that user can interact with each
program while it is running. To meet these objective, the process scheduler selects
an available process from a set of processes for program execution in CPU.
As process enters the system they are put into the job queues which consist of all the
process in the system. The process that are residing in main memory and are ready
to execute are kept on a list called ready queue. This queue is generally stored in
linked list and a ready queue headers contains pointer to the first and last PCB in the
list. Each PCB contains a pointer field that points to the next PCB in the ready queue.
When a process is allocated the CPU, it executes for a while then eventually quits or
interrupted or wait for the occurrence of particular event like completion of I/O
request. Thus the process may have to wait for disk. The list of process waiting for
a particular I/O device is called device queue.
The above figure shows the representation of process scheduling. Each rectangular
box represents a queue. Here two types of queues are present: the ready queue and a
set of device queue. The circle represents the resources that serve the queues and
arrow represents flow of process in the system.
A new process is initially put into ready queue and waits there until it is selected for
execution or dispatched. Once the process is allocated to CPU and is executing
following events could occurs:
• A process could issues an I/O request and then be placed on I/O queue.
• A process could create a new child process and for child termination
• The process could be removed forcibly from CPU due to interrupt and be put
back in the ready queue.
In the first two case process eventually switches from waiting state to the ready state
and is then put back into ready queue and continues until it is terminated. The address
space of the current process must be preserved as the space of the next task is
prepared for use.
Process Context Switch:
Switching the CPU to another process requires performing a state save of the current
process and a state restore of a different process. This task is known as context
switch. Because of this CPU can resumes the process execution from the same point
at which it has been stopped. When a context switch occur the kernel saves the
context of old process in its PCB and load the saved context of the new process
scheduled to run. Context-switching is pure overhead because system does no useful
work while switching. Switching speed varies from machine to machine depending
upon memory speed, number of register and the existence of special instruction.
Context switch times are highly dependent on hardware
Operation on Processes:
The process in most of the system can execute concurrently and they must be created
and deleted dynamically. The system must provide the mechanism for process
creation and termination. The mechanism involved in creating and terminating
process in UNIX system is shown below:
1. Process creation:
A process may create several new process during the course of execution. The
creating process is called parent process and the new process are called the
children of that process. Each of these process may in turn create other
processes forming a tree of process known as hierarchies of process.
Processes are identified according to the unique process identifier or pid,
which is typically an integer value. The pid provides a unique value for each
process in the system and it can be used as an index to access various attributes
of a process within the kernel.
The operating system can run many process at the same time but initially it
directly starts the one process called init (initial) process. The init process
which always has a pid 1 serve as the root parent process for all user process.
When a process creates a new process, the two possibilities for execution
exist:
• The parent continue to execute concurrently with its children
• The parent waits until some or all of its children have terminated.
There are also two address space possibility for the new process:
• The child process is duplicate of the parent process i.e. it has the same
program and data as the parent.
• The child process has a new program loaded into it.
When a process created a child process, that child process will need certain
resources (CPU time, memory etc.) to accomplish the task. Such resources
can be obtain directly from the operating system or it may be constrained to
the subset of resource of the parent process.
Following figure illustrate the typical process tree for Linux operating system,
showing the name of the process and pid. Once the system has started, the init
process can create various other process such as web or print server, ssh server
etc.
In above figure, there are three children of init: kthreadd, sshd and login. The
kthreaded process is responsible for creating additional process that perform
the task in behalf of kernel. The sshd process is responsible for managing the
client that connected to system by using ssh (secure shell).
The login process is responsible for the managing the client that directly logs
on to the system. Here, a clients have logged in and is using a bash shell which
has been assigned pid 8416. Using the bash command line user has created
the process ps and emacs editor.
The following C program illustrate the creation and execution of child process in
UNIX system:
#include <stdio.h>
#include <sys/types.h>
int main (){
pid_t pid;
if(pid<0){
fprintf(stderr, “Fork failed”);
return -1;
}
else if(pid==0){
/* child process */
execlp(“/bin/ls”, “ls”, NULL);
}
else{
/* parent process */
wait (NULL);
printf(“child complete”);
}
return 0;
}
Flow of execution:
Here we have two different process running copies of the same program. The new
process is created by fork() system call. This new process consist the copy of the
address of the parent process (main()) because of which parent process can
communicate with child process. The value of the pid for the child process is zero
while for the parent it is the integer value greater than zero.
The exec() or execlp() system call replace the process’s memory space with new
program and starts its execution. The child process then overlaps its address space
with the UNIX command /bin/ls using the execlp() system call. The parent waits for
the child process to complete with the wait() system call.
When the child process complete by invoking exit() system call, the parent process
resumes from the call to wait().
2. Process Termination:
A process terminates when it finishes executing its final statement and ask the
operating system to delete it by using exit() system call. At this point process
may return the status value to its parent process via the wait() system call. All
the resource that process consumed are deallocated by the operating system.
A parent process may terminated 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.
• The task assigned to the children is no longer required.
• The parent is exiting and the OS does not allow a child to continue if
its parent terminate.
In some system, if parent terminate then child process are not allowed to exist
i.e. child process also have to terminate. This phenomenon is referred as
cascading termination which is normally initiated by operating system.
In Linux and UNIX system, process are terminated by using exit() system call
providing status as parameter. Exit may be called either directly as shown
below or indirectly (by a return statement in main()).
exit(1)
A process that has been terminated but whose parent has not yet called wait()
is known as zombie process. Once the process call wait(), the process
identifier of the zombie process and its entry in the process table are released.
Inter-Process Communication:
Process executing concurrently in the operating system may require to exchange
message between them. Such concurrently executing process may either be
independent process or cooperating process. Any process that does not shares the
data with any other process is independent process. Such process cannot affect or
be affected by the other processes executing in the system.
Any process that shares data with other processes is a cooperating process. Such
process can affect or be affected by other process executing in the system. There are
several reason for providing inter process communication such as for information
sharing, computation speedup, modularity, convenience.
Inter-process communication mechanism are required for cooperating process in
order to exchange data and information. There are two fundamental model for inter-
process communication:
• Shared memory
• Massage passing
1. Shared Memory:
In shared memory, a region of the memory that is shared by the cooperating
process is established. The process can then exchange information by reading
and writing data to the shared region. A shared memory region resides in the
address space of the process creating the shared memory segment. Other
process that wish to communicate using this shared memory region must
attach address space of the process that have created the shared memory
region. Generally, operating system tries to prevent one process from
accessing other process’s space but for shared memory two process must have
to remove this restriction. They can then exchange information by reading and
writing data in the shared area.
System calls are requires only to establish shared-memory region. Once
shared memory is established, all access are treated as routine memory access
and assistance from kernel is not required. The form of the data and location
are determined by these process and are not under the operating system’s
control. Process are also responsible for ensuring that they are not writing to
same location simultaneously.
For the two or more process to communicate, the communication link must
exist between them. Here link are implemented logically and their methods
are illustrated below:
• Direct or indirect communication:
Under direct communication, each process that wants to communicate
must explicitly name the recipient or sender of the communication. This
scheme exhibits either symmetry in addressing where both the sender
and the receiver process must name the other to communicate or
asymmetry in address where only sender name the recipient but
recipient is not required to name the sender. Both scheme are shown
below:
For symmetry:
Send (P, message) – send a message to process P.
Receiver (Q, message) – receive a message from process Q.
For asymmetry:
send (P, message) – send a message to process P.
receiver (id, message) – receive a message from any process. Id
specifies the name of the process with which communication has taken
place.
Here link is established automatically and associated with exactly two
process with exactly one link.
With the indirect communication the message are sent to receive from
mail box or ports. Each mail-box have a unique identification and can
be view abstractly as an object into which message can be placed and
removed by process. Two process can communicate if they have shared
mail-box. The send () and receive () primitive are shown below:
send (A, message) – send a message to mailbox A
receive (A, message) – receives a message form mailbox B.
Here, a link may be associated with more than two processes but for
two process to communicate they must share common mail box.
• Synchronization:
Message passing may be either blocking or non-blocking also known
as synchronous and asynchronous. In Blocking send: the sending
process is blocked until the message is received by the receiving
process or by the mailbox. In non-blocking send: the sending process
sends the message and resumes operation. In Blocking receive: the
receiver block until a message is available. In Non-blocking receive the
receiver retrieves either a valid message or a null.
When both send () and receive () are blocking there is a rendezvous
point between the sender and the receiver.
• Buffering:
Whether the communication is direct or indirect, message exchanged
by communicating process reside in a temporary queue. Such queues
can be implemented in three ways:
Scheduler:
A process migrate among the various scheduling queues throughout its life time. The
operating system must select process from this queues for the process to execute.
This selection process is carried out by scheduler. Hence, the work of scheduler is
to select the appropriate process from the queue for execution.
In a batch system, more process are submitted than that can be executed. Such
process are spooled or kept in mass storage for later execution. In this case, the long-
term scheduler or job scheduler selects processes from this pool and loads them into
the memory for execution. Another scheduler known as short term scheduler or CPU
scheduler selects the process that are ready to execute and allocated the CPU to one
of them.
Time sharing system have introduce an additional intermediate level of scheduling
known as medium term scheduler. Here the process is swapped out with other
process and swapped in later by medium term scheduler. The running process are
removed first (swap out) and placed in separate queue and later on it is reintroduces
in the memory and its execution can be continued where it is left off. This scheme is
known as swapping.