Module 2 Process Management-1
Module 2 Process Management-1
Process
Management
Definition:
A process is basically a program in execution or
instance of the program execution. The
execution of a process must progress in a
sequential fashion.
● Process is not as same as program code
but a lot more than it.
● A process is an 'active' entity as opposed to
program which is considered to be a
'passive' entity.
● Attributes held by process include
hardware state, memory, CPU etc.
2
Process memory is divided into four sections for
efficient working :
3
Process State
4
The Five-State Model
● New: The process has just been created. It hasn’t started running yet, but its PCB (Process
Control Block) is prepared.
● Ready: The process is loaded into memory and prepared to run as soon as the CPU becomes
free.
● Running: The CPU is currently executing this process. With a single CPU system, only one
process can be in this state at a time.
● Blocked/Waiting: The process cannot continue execution because it is waiting for an event
like I/O completion or data input.
● Exit/Terminate: The process has completed its execution or has been stopped, and the
operating system removes it from memory.
5
6
The main process states are:
● New: The process is in the creation phase. The program exists in secondary memory, and the OS prepares its
PCB before loading it into main memory.
● Ready: The process has been loaded into main memory and is prepared to run. It is waiting in the ready queue
until the CPU becomes available.
● Running: The CPU is currently executing the instructions of the process. Only one process can be in this state
at a time on a single-processor system.
● Blocked/Waiting: The process cannot continue because it is waiting for an event such as I/O completion, user
input, or access to a locked resource. Once the event is complete, it moves back to the ready state.
● Terminated: The process has completed its execution or has been stopped. The OS deletes its PCB and frees
all resources allocated to it.
● Suspend Ready: A ready process that has been swapped out of main memory and placed in secondary
storage due to memory shortage. When brought back to main memory, it returns to the ready state.
● CPU-Bound / I/O-Bound: A CPU-bound process spends most of its time performing computations, while an
I/O-bound process spends more time waiting for input/output operations.
7
Process Creation
The first operation that a process undergoes when it enters a system is process creation. It
involves formation of a process and is associated with New state of the process. Process
creation occurs due to any of the following events −
● System initialization − When the computer is started, a number of system processes and
background processes are created.
● User request − A user may start executing a program leading to creation of a process.
● Child process system call − A running process may create a child process by process
creation system call.
● Batch system − The batch system may initiate batch jobs
A process may be created by another process using fork(). The creating process is called the
parent process and the created process is the child process. A child process can have only one
parent but a parent process may have many children. Both the parent and child processes have
the same memory image, open files, and environment strings. However, they have distinct
address spaces.
8
9
Process Dispatch
When a process in the ready state is selected for execution by the scheduler,
process dispatch operation occurs. Process dispatch is initiated according to the
scheduling algorithm used. It may happen when the CPU becomes idle and the
ready queue has processes, time quantum allotted for an ongoing process expires, a
high priority process enters the system or a hardware interrupt occurs. When a new
process is assigned to the CPU, its status is loaded from its Process Control Block
(PCB).
10
Process Preemption
Process pre-emption is an operation by
which the execution of an on-going process
is suspended and some other process is
selected by the CPU for execution. Process
pre-emption may occur when time quantum
of ongoing process expires, a high priority
process enters the ready queue or a
hardware interrupt occurs.
The preemption of executing processes in
the CPU causes a phenomenon called
context switch. Here, the context or state of
the outgoing process is stored in its PCB so
that it can be reloaded when required and
execution can be resumed from the same
point as earlier.
11
Process Blocking
The on-going process is blocked and put in the Waiting state and swapped out of the CPU, if it
needs some event to occur in order to proceed with execution. This event may be an
input/output system call since the I/O events are executed in the main memory and don't
require the processor.
Here, the operating system blocks the process, when the process itself demands so. After the
event is complete, the process again goes to the ready state.
12
Difference Between Blocking and Preemption
13
Process Termination
Process Termination is the operation of ending a process and releasing all the resources
that have been held by the process. The process ceases to exist after its termination.
Similar to process creation, there may be multiple reasons for process termination, as
follows −
● The process has completed the execution of its last instruction and so it is terminated
by the operating system.
● A child process can be terminated by its parent process if its task is no longer
relevant. The child process sends its status information to the parent process before
it terminates. Also, when a parent process is terminated, its child processes are
terminated as well as the child processes cannot run if the parent processes are
terminated.
● The process can be terminated by the operating system if there are service errors.
● A hardware malfunction may also cause process termination.
14
In most cases, process termination occurs when a process executes to completion.
The steps involved are as follows −
● The parent process issues a SIGTERM message to the child process as a signal to
initiate termination.
● On receiving this signal, the child process performs clean-up procedures like
releasing allocated resources, closing shared files, giving up access to shared
variables and tables etc.
● The child process then exits and the control returns to either the parent process
or the operating system.
15
Process Control Block (PCB) is a data structure that contains information of the
process related to it. The process control block is also known as a process descriptor or
a task control block. The PCBs of the different processes are stored in a process table
in the kernel of the operating system.
When a new process is created, the operating system initializes a process control block
for it that stores the values of the parameters associated with the process as well as
tracks the process state (i.e. new, ready, running, waiting or terminated).
16
● Process Number or ID − This shows the identification
number of the particular process.
● Process State − This specifies the process state i.e. new,
ready, running, waiting or terminated.
● Program Counter − This contains the address of the next
instruction that needs to be executed in the process.
● Registers − This specifies the registers that are used by
the process. They include accumulators, index registers,
stack pointers, general purpose registers etc.
● List of Open Files − These are the different files that are
associated with the process.
● CPU Scheduling Information − The process priority,
pointers to scheduling queues etc. is the CPU scheduling
information that is contained in the PCB. This may also
include any other scheduling parameters.
17
● Memory Management Information − The memory management
information includes the page tables or the segment tables
depending on the memory system used. It also contains the
value of the base registers, limit registers etc.
● I/O Status Information − This information includes the list of I/O
devices used by the process, the list of files etc.
● Accounting Information − The time limits, account numbers,
amount of CPU used, process numbers etc. are all a part of the
PCB accounting information.
● Location of The Process Control Block − The process control
block is kept in a memory area that is protected from the
normal user access. This is done because it contains important
process information. Some of the operating systems place the
PCB at the beginning of the kernel stack for the process as it is
a safe location.
18
Context Switching
A computer often runs multiple applications such as a browser, music player, and editor at the same
time. Since there is only one CPU, it cannot execute all processes together. Context switching allows
the CPU to switch rapidly between processes, giving each process a small time slice. This creates an
illusion that all programs are running simultaneously.
Example:
While typing notes in MS Word, music continues to play in the background.
In time-sharing systems, each process is given a fixed time called time quantum. When this time
expires, the OS must stop the current process and give the CPU to another waiting process.
Context switching ensures fair CPU allocation so that no process monopolizes the CPU.
Example:
Process P1 runs for 10 ms. After the time quantum expires, the CPU switches to Process P2.
20
3. When a Process Performs I/O Operations
I/O operations such as disk access or printer output are slow. If the CPU waits for these operations
to complete, it will be wasted.
Context switching allows the OS to block the waiting process and switch to another ready process,
keeping the CPU busy.
Example:
A program requests data from the hard disk. While waiting, the CPU executes another program.
Some processes are more important and must be executed immediately. When such a process
arrives, the OS interrupts the currently running process.
Example:
While playing a game, an incoming phone call or system interrupt must be handled immediately.
21
5. To Handle Interrupts
Interrupts are signals from hardware or software that require immediate attention.
Context switching allows the CPU to pause the current process, handle the interrupt, and then
resume execution from the same point.
Example:
Pressing a key on the keyboard interrupts the current process so that input can be processed.
Without context switching, the CPU would remain idle during waiting times. By switching between
processes, the OS ensures maximum utilization of CPU resources.
Example:
While one process waits for network data, another process is executed.
22
1. Process P0 is executing
Initially, Process P0 is using the CPU.
● Process P1 is idle (waiting).
2. Interrupt or System Call occurs
An interrupt (timer, I/O) or system call
happens.
● Control shifts from Process P0 to the
Operating System.
3. Save state of Process P0
● The OS saves the current CPU state of P0
(registers, program counter, etc.)
● This state is stored in PCB0 (Process
Control Block of P0).
23
4. Reload state of Process P1
24
7. Save state of Process P1
25
Threads
A thread is a lightweight part of a process that performs a specific task.
Multiple threads can run simultaneously within the same process.
👉 A process is a program in execution, and
👉 A thread is the smallest unit of work inside that process.
If there were no threads, the browser would freeze whenever one task takes time.
👉 Threads allow:
● Faster execution
● Better CPU usage
● Smooth user experience
26
27
Types of threads:
★ User-level threads are managed by user programs, not by the operating system kernel.
It involves thread library that contains code and functions for creating and destroying threads,
for passing messages and data between threads, for scheduling thread execution, for saving
and restoring thread contexts etc.
Characteristics
28
★ Kernel level threads are created, scheduled and managed by the kernel. Kernel threads are
mostly created for some specific functions like handling I/O request, handling interrupt etc. The
kernel maintains thread table to keep a track of all threads in the system. Kernel use system calls
to create and manage threads. The thread context switching is done by kernel based on the
scheduling policy implemented.
29
Thread Model
A thread model describes how user-level threads are mapped to kernel-level threads by the
operating system.
In simple words:
Threads improve:
30
Types of Thread Models
1. Many-to-One
2. One-to-One
3. Many-to-Many
31
1. Many-to-One
Advantages
Disadvantages
● Blocking problem
If one thread performs a blocking system call → all threads block
32
Example
33
2. One-to-One
● OS manages threads
● Each thread can run independently
● Multiple threads can run on multiple CPUs
Advantages
✔ True parallelism
✔ No blocking problem
✔ Better performance on multicore systems
Disadvantages
34
Example
● Windows
● Linux
● Modern Java threads
35
3. Many-to-Many
Advantages
Disadvantages
● Complex to implement
● Harder to manage
36
Example
37
Scheduling
Scheduling is the process by which the Operating System decides which process should get the CPU,
when, and for how long.
Since CPU is limited and many processes are waiting, OS must share CPU efficiently.
Example
Example
Typing in Word → CPU burst
Waiting for file save → no CPU
2. I/O Burst
● Time during which process performs I/O operations
Example
● Reading from disk
● Waiting for printer
3. Arrival Time
● Time when a process enters ready queue
Example
P1 arrives at time = 0
39
P2 arrives at time = 3
4. Burst Time (Execution Time)
Example
Process needs 5 ms of CPU → Burst Time = 5 ms
5. Completion Time
Formula:
40
7. Waiting Time (WT)
● Formula:
8. Response Time
Important for:
● Interactive systems
● Time-sharing OS
9. Throughput
41
Scheduler
42
1. Long-Term Scheduler (Job Scheduler)
The Long-Term Scheduler is responsible for loading processes from disk into main memory so they
can begin execution. When a new process is admitted, it moves into the Ready state.
43
2. Short-Term Scheduler (CPU Scheduler)
The Short-Term Scheduler (STS) is responsible for selecting a process from the ready queue and
assigning the CPU to it.
● Frequently selects the next process to execute from the ready state.
● Ensures no process suffers from starvation.
● Uses various CPU scheduling algorithms to decide process order.
● Maximizes CPU utilization by keeping the processor as busy as possible.
● Calls the dispatcher, which performs the actual context switch.
● It is the fastest scheduler, since it operates very frequently (often every few milliseconds).
44
3. Medium-Term Scheduler
45
46
1. Non-Preemptive Scheduling
In non-preemptive scheduling, once a process gets the CPU, it keeps the CPU until it finishes its
execution or goes into waiting (I/O).
The CPU cannot be taken away from the running process.
● Simple to implement
47
2. Preemptive Scheduling
Definition
In preemptive scheduling, the CPU can be taken away from a running process if:
Key Points
● Complex to implement
48
FCFS (First Come First Serve) Scheduling
FCFS (First Come First Serve) is a non-preemptive CPU scheduling algorithm in which the process
that arrives first in the ready queue is executed first.
Characteristics
● Non-preemptive scheduling
● Simple and easy to implement
● No starvation (every process gets CPU)
● May cause long waiting time
● Suffers from convoy effect
49
How Does FCFS Work?
The mechanics of FCFS are straightforward:
1. Arrival: Processes enter the system and are placed in a queue in the order they arrive.
2. Execution: The CPU takes the first process from the front of the queue, executes it until it is
complete, and then removes it from the queue.
3. Repeat: The CPU takes the next process in the queue and repeats the execution process.
50
Scenario 1: Processes with Same Arrival Time
Consider the following table of arrival time and burst time for three processes P1, P2 and P3
Step-by-Step Execution:
1. P1 will start first and run for 5 units of time (from 0 to 5).
2. P2 will start next and run for 3 units of time (from 5 to 8).
3. P3 will run last, executing for 8 units (from 8 to 16).
51
52
53
calculate average waiting time and turn around time:
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turnaround Time - Burst Time
54
Scenario 2: Processes with Different Arrival Times
Consider the following table of arrival time and burst time for three processes P1, P2 and P3
Step-by-Step Execution:
● P2 arrives at time 0 and runs for 3 units, so its
completion time is:
Completion Time of P2=0+3=3
● P1 arrives at time 2 but has to wait for P2 to
finish. P1 starts at time 3 and runs for 5 units.
Its completion time is:
Completion Time of P1=3+5=8
● P3 arrives at time 4 but has to wait for P1 to
finish. P3 starts at time 8 and runs for 4 units.
Its completion time is:
Completion Time of P3=8+4=12 55
56
57
58
Advantages of FCFS
Disadvantages of FCFS
● As it is a Non-preemptive CPU Scheduling Algorithm, FCFS can result in long waiting times, especially
if a long process arrives before a shorter one. This is known as the convoy effect, where shorter
processes are forced to wait behind longer processes, leading to inefficient execution.
● The average waiting time in the FCFS is much higher than in the others
● Since FCFS processes tasks in the order they arrive, short jobs may have to wait a long time if they
arrive after longer tasks, which leads to poor performance in systems with a mix of long and short
tasks.
● Processes that are at the end of the queue, have to wait longer to finish.
● It is not suitable for time-sharing operating systems where each process should get the same amount
of CPU time.
59
Problem 1
Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting time
(WT), Response time (RT), Average Turn-around time and Average Waiting time.
P1 2 2
P2 5 6
P3 0 4
P4 0 7
P5 7 4
60
61
Problem 2
Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting time
(WT), Response time (RT), Average Turn-around time and Average Waiting time.
P1 2 2
P2 0 1
P3 2 3
P4 3 5
P5 4 5
62
63
Shortest Job First or SJF CPU Scheduling
Shortest Job First (SJF) or Shortest Job Next (SJN) is a scheduling process that selects the waiting process
with the smallest execution time to execute next. This scheduling method may or may not be preemptive.
Significantly reduces the average waiting time for other processes waiting to be executed.
64
Example: Consider the following table of arrival time and burst time for three processes P1, P2 and P3.
65
66
67
● Turn Around time = Completion time - arrival time
● Waiting Time = Turnaround time - burst time
68
Example 2
P1 0 6
P2 4 10
P3 4 4
P4 8 3
69
● Turn Around time = Completion time - arrival time
● Waiting Time = Turnaround time - burst time
P1 0 6 6 6-0=6 0
P2 4 10 23 23-4=19 9
P3 4 4 10 10-4=6 2
P4 8 3 13 13-8=5 2
70
Preemptive SJF (Shortest Remaining Time First – SRTF)
In Preemptive SJF Scheduling, jobs are put into the ready queue as they come. A process with
shortest burst time begins execution. If a process with even a shorter burst time arrives, the current
process is removed or preempted from execution, and the shorter job is allocated CPU cycle.
71
72
Process ID Arrival Time Burst Time Completion Turnaround Waiting Time
Time (CT) Time (TAT) (WT)
C-A T-B
P1 2 6 15 13 7
P2 5 2 7 2 0
P3 1 8 23 22 14
P4 0 3 3 3 0
P5 4 4 10 6 2
Priority scheduling is one of the most common scheduling algorithms used by the operating system to
schedule processes based on their priority. Each process is assigned a priority value based on criteria
such as memory requirements, time requirements, other resource needs, or the ratio of average I/O to
average CPU burst time.
Priority Scheduling can be implemented in two ways:
● Non-Preemptive Priority Scheduling
● Preemptive Priority Scheduling
74
Non-Preemptive Priority Scheduling
In Non-Preemptive Priority Scheduling, the CPU is not taken away from the running process. Even if a
higher-priority process arrives, the currently running process will complete first.
Ex: A high-priority process must wait until the currently running process finishes.
Consider the following table of arrival time and burst time for three processes P1, P2 and P3:
Note: Lower number represents higher priority.
P1 P2 P3
0 4 6 12
76
Preemptive Priority Scheduling
In Preemptive Priority Scheduling, the CPU can be taken away from the currently running process if a
new process with a higher priority arrives.
Ex: A low-priority process is running, and a high-priority process arrives; the CPU immediately switches
to the high-priority process.
Example of Preemptive Priority Scheduling (Same Arrival Time)
Consider the following table of arrival time and burst time for three processes P1, P2 and P3:
Note: Higher number represents higher priority.
P3 P1 P2
0 6 13 17
78
Example of Preemptive Priority Scheduling (Different Arrival Time)
Consider the following table of arrival time and burst time for three processes P1, P2 and P3:
79
80
Round Robin Scheduling in Operating System ( preemptive CPU scheduling )
Round Robin Scheduling is a method used by operating systems to manage the execution time of multiple
processes that are competing for CPU attention. It is called "round robin" because the system rotates
through all the processes, allocating each of them a fixed time slice or "quantum", regardless of their
priority.
The primary goal of this scheduling method is to ensure that all processes are given an equal opportunity
to execute, promoting fairness among tasks.
● Process Arrival: Processes enter the system and are placed in a queue.
● Time Allocation: Each process is given a certain amount of CPU time, called a quantum.
● Execution: The process uses the CPU for the allocated time.
● Rotation: If the process completes within the time, it leaves the system. If not, it goes back to the
end of the queue.
● Repeat: The CPU continues to cycle through the queue until all processes are completed.
81
Scenario 1: Processes with Same Arrival Time
Consider the following table of arrival time and burst time for three processes P1, P2 and P3 and given Time
Quantum = 2 ms
82
83
84
Scenario 2: Processes with Different Arrival Times
Consider the following table of arrival time and burst time for three processes P1, P2 and P3 and given
Time Quantum = 2
85
86