Process
Process
Process
A process can be thought
of as a program in
execution (or) A process
is the unit of work in a
modern time-sharing
system.
A process will need
certain resources such
as CPU time, memory,
files and I/O devices to
accomplish its task.
These resources are
allocated to the process
either when it is created
or while it is executing
Process
A process can be thought of as a program in execution (or) A process is the
unit of work in a modern time-sharing system.
A process will need certain resources such as CPU time, memory, files and
I/O devices to accomplish its task. These resources are allocated to the
process either when it is created or while it is executing.
Process
A process can be thought of as a program in execution (or) A process is the unit of
work in a
modern time-sharing system.
A process will need certain resources such as CPU time, memory, files and I/O
devices to
accomplish its task.
These resources are allocated to the process either when it is created or while it is
executing
Process Concept:
An operating system executes a variety of programs: A batch system
executes jobs, whereas a time-shared system has user programs, or tasks
We will use the terms job and process almost interchangeably.
Process – is a program in execution (informal definition).
Program is passive entity stored on disk (executable file), process is active
o Program becomes process when executable file loaded into memory.
Execution of program started via GUI, command line entry of its name, etc.
One program can be several processes
o Consider multiple users executing the same program.
The below figure shows the structure of process in memory:
The process contains several sections: Text, Data, Heap and Stack.
Text Section contains the program code. It also includes the current
activity, as
represented by the value of the program counter and the contents of
the processor’s registers.
Process stack contains temporary data such as function parameters,
return addresses and local variables.
Data section contains global variables.
Heap is memory that is dynamically allocated during process run
time.
Difference between Program and Process:
A program is a passive entity, such as a file containing a list of
instructions stored on disk
often called an executable file.
A process is an active entity with a program counter specifying the
next instruction to
execute and a set of associated resources.
A program becomes a process when an executable file is loaded into
memory.
Two common techniques for loading executable files are double-
clicking an icon representing the executable file and entering the name
of the executable file on the command line as in prog.exe or a.out.
Although two processes may be associated with the same program, they are
considered as two separate execution sequences.
For instance, several users may be running different copies of the mail
program or the same user may invoke many copies of the web browser
program. Each of these is considered as a separate process.
Process State
As a process executes, it changes state. The process state defines the current
activity of that process.
A process may be in one of the following states:
New: The process is being created.
Ready: The process is waiting to be assigned to a processor.
Running: Instructions are being executed.
Waiting: The process is waiting for some event to occur such as an
I/O completion or reception of a signal.
Terminated: The process has finished execution.
Note: Only one process can be running on any processor at any instant of
time.
Process Control Block
Each process is represented in the operating system by a Process Control
Block (PCB). It is also called a Task Control Block.
PCB serves as the repository for any information that may vary from process
to process.
Process state: The state may be new, ready, running, waiting and
terminated.
Program counter: The counter indicates the address of the next instruction
to be executed for this process.
CPU registers: The registers vary in number and type, depending on the
computer architecture. They include accumulators, index registers, stack
pointers and general purpose registers etc. Along with the program counter,
this state information must be saved when an interrupt occurs, to allow the
process to be continued correctly afterward.
I/O status information: This information includes the list of I/O devices
allocated to the process, a list of open files and so on.
Process Scheduling:
Ready queue – set of all processes residing in main memory, ready and
waiting to execute. Stored as a linked list.
The circles represent the resources that serve the queues, and the arrows
indicate the flow of processes in the system.
The process could issue an I/O request and then be placed in an I/O
queue.
• The process could create a new child process and wait for the child’s
termination.
Schedulers
1. I/O bound process is one that spends more of its time doing I/O than it spends
doing computations.
2. CPU Bound process using more of its time doing computations and generates
I/O requests infrequently.
The long-term scheduler selects a good process mix of I/O-bound and CPU-bound
processes.
If all processes are I/O bound, the ready queue will almost always be empty and
the CPU will remain idle for long time because I/O device processing takes a lot of
time.
If all processes are CPU bound, the I/O waiting queue will almost always be
empty. I/O devices will be idle and CPU is busy for most of the time.
Thus if the system maintains the combination of CPU bound and I/O bound
processes then the system performance will be increased.
Note: Time-sharing systems such as UNIX and Microsoft Windows systems often
have no long-term scheduler but simply put every new process in memory for the
short-term scheduler.
Context Switching
Switching the CPU from one process 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 a Context Switch.
The context is represented in the PCB of the process. It includes the value of the
CPU registers, the process state and memory-management information.
When a context switch occurs, the kernel saves the context of the old process in
its PCB and loads the saved context of the new process scheduled to run.
Context-switch time is pure overhead, because the system does no useful work
while switching. Context switch time may be in few milliseconds.
Operations on Processes
1. Process Creation
2. Process Termination
Process Creation
During the execution of a process in its life time, a process may create
several new processes.
The creating process is called a parent process and the new processes are
called children process.
Each of these new processes may create other processes forming a tree of
processes.
Operating system identifies processes according to process identifier (pid).
Pid provides an unique integer number for each process in the system.
Pid can be used as an index to access various attributes of a process within
the kernel.
The below figure shows the process tree for the Linux OS that shows the
name of each process and its pid. In Linux process is called task.
The init process always has a pid of 1. The init process serves as the root
parent process for all user processes.
Once the system has booted, the init process can also create various user
processes, such as a web or print server, an ssh server etc.
kthreadd and sshd are child processes of init.
The kthreadd process is responsible for creating additional processes that
perform tasks on behalf of the kernel.
The sshd process is responsible for managing clients that connect to the
system by using secure shell (ssh).
ps –el
The command will list complete information for all processes currently
active in the system.
When a process creates a child process, that child process will need certain
resources such as CPU time, memory, files, I/O devices to accomplish its
task.
A child process may be able to obtain its resources directly from the
operating system or
it may be constrained to a subset of the resources of the parent process.
The parent may have to partition its resources among its children or it may
be able to share some resources such as memory or files among several of its
children.
Restricting a child process to a subset of the parent’s resources prevents any
process from overloading the system by creating too many child processes.
When a process creates a new process there exist two possibilities for execution:
2. The parent waits until some or all of its children have terminated.
There are also two address-space possibilities for the new process:
1. The child process is a duplicate of the parent process (i.e) it has the same
program and
Process System calls in Unix/ Linux: fork( ), exec( ), wait( ), exit( ) fork( ):
Program for Creating a separate process using the UNIX fork( ) system
call
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main( )
{
pid_t pid;
/* fork a child process */
pid = fork( );
if (pid < 0)
{ /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0)
{ /* child process */
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
}
The above C program shows the UNIX system calls fork, exec, wait. Two
different processes are running copies of the same program.
The only difference is that the value of pid for the child process is
zero, while the value of pid for the parent is an integer value greater
than zero (i.e. the actual pid of the child process).
The child process inherits privileges and scheduling attributes from
the parent, as well as certain resources such as open files.
The child process then overlays its address space with the UNIX
command /bin/ls (used to get a directory listing) using the execlp( )
system call (execlp( ) is a version of the exec( ) system call).
The parent waits for the child process to complete with the wait( )
system call.
When the child process completes by either implicitly or explicitly
invoking exit( ), the parent process resumes from the call to wait( ),
where it completes using the exit( ) system call.
INTERPROCESS COMMUNICATION:
A process is cooperating if it can affect or be affected by the other processes
executing in the system.
There are several reasons for providing an environment that allows process
cooperation:
• Information sharing. Since several users may be interested in the same piece of
information (for instance, a shared file), we must provide an environment to allow
concurrent access to such information.
• Computation speedup. If we want a particular task to run faster, we must break
it into subtasks, each of which will be executing in parallel with the others. Notice
that such a speedup can be achieved only if the computer
has multiple processing cores.
• Modularity. We may want to construct the systemin a modular fashion, dividing
the system functions into separate processes or threads
• Convenience. Even an individual user may work on many tasks at the
same time. For instance, a user may be editing, listening to music, and
compiling in parallel.
Cooperating processes require an interprocess communication (IPC) mechanism
that will allow them to exchange data and information. There are two fundamental
models of interprocess communication: shared memoryand message
passing.
In the shared-memory model, a region of memory that is shared by cooperating
processes is established. Processes can then exchange information by reading
and writing data to the shared region.
In the message-passing model, communication takes place by means of
messages exchanged between the cooperating processes.
Shared memory systems:
If the process wants to initiate the communication and it has some data to
share, then establish the shared memory region in its address space.
After that, another process wants to communicate and tries to read the
shared data, and must attach itself to the initiating process’s shared address
space.
considering two processes. The diagram is shown below –
Let the two cooperating processes P1 and P2. Both the processes P1 and P2,
have their different address spaces. Now let us assume, P1 wants to share
some data with P2.
Step 1 − Process P1 has some data to share with process P2. First P1 takes
initiative and establishes a shared memory region in its own address space
and stores the data or information to be shared in its shared memory region.
Step 2 − Now, P2 requires the information stored in the shared segment of
P1. So, process P2 needs to attach itself to the shared address space of P1.
Now, P2 can read out the data from there.
Step 3 − The two processes can exchange information by reading and
writing data in the shared segment of the process.
Message-Passing Systems
Message passing provides a mechanism to allow processes to communicate
and to synchronize their actions without sharing the same address space.
It is particularly useful in a distributed environment, where the
communicating processes may reside on different computers connected by a
network.
For example, an Internet chat program could be designed so that chat
participants communicate with one another by exchanging messages.
Naming :
Under direct communication, each process that wants to communicate must
explicitly name the recipient or sender of the communication.
In this scheme, the send() and receive() primitives are defined as:
send(P, message)—Send a message to process P.
receive(Q, message)—Receive a message from process Q.
A communication link in this scheme has the following properties:
A link is established automatically between every pair of processes that
want to communicate.
A link is associated with exactly two processes.
Between each pair of processes, there exists exactly one link
Symmetry in addressing: both the sender process and the receiver process must
name the other to communicate.
Asymmetry: A variant of this scheme employs asymmetry in addressing.
Here, only the sender names the recipient; the recipient is not required to name the
sender.
In this scheme, the send() and receive() primitives are defined as follows:
• send(P, message)—Send a message to process P.
• receive(id, message)—Receive a message from any process. The variable id is set
to the name of the process with which communication has taken place.
Symmetry: eg., E-mail, Asymmetric : Broadcast message or sending parcel (mail
delivery).
Disadvantage of direct communication:
Changing the identifier of a process may necessitate examining all other process
definitions.
Indirect communication:
In indirect communication, the sender and receiver do not directly know each
other or interact with each other directly.
Instead, they communicate by sending messages to a shared intermediate
object, often called a mailbox or port
A process can communicate with another process via a number of different
mailboxes, but two processes can communicate only if they have a shared
mailbox.
The send() and receive() primitives are defined as follows:
send(A, message)—Send a message to mailbox A.
receive(A, message)—Receive a message from mailbox A.
Blocking send. The sending process is blocked until the message is received by
the receiving process or by the mailbox.
• Nonblocking send. The sending process sends the message and resumes
operation.
Buffering
Zero capacity: The sender must wait until the receiver picks up the message, as
no waiting messages are allowed.
Bounded capacity: The sender can send messages freely if the queue has space;
otherwise, it must wait.
Unbounded capacity: Messages can be sent indefinitely without the sender ever
needing to wait.
CPU SCHEDULING
CPU scheduling is the basis of Multi-programmed operating systems. By
switching the CPU among processes, the operating system can make the
computer more productive.
In a single-processor system, only one process can run at a time. Others must
wait until the CPU is free and can be rescheduled.
The CPU will sit idle and waiting for a process that needs an I/O operation to
complete. If the I/O operation completes then only the CPU will start executing
the process. A lot of CPU time has been wasted with this procedure.
The objective of multiprogramming is to have some process running at all times
to maximize CPU utilization.
When several processes are in main memory, if one processes is waiting for I/O
then the operating system takes the CPU away from that process and gives the
CPU to another process. Hence there will be no wastage of CPU time.
Concepts of CPU Scheduling
1. CPU–I/O Burst Cycle
2. CPU Scheduler
3. Pre-emptive Scheduling
4. Dispatcher
CPU–I/O Burst Cycle
Process execution consists of a cycle of CPU execution and I/O wait.
Process execution begins with a CPU burst. That is followed by an I/O
burst. Processes alternate between these two states.
The final CPU burst ends with a system request to terminate execution.
Hence the First cycle and Last cycle of execution must be CPU burst.
CPU Scheduler
Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried out by
the Short-Term
Preemptive Scheduling
CPU-scheduling decisions may take place under the following four cases:
1. When a process switches from the running state to the waiting state.
2. When a process switches from the running state to the ready state.
3. When a process switches from the waiting state to the ready state.
Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected
by the short-term scheduler. Dispatcher function involves:
1. Switching context
3. Jumping to the proper location in the user program to restart that program.
The dispatcher should be as fast as possible, since it is invoked during every
process switch.
The time it takes for the dispatcher to stop one process and start another process
running is known as the Dispatch Latency.
SCHEDULING CRITERIA
Different CPU-scheduling algorithms have different properties and the choice of a
particular algorithm may favor one class of processes over another.
Many criteria have been suggested for comparing CPU-scheduling algorithms:
CPU utilization: CPU must be kept as busy as possible. CPU utilization can
range from 0 to 100 percent. In a real system, it should range from 40 to 90
percent.
Throughput: The number of processes that are completed per time unit.
Turn-Around Time: It is the interval from the time of submission of a
process to the time of completion. Turnaround time is the sum of the periods
spent waiting to get into memory, waiting in the ready queue, executing on
the CPU and doing I/O.
Waiting time: It is the amount of time that a process spends waiting in the
ready queue.
Response time: It is the time from the submission of a request until the first
response is produced. Interactive systems use response time as its measure.
Note: It is desirable to maximize CPU utilization and Throughput and to minimize
TurnAround Time, Waiting time and Response time.
Example:1 Consider the following set of processes that arrive at time 0. The
processes are arrived in the order P1, P2, P3, with the length of the CPU burst
given in milliseconds.
Threads:
A thread is a single sequence stream within a process. Threads are also called
lightweight processes as they possess some of the properties of processes.
Threads provide a way to improve application performance through
parallelism.
A thread is the smallest unit of execution within a process.
A process can have multiple threads that run concurrently, sharing resources
like memory, open files, and global variables. However, each thread has its
own program counter, registers, and stack.
Advantages of Thread
• Threads minimize the context switching time.
• Use of threads provides concurrency within a process.
• Efficient communication.
• It is more economical to create and context switch threads.
• Threads allow utilization of multiprocessor architectures to a greater scale
and efficiency.
Types of Thread
In this case, the thread management kernel is not aware of the existence of
threads. The thread library contains code for creating and destroying threads,
for passing message and data between threads, for scheduling thread execution
and for saving and restoring thread contexts. The application starts with a single
thread.
Kernel Level Threads
The Kernel maintains context information for the process as a whole and for
individuals’ threads within the process. Scheduling by the Kernel is done on a
thread basis. The Kernel performs thread creation, scheduling and management in
Kernel space.
Kernel threads are generally slower to create and manage than the user threads.
Multithreading Models
Some operating system provide a combined user level thread and Kernel level
thread facility. Solaris is a good example of this combined approach.
In a combined system, multiple threads within the same application can run in
parallel on multiple processors and a blocking system call need not block the entire
process.
Multithreading models are three types
The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads.
Threading Issues
• Some other issue related to the thread is discussed in this section. The issue
includes fork and exec system call, thread cancellation, signal handling, thread
pool etc.
• Based on the application, system uses fork system calls. Sometimes, duplicating
all the threads are unnecessary. If we call exec immediately after fork then there is
no use of duplicating threads.
• Forking provides a way for an existing process to start a new one, but what about
the case where the new process is not part of the same program as parent process?
This is the case in the shell; when a user starts a command it needs to run in a new
process, but it is unrelated to the shell.
• This is where the exec system call comes into play. An exec will replace the
contents of the currently running process with the information from a program
binary. Thus the process the shell follows when launching a new program is to
firstly fork, creating a new process, and then exec the program binary it is
supposed to run.
Thread Cancellation
• Cancellation allows one thread to terminate another. One reason to cancel a
thread is to save system resources such as CPU time. When your program
determines that the thread's activity is no longer necessary then thread is
terminated.
• The target thread can keep cancellation requests pending and can perform
application-specific cleanup when it acts upon the cancellation notice.
Signal Handling
Signal is used to notify a process that a particular event has occurred. Signal may
be synchronous or asynchronous. All types of signals are based on the following
pattern:
1. At a specific event, signal is generated.
2. Generated signal is sent to a process/thread.
The method for delivering a signal depends on the type of signals generated.
1. Synchronous signals is sent to the thread which causes the signal.
2. All the thread received asynchronous signals.
Thread Pool
A thread pool offers a solution to both the problem of thread life-cycle overhead
and the problem of resource thrashing. By reusing threads for multiple tasks, the
thread-creation overhead is spread over many tasks.
• Thread pools group CPU resources, and contain threads. used to execute tasks
associated with that thread pool. Threads host engines that execute user tasks, run
specific jobs such as signal handling and process requests from a work queue.
• Multithreaded server has potential problems and these problems are solved by
using thread pool. Problems with multithreaded server :
Deadlock Handling
The various strategies for handling deadlock are1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
4. Deadlock Ignorance
1. Deadlock Prevention
Deadlocks can be prevented by preventing at least one of the four required
conditions:
Mutual Exclusion
Shared resources such as read-only files do not lead to deadlocks.
Unfortunately, some resources, such as printers and tape drives, require exclusive
access by a single process.
Hold and Wait
To prevent this condition processes must be prevented from holding one or more
resources while simultaneously waiting for one or more others.
No Preemption
Preemption of process resource allocations can prevent this condition of
deadlocks, when it is possible.
Circular Wait
One way to avoid circular wait is to number all resources, and to require that
processes request resources only in strictly increasing (or decreasing ) order.
2. Deadlock Avoidance
In deadlock avoidance, the operating system checks whether the system is in
safe state or in unsafe state at every step which the operating system performs.
The process continues until the system is in safe state.
Once the system moves to unsafe state, the OS has to backtrack one step.
In simple words, The OS reviews each allocation so that the allocation doesn't
cause the deadlock in the system.
3. Deadlock detection and recovery
This strategy involves waiting until a deadlock occurs.
After deadlock occurs, the system state is recovered.
The main challenge with this approach is detecting the deadlock.
4. Deadlock Ignorance
This strategy involves ignoring the concept of deadlock and assuming as if it
does not exist.
This strategy helps to avoid the extra overhead of handling deadlock.
Windows and Linux use this strategy and it is the most widely used method.