Module 2 Process Management
Module 2 Process Management
Curriculum
• Basic Concepts of Process: Process State
Transition Model, Operations, Process Control
Block, Context Switching; Introduction to
Threads, Types of Threads, Thread Models,
• Basic Concepts of Scheduling, Types of
Schedulers, Type of scheduling algorithms:
Preemptive and non preemptive (FCFS, SJF,
Priority and Round Robin)
Basic concepts of process
• A process can be thought of as a program in execution.
• 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.
• Systems consist of a collection of processes: operating-
system processes execute system code, and user processes
execute user code.
• All these processes may execute concurrently.
Basic concepts of process
• The operating system is responsible for several important aspects of
process and thread management: the creation and deletion of both
user and system processes; the scheduling of processes; and the
provision of mechanisms for synchronization, communication, and
deadlock handling for processes.
Program Process
The program contains a set of instructions The process is an instance of an executing
designed to complete a specific task. program.
A program is a passive entity as it resides in The process is an active entity as it is
the secondary memory. created during execution and loaded into
the main memory.
Program exists at a single place and The process exists for a limited period as it
continues to exist until it is deleted. gets terminated after the completion of the
task.
A program is a static entity. The process is a dynamic entity.
The program does not have any resource The process has a high resource
requirement, it only requires memory requirement, it needs resources like CPU,
space for storing the instructions. memory address, and I/O during its
lifetime.
Program Process
The program does not have any control Process has its control block called
block. Process Control Block.
The program has two logical In addition to program data, a process
components: code and data. also requires additional information
required for the management and
execution.
The program does not change itself. Many processes may execute a single
program. Their program code may be
the same but program data may be
different. these are never the same.
Program contains instructions The process is a sequence of instruction
execution.
Process State Diagram(5 states)
Process States
• As a process executes, it changes state. The state of a process is
defined in part by the current activity of that process. A process may
be in one of the following states:
• New. The process is being created.
• 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).
• Ready. The process is waiting to be assigned to a processor.
• Terminated. The process has finished execution.
Process State Diagram(6 states)
Process States
• New – The process is being created.
• Ready – The process is loaded into memory and waiting for CPU time.
• Running – The process is currently being executed by the CPU.
• Waiting (Blocked) – The process is waiting for an event or I/O to
complete.
• Suspended Ready – The process is ready to run but has been
swapped out of main memory.
• Suspended Waiting (Suspended Blocked) – The process is waiting for
an event and is also swapped out of memory.
Process State Diagram(7 states)
Process State Diagram(7 states)
• New − Contains the processes that are newly coming for execution.
• Ready − Contains the processes that are present in main memory and available for
execution.
• Running − Contains the process that is running or executing.
• Blocked − Contains the processes that are present in main memory and awaiting
an event to occur.
• Blocked Suspend − It contains the process present in secondary memory and
awaits an event to occur.
• Ready Suspend − Contains the processes that are present in secondary memory
but is available for execution as soon as it is loaded into main memory.
• Exit − Contains the processes that complete its execution.
Process Id: Process Id is a unique Id that identifies
Process Control Block each process of the system uniquely.
A process Id is assigned to each process during its
creation.
Program Counter: Program counter specifies the
address of the instruction to be executed next.
Before execution, program counter is initialized with
the address of the first instruction of the program.
After executing an instruction, value of program
counter is automatically incremented to point to the
next instruction.
This process repeats till the end of the program.
Process Control Block
• Process State: Each process goes through different states during its lifetime.
• Process state specifies the current state of the process.
• Priority: Priority specifies how urgent is to execute the process.
• Process with the highest priority is allocated the CPU first among all the
processes.
• List of Open files: This information includes the list of files opened for a process.
• PCB pointer: This field contains the address of the next PCB, which is in ready
state. This helps the operating system to hierarchically maintain an easy control
flow between parent processes and child processes.
Process Control Block
• 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, plus any condition-code information.
• Along with the program counter, this state information must be saved when an
interrupt occurs, to allow the process to be continued correctly afterward
(
• PCB serves as the repository for any information that may vary from
process to process
Process Control Block
Advantages of Process Control Block (PCB)
• Efficient Process Management: The Process Control Block allows the
operating system to manage multiple processes that ensures
maximum resource utilization.
• Quick Context Switching: In order to keep the system responsive, the
PCB allows rapid context switching by storing the state of a process.
• Resource Tracking and Allocation: The PCB tracks all the resources
allocated to a process to make sure that the resources are used
effectively and the errors are minimized.
• Simplified Process Control: The PCB provides a centralized structure
for storing process information, simplifying the control and
management of processes within the operating system.
Process Control Block
Disadvantages of Process Control Block (PCB)
• Increased Overhead: When there are a large number of processes,
maintaining them can create major overheads which consumes
memory and processing power.
• Synchronization Issues: For ensuring that the PCBs are updated
correctly and consistently with multi-threads, complicated
synchronization mechanisms are required.
• Difficulty in Modifications: Once a PCB structure is created making
changes or updates can be complex and may require significant
redesign efforts.
Process Table
PC: Program Counter, SP: Stack Pointer, PSW, Process-id
Program Status Word, CC: Condition Code, User-id
GRPs: General Purpose Registers
Process State
Scheduling info
PCB1 Accounting info
PCB2 Pinter to other data
PCB3 structures
… Open files, Open
Sockets, Memory
PCBn regions
PC, SP, PSW, CC, GRPs
CPU Switch From Process to Process
It needs more time for work and conception. It needs less time for work and conception.
Context switching takes maximum time here. Here, context switching takes minimum time.
If one process is obstructed then it will not If one thread is obstructed then it will affect the
affect the operation of another process. execution of another process.
Aspect Interrupt Handling Context Switching
Hardware/software
Trigger Scheduler decision
signal
Purpose Handle an event Switch execution
Cost Low High
Duration Very short Longer
Process change Usually no Yes
Example Keyboard interrupt Switching from P1 to P2
Introduction to Threads
• A thread is a single flow of execution and considered a
basic unit of CPU utilization.
• A process can have one or more threads.
• Each thread can run independently.
• If there are multiple CPUs or multicores within a CPU,
threads of a single process can execute in parallel
simultaneously.
• Each thread has a thread ID, and holds a program
counter (PC), a register set, and a stack on its own.
• However, code section, data section, and other
operating-system resources, such as open files and
signals are shared by all threads within a process
Thread States
• Once a thread is created, it can go to the following principal states as
follows:
• Ready: the thread is prepared to go for execution, but not yet
scheduled a CPU core
• Running: after a CPU core is allocated, the thread executes
instructions from code
• Blocked: If the thread waits for some event to complete.
Thread States
• There are four basic thread operations associated with a change in thread state:
• Spawn: Typically, when a new process is spawned, a thread for that process is
also spawned. Subsequently, a thread within a process may spawn another
thread within the same process, providing an instruction pointer and arguments
for the new thread. The new thread is provided with its own register context and
stack space and placed on the ready queue.
• Block: When a thread needs to wait for an event, it will block (saving its user
registers, program counter, and stack pointers).The processor may now turn to
the execution of another ready thread in the same or a different process.
• Unblock: When the event for which a thread is blocked occurs, the thread is
moved to the Ready queue.
• Finish: When a thread completes, its register context and stacks are deallocated.
Types of Threads
• There are two categories of threads:
• User level threads (ULTs) and kernel level threads (KLTs).
• KLTs are also called Lightweight processes (LWPs).
User Level Threads(ULT)
• User Level Thread is implemented in the user level library; they are
not created using the system calls.
• Thread switching does not need to call OS and to cause interrupt to
Kernel.
• Kernel doesn't know about the user level thread and manages them
as if they were single-threaded processes.
User Level Threads(ULT)
Advantages:
• Can be implemented on an OS that doesn't support multithreading .
• Simple representation since thread has only program counter ,
register set, stack space.
• Simple to create since no intervention of kernel.
• Thread switching is fast since no OS calls need to be made.
Disadvantages:
• No or less co-ordination among the threads and Kernel.
• If one thread causes a page fault, the entire process blocks.
Kernel Level Thread
• Kernel knows and manages the threads.
• Instead of thread table in each process, the kernel itself has thread
table (a master one) that keeps track of all the threads in the system.
• In addition, kernel also maintains the traditional process table to keep
track of the processes.
• OS kernel provides system call to create and manage threads.
Kernel Level Thread
Advantages
• Since kernel has full knowledge about the threads in the system,
scheduler may decide to give more time to processes having large
number of threads.
• Good for applications that frequently block.
Disadvantages
• Slow and inefficient.
• It requires thread control block so it is an overhead.
Thread Models
• There are four basic thread models :
1. User Level Single Thread Model
2. User Level Multi Thread Model
3. Kernel Level Single Thread Model
4. Kernel Level Multi Thread Model
User Level Single Thread Model
• Each process contains a
single thread.
• Single process is itself a
single thread.
• process table contains an
entry for every process by
maintaining its PCB.
User Level Multi Thread Model
• Each process contains multiple
threads.
• All threads of the process are
scheduled by a thread library at user
level.
• Thread switching can be done faster
than process switching.
• Thread switching is independent of
operating system which can be done
within a process.
User Level Multi Thread Model
• Blocking one thread makes blocking of entire process.
• Thread table maintains Thread Control Block of each thread of a
process.
• Thread scheduling happens within a process and not known to
Kernel.
Kernel Level Single Thread Model
It is almost absent or
It is also minimal in time It is a part of Time sharing
4 minimal in time sharing
sharing system systems.
system
It selects processes from It selects those processes It can re-introduce the process
5 pool and loads them into which are ready to into memory and execution can
memory for execution execute be continued.
CPU Scheduling
• CPU scheduling is needed under the following circumstances.
1. A newly created process joins the ready queue, and the process
needs to be immediately executed.
2. The time slice allocated to a process is over and another process
needs to get the CPU.
3. A process needs an I/O before it can proceed any further.
4. A process waits for its children to complete first before it proceeds
further.
5. A process waits for some interrupt (other than timer) and the
interrupt occurs.
6. A process completes its execution.
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 scheduler, or CPU scheduler.
• The scheduler selects a process from the processes in memory that
are ready to execute and allocates the CPU to that process.
• Scheduling Algorithms define the different ways of scheduling
processes to ensure maximum and efficient utilization of the CPU.
• Scheduling algorithms in operating systems can be preemptive or
non-preemptive.
Preemptive Scheduling
• The OS can interrupt (preempt) a currently running process and
allocate the CPU to another process (usually one with higher priority
or shorter remaining time).
• CPU can be taken away at any time
• Better responsiveness
• More context switching overhead
• Suitable for multitasking and real-time systems
• A low-priority process is running, and a high-priority process arrives
→ OS preempts the running process immediately.
Preemptive Scheduling
Advantages
• Faster response time
• Prevents CPU monopolization
• Better for interactive systems
Disadvantages
• Higher overhead due to frequent context switches
• More complex to implement
• Risk of starvation (low-priority processes may wait long)
Common Preemptive Algorithms
• Round Robin (RR), Shortest Remaining Time First (SRTF), Preemptive Priority
Scheduling, Multilevel Feedback Queue
Non-Preemptive Scheduling
• Once a process gets the CPU, it keeps it until it terminates or enters a
waiting state (I/O, sleep, etc.).
• CPU is not forcibly taken away
• Simple and low overhead
• Poor responsiveness
• A long process starts running → even if a short process arrives, it
must wait until the current process finishes.
Non-Preemptive Scheduling
Advantages
• Simple to implement
• No context switching overhead
• Predictable behavior
Disadvantages
• Poor response time
• Convoy effect (short jobs wait behind long jobs)
• Not suitable for real-time systems
Common Non-Preemptive Algorithms
• First Come First Serve (FCFS), Shortest Job First (SJF – non-
preemptive), Non-Preemptive Priority Scheduling
Feature Preemptive Scheduling Non-Preemptive Scheduling
Process The OS can interrupt a process The process runs until it finishes or
Control before completion. enters I/O.
CPU More efficient, as short and high- Less efficient, as long tasks can block
Utilizationpriority tasks get CPU sooner. others.
Context Frequent switching leads to higher No switching during execution,
Switching overhead. reducing overhead.
Ensures fair execution for all Some processes may suffer long
Fairness
processes. waiting times.
Complexity More complex to implement. Simpler and easier to manage.
Possible if low-priority processes No starvation, but long processes
Starvation
keep getting preempted. can slow the system.
Multitasking, real-time OS, time- Batch processing, simple OS, low-
Best For
sharing. overhead systems.
Terminologies Used in CPU Scheduling (CPU Scheduling Criterion)
• Throughput - The number of processes that complete their execution per unit
time.
• Arrival time - The time at which a process enters the Ready queue.
• Completion time - The time at which a process completes its execution.
• Burst time - The time takes to execute a process in the CPU.
• Turnaround time - The time difference between completion time and arrival
time. It is the time interval between the submission and completion of a
process. Turn Around Time = Completion Time – Arrival Time
• Waiting time - The time difference between turnaround time and burst time. It
is the amount of time a process spends in the ready queue waiting for its turn
with the CPU.
• CPU Utilization: Measures the percentage of time the CPU is busy processing a
task
CPU Scheduling Criterion
First Come First Serve (FCFS)
• The process that requests the CPU first is allocated the CPU first.
• Every process is scheduled based on its arrival time (time of joining
the ready queue of the CPU) and continues to run until it is complete
or voluntarily leaves CPU for some I/O operation.
• When the CPU is free, the process that has the earliest arrival time is
scheduled next.
• FCFS can be implemented using a FIFO ready queue.
• When a process enters the ready queue, its PCB is linked onto the tail
of the queue.
• When the CPU is free, it is allocated to the process at the head of the
queue.
First Come First Serve (FCFS)
Processes with Different Arrival Times
0 4 7 9
First Come First Serve (FCFS)
Disadvantages
• Since higher priority processes take over the majority of the CPU
time, it leads to starvation of lower priority processes.
• Priority inversion can occur, where a low-priority task holds a
resource needed by a higher-priority task.
• The need for dynamic adjustments to priorities adds complexity to
the scheduling algorithm.
• Assigning accurate and meaningful priorities to diverse processes can
be challenging.
Round Robin Scheduling
• 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.
Round Robin Scheduling
Process Burst Completion Arrival Time Turn Around Waiting Time
Term Time Time = CT-AT WT = TAT - BT
P1 5 15 0 15 15 - 5 = 10
P2 2 4 0 4 4-2=2
P3 6 17 0 17 17 - 6= 11
P4 4 14 0 14 14 - 4 = 10
P1 P2 P3 P4 P1 P3 P4 P1 P3
0 2 4 6 8 10 12 14 15 17
Round Robin Scheduling
• Advantages
• All processes have the same priority and are given an equal share of
CPU time.
• There is no starvation and no process is left unattended.
• Short processes generally experience lower waiting times compared
to some other scheduling algorithms.
• It is a straightforward and easy-to-implement scheduling algorithm.
Round Robin Scheduling
• Disadvantages
• It has a long average waiting time.
• Setting a short time quantum can reduce CPU utilization and reduce
performance.
• The algorithm may result in lower throughput compared to other
scheduling algorithms, especially when processes have varying burst
times.
• Short processes can get stuck waiting behind a long process, leading to
a convoy effect where short processes are delayed.
Preemptive Scheduling
• Preemptive scheduling is a CPU scheduling approach where
the operating system can interrupt (preempt) a currently
running process and switch the CPU to another process before
the first one finishes.
• A process starts running on the CPU
• If a higher-priority process arrives or a time slice expires, the
OS pauses the current process
• The CPU is reassigned to another process
• The paused process is saved and can resume later
Preemptive Scheduling
Advantages of Preemptive Scheduling:
• Improved Response Time:
Preemptive scheduling ensures that time-sensitive tasks (with high
priority) get immediate access to CPU resources, improving overall
system responsiveness.
• Fairer Process Handling:
All processes in the ready queue are given a chance to run, ensuring
fairness. Low-priority tasks will not be starved unless high priority tasks
continuously arrive.
• Effective for Time sharing Systems:
In systems where multiple users interact with the CPU, preemptive
scheduling ensures that no single process monopolizes CPU time,
keeping all users’ tasks running smoothly.
Preemptive Scheduling
Disadvantages of Preemptive Scheduling:
• Context Switching Overhead:
Frequent interruptions lead to increased context switching, which
involves saving and restoring process states, consuming CPU time.
• Complexity in Management:
Managing priority levels and determining the right moment for
preemption adds complexity to the system scheduler.
• Starvation:
Low priority processes may suffer from starvation if high priority tasks
are constantly entering the queue.
Preemptive Scheduling Non-preemptive Scheduling
A processor can be preempted to Once the processor starts its execution,
execute the different processes in the it must finish it before executing the
middle of any current process execution. other. It can’t be paused in the middle.
CPU utilization is more efficient
CPU utilization is less efficient compared
compared to Non-Preemptive
to preemptive Scheduling.
Scheduling.
Waiting and response time of Waiting and response time of the non-
preemptive Scheduling is less. preemptive Scheduling method is higher.
When any process enters the state of
Preemptive Scheduling is prioritized. The
running, the state of that process is
highest priority process is a process that
never deleted from the scheduler until it
is currently utilized.
finishes its job.
Preemptive Scheduling Non-preemptive Scheduling
Preemptive Scheduling is flexible. Non-preemptive Scheduling is rigid.
Examples: First Come First Serve,
Examples: – Shortest Remaining Time
Shortest Job First, Priority Scheduling,
First, Round Robin, etc.
etc.
Preemptive Scheduling algorithm can be
In non-preemptive scheduling process
pre-empted that is the process can be
cannot be Scheduled
Scheduled
In this process, CPU is allocated to the
In this process, the CPU is allocated to
process until it terminates or switches to
the processes for a specific time period.
the waiting state.
SJF Preemptive
• A pre-emptive SJF algorithm will preempt the currently executing
process.
• Preemptive SJF scheduling is sometimes called shortest-remaining-
time-first scheduling.
P1 P2 P4 P3 P1
SJF Preemptive
Process Arrival Burst Completion Turn Waiting
Time Time Time Around Time
Time
P1 0 18 31 31 13
P2 1 4 5 4 0
P3 2 7 14 12 5
P4 3 2 7 4 2
Average WT = (13 + 0 + 5 + 2) / 4 = 5
Turn Around time = Exit time – Arrival time Average Turn Around time = (15 + 11 + 1 + 5 + 6) / 5 = 38 / 5 = 7.6 un
Average waiting time = (11 + 8 + 0 + 0 + 4) / 5 = 23 / 5 = 4.6 unit
Waiting time = Turn Around time – Burst time
Priority Preemptive
Process Arrival Time Burst Time Priority
P1 0 9 5
P2 1 4 3
P3 2 5 1
(Lower
P4 3 7 2
number
P5 4 3 4 represents
higher
priority)
Priority Preemptive
• 0–1: Only P1(5) → runs
• 1–2: P2(3) arrives (higher priority than P1) → preempts
• 2–7: P3(1) arrives (highest priority overall) → preempts all
• 7–14: Next highest is P4(2)
• 14–18: Resume P2
• 18–21: P5(5) runs
• 21–26: Finally P1 completes
P1 P2 P3 P4 P2 P5 P1
Priority Preemptive
Turn Around time = Exit time – Arrival time Average Turn Around time = 76 / 5 = 15.2
Waiting time = Turn Around time – Burst time Average waiting time = 56 / 5 = 11.2
Round Robin Preemptive
• Round Robin scheduling is inherently preemptive because the CPU is
forcibly taken away after the expiration of the time quantum.