Process Management
Process Management
1. Process
To put it in simple terms, we write our computer programs in a text file and when we execute this
program, it becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into four
sections
— stack, heap, text and data. The following image shows a simplified layout of a process inside main
memory −
1 Stack
2 Heap
3 Text
This includes the current activity represented by the value of Program Counter
and the contents of the processor's registers.
4 Data
2. Program
A program is a piece of code which may be a single line or millions of lines. A computer program is
usually written by a computer programmer in a programming language. For example, here is a simple
program written in C programming language −
#include <stdio.h>
int main() {
return 0;
A computer program is a collection of instructions that performs a specific task when executed by a
computer. When we compare a program with a process, we can conclude that a process is a dynamic
instance of a computer program.
When a process executes, it passes through different states. These stages may differ in different
operating systems, and the names of these states are also not standardized.
In general, a process can have one of the following five states at a time.
1 Start
2 Ready
Once the process has been assigned to a processor by the OS scheduler, the
process state is set to running and the processor executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a resource, such
as waiting for user input, or waiting for a file to become available.
5 Terminated or Exit
1 Process State
The current state of the process i.e., whether it is ready, running, waiting, or
whatever.
2 Process privileges
3 Process ID
5 Program Counter
6 CPU registers
Various CPU registers where process need to be stored for execution for running
state.
9 Accounting information
This includes the amount of CPU used for process execution, time
limits, execution ID etc.
10 IO status information
The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.
5. Messaging Passing
The process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Process communication is the mechanism provided by the operating system that allows processes
to communicate with each other. This communication could involve a process letting another
process know that some event has occurred or transferring of data from one process to another.
One of the models of process communication is the message passing model.
Message passing model allows multiple processes to read and write data to the message queue
without being connected to each other. Messages are stored on the queue until their recipient
retrieves them. Message queues are quite useful for interprocess communication and are used by
most operating systems.
In the above diagram, both the processes P1 and P2 can access the message queue and store and
retrieve data.
1. The message passing model is much easier to implement than the shared memory model.
2. It is easier to build parallel hardware using message passing model as it is quite tolerant of
higher communication latencies.
5.2. Disadvantages of messaging passing model
The message passing model has slower communication than the shared memory model because
the connection setup takes time.
6. CPU Scheduling
CPU scheduling is a process which allows one process to use the CPU while the execution of
another process is on hold(in waiting state) due to unavailability of any resource like I/O etc,
thereby making full use of CPU. The aim of CPU scheduling is to make the system efficient, fast
and fair.
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 scheduler
(or CPU scheduler). The scheduler selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them.
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program from where it left
last time.
The dispatcher should be as fast as possible, given that it is invoked during every process switch.
The time taken by the dispatcher to stop one process and start another process is known as the
Dispatch Latency. Dispatch Latency can be explained using the below figure:
CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state (for I/O request or
invocation of wait for the termination of one of the child processes).
2. When a process switches from the running state to the ready state (for example, when an
interrupt occurs).
3. When a process switches from the waiting state to the ready state (for example, completion of
I/O).
4. When a process terminates.
In circumstances 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in
the ready queue) must be selected for execution. There is a choice, however in circumstances 2
and 3.
When Scheduling takes place only under circumstances 1 and 4, we say the scheduling scheme is
non-preemptive; otherwise the scheduling scheme is preemptive.
The Operating System maintains the following important process scheduling queues −
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing in main memory, ready
and waiting to execute. A new process is always put in this queue.
Device queues − The processes which are blocked due to unavailability of an I/O device
constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin, Priority, etc.). The
OS scheduler determines how to move processes between the ready and run queues which can
only have one entry per processor core on the system; in the above diagram, it has been merged
with the CPU.
When a new process is created, it enters into the system as in the running state.
2 Not Running
Processes that are not running are kept in queue, waiting for their turn to execute.
Each entry in the queue is a pointer to a particular process. Queue is implemented
by using linked list. Use of dispatcher is as follows. When a process is
interrupted, that process is transferred in the waiting queue. If the process has
completed or aborted, the process is discarded. In either case, the dispatcher then
selects a process from the queue to execute.
9. Schedulers
Schedulers are special system software which handle process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to run.
Schedulers are of three types −
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
9.1. Long Term Scheduler
It is also called a job scheduler. A long-term scheduler determines which programs are admitted
to the system for processing. It selects processes from the queue and loads them into memory for
execution. Process loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O
bound and processor bound. It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal to the
average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-sharing
operating systems have no long term scheduler. When a process changes the state from new to
ready, then there is use of long-term scheduler.
Short-term schedulers, also known as dispatchers, make the decision of which process to execute
next. Short-term schedulers are faster than long-term schedulers.
A running process may become suspended if it makes an I/O request. A suspended processes
cannot make any progress towards completion. In this condition, to remove the process from
memory and make space for other processes, the suspended process is moved to the secondary
storage. This process is called swapping, and the process is said to be swapped out or rolled out.
Swapping may be necessary to improve the process mix.
2 Speed is lesser than short Speed is fastest among Speed is in between both
term scheduler other two short and long term
scheduler.
When the scheduler switches the CPU from executing one process to execute another, the state
from the current running process is stored into the process control block. After this, the state for
the process to run next is loaded from its own PCB and used to set the PC, registers, etc. At that
point, the second process can start executing.
Context switches are computationally intensive since register and memory state must be saved and
restored. To avoid the amount of context switching time, some hardware systems employ two or
more sets of processor registers. When the process is switched, the following information is stored
for later use.
Program Counter
Scheduling information
Base and limit register value
Currently used register
Changed State
I/O State information
Accounting information
Shared memory is a memory shared between two or more processes. However, why do we need to
share memory or some other means of communication?
To reiterate, each process has its own address space, if any process wants to communicate with some
information from its own address space to other processes, then it is only possible with IPC (inter
process communication) techniques. As we are already aware, communication can be between
related or unrelated processes.
Usually, inter-related process communication is performed using Pipes or Named Pipes. Unrelated
processes (say one process running in one terminal and another process in another terminal)
communication can be performed using Named Pipes or through popular IPC techniques of Shared
Memory and Message Queues.
We have seen the IPC techniques of Pipes and Named pipes and now it is time to know the
remaining IPC techniques viz., Shared Memory, Message Queues, Semaphores, Signals, and
Memory Mapping.
Create the shared memory segment or use an already created shared memory segment
(shmget())
Attach the process to the already created shared memory segment (shmat())
Detach the process from the already attached shared memory segment (shmdt())
A Process Scheduler schedules different processes to be assigned to the CPU based on particular
scheduling algorithms. There are six popular process scheduling algorithms which we are going to
discuss in this chapter −
P0 0-0=0
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
Easy to implement in Batch systems where required CPU time is known in advance.
Impossible to implement in interactive systems where required CPU time is not known.
The processer should know in advance how much time process will take.
Wait time of each process is as follows −
P0 3-0=3
P1 0-0=0
P2 16 - 2 = 14
P3 8-3=5
Each process is assigned a priority. Process with highest priority is to be executed first and so
on.
Processes with same priority are executed on first come first served basis.
Priority can be decided based on memory requirements, time requirements or any other
resource requirement.
P0 9-0=9
P1 6-1=5
P2 14 - 2 = 12
P3 0-0=0
The processor is allocated to the job closest to completion but it can be preempted by a newer
ready job with shorter time to completion.
Impossible to implement in interactive systems where required CPU time is not known.
It is often used in batch environments where short jobs need to give preference.
Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.
P0 (0 - 0) + (12 - 3) = 9
P1 (3 - 1) = 2
P3 (9 - 3) + (17 - 12) = 11