OS Unit 3
OS Unit 3
CPU Scheduling
CPU Scheduling is the process of selecting which process from the ready queue will be
allocated the CPU for execution. It is a crucial aspect of multitasking operating systems,
ensuring efficient utilization of the CPU and improving system performance.
Scheduling criteria are the key performance measures used to evaluate CPU scheduling algorithms.
These criteria help determine the efficiency, fairness, and effectiveness of a scheduling strategy.
1. CPU Utilization
Definition: The percentage of time the CPU is actively executing processes rather than being
idle.
Goal: Maximize CPU utilization to ensure efficient use of system resources.
2. Throughput
Formula:
3. Turnaround Time
Definition: The total time taken for a process to complete from submission to termination.
Formula:
4. Waiting Time
Definition: The total time a process spends in the ready queue waiting for CPU execution.
Formula:
5. Response Time
Definition: The time taken from when a process is submitted until it starts execution for the
first time.
Formula:
Goal: Lower response time is crucial for interactive systems to provide fast user feedback.
6. Fairness
Definition: Ensuring that all processes get a fair share of CPU time without starvation.
Goal: Prevent starvation by balancing short and long processes effectively.
7. Predictability
8. Priority Enforcement
CPU scheduling can be classified into two main types: pre-emptive scheduling and non-
pre-emptive scheduling. The key difference between them is whether a running process can
be forcibly interrupted or not.
1. Pre-emptive Scheduling
🔹 Definition: In pre-emptive scheduling, the CPU can be taken away from a running
process if a higher-priority process arrives or the time slice (quantum) expires.
🔹 Characteristics:
The CPU can switch to another process before the current one finishes.
It allows better resource sharing and multitasking.
It is used in time-sharing and real-time operating systems.
🔹 Example Algorithms:
✅ Round Robin (RR)
✅ Shortest Remaining Time First (SRTF)
✅ Priority Scheduling (Pre-emptive)
✅ Multilevel Queue Scheduling
🔹 Advantages:
✔ Better responsiveness for high-priority tasks.
✔ Prevents process starvation in long-running tasks.
✔ Increases CPU utilization.
🔹 Disadvantages:
✖ Frequent context switching increases overhead.
✖ More complex to implement.
2. Non-Pre-emptive Scheduling
🔹 Characteristics:
🔹 Example Algorithms:
✅ First Come First Serve (FCFS)
✅ Shortest Job First (SJF)
✅ Priority Scheduling (Non-Pre-emptive)
🔹 Advantages:
✔ No overhead of context switching.
✔ Simpler and easier to implement.
🔹 Disadvantages:
✖ Can lead to process starvation (long jobs delay short ones).
✖ Less efficient for multitasking.
Key Differences Between Pre-emptive and Non-Pre-emptive Scheduling
🔹 Definition: Multi-Level Queue (MLQ) Scheduling is a CPU scheduling algorithm that divides
processes into different priority-based queues and assigns a separate scheduling algorithm to each
queue.
Different types of processes have different scheduling needs (e.g., system processes vs. user
processes).
Some tasks require fast response times (interactive processes), while others need CPU-
intensive execution (batch jobs).
MLQ ensures efficient CPU utilization and fair resource allocation for different process
categories.
1. Structure of Multi-Level Queue Scheduling
✔ Efficient CPU Utilization – Different process types get CPU time based on their needs.
✔ Better Responsiveness – Interactive and system processes get priority.
✔ Flexibility – Each queue can have a different scheduling algorithm.
✖ Starvation – Lower-priority queues may never get CPU time if priority rules are strict.
✖ Complex Implementation – Requires managing multiple scheduling algorithms.
✖ Overhead – Context switching between queues adds CPU overhead.
Types of Schedulers in Operating Systems
Schedulers are responsible for managing process execution by selecting processes at different stages
of their lifecycle. There are three main types of schedulers:
🔹 Purpose: Controls which processes are admitted into the system for execution.
🔹 Function:
Selects processes from the job queue and loads them into main memory.
Ensures a balanced mix of CPU-bound and I/O-bound processes.
Controls the degree of multiprogramming (number of processes in memory).
🔹 Execution Frequency: Infrequent (Runs only when a new process arrives).
🔹 Example: In batch processing systems, the long-term scheduler decides which jobs enter
execution.
🔹 Purpose: Determines which process from the ready queue will execute next on the CPU.
🔹 Function:
3. Medium-Term Scheduler
🔹 Purpose: Temporarily removes processes from memory (swapping) to reduce system load.
🔹 Function:
Comparison of Schedulers
Admits processes into Selects process for CPU Swaps processes in and out of
Purpose
memory execution memory
Degree of
Controls CPU scheduling Memory utilization
multiprogramming
A process goes through multiple states during its lifecycle. These states define what the
process is doing at a given time. The main process states are:
1. New State
🔹 Definition: The process is being created but is not yet ready to execute.
🔹 What Happens?
🔹 Definition: The process is in memory and ready to run but waiting for CPU execution.
🔹 What Happens?
3. Running State
The CPU scheduler selects the process from the ready queue.
The process runs until it completes or is interrupted.
🔹 Example: A user application is currently running and performing computations.
🔹 Definition: The process is waiting for an I/O operation or some event to complete.
🔹 What Happens?
🔹 Definition: The process has finished execution and is removed from memory.
🔹 What Happens?
🔹 Definition: Context switching is the process of saving and restoring the state of a CPU so that
execution can resume from the same point later. It occurs when the CPU switches from one process to
another.
The OS saves the CPU registers (e.g., program counter, stack pointer) of the currently
running process in its Process Control Block (PCB).
The process is then moved to the Ready or Waiting state.
The OS selects the next process from the ready queue using the CPU scheduler.
It loads the saved state from the PCB of the selected process.
3. Resume Execution
The CPU resumes execution of the new process from where it left off.
When Does Context Switching Occur?
🔸 Takes CPU time – The CPU must save and restore states, which reduces efficiency.
🔸 Involves Memory Operations – Storing and restoring registers causes additional system load.
🔹 Definition: Process suspension occurs when a process is temporarily removed from memory and
moved to secondary storage (swap space). This is done to free up resources and improve system
performance.
If too many processes are in memory, the system runs out of RAM.
Suspending some processes frees up space for higher-priority processes.
If a process is waiting for slow I/O operations (e.g., disk, network), it does not need the
CPU.
Suspending the process allows the OS to run other processes efficiently.
If the system is overloaded, the OS removes inactive processes and gives CPU time to active
ones.
Prevents the system from becoming unresponsive.
When RAM is full, the OS swaps processes to disk and brings them back when needed.
This allows running more processes than available physical memory.
🔹 Suspended Ready: Process is in secondary storage but ready to execute once brought back.
🔹 Suspended Blocked: Process is waiting for an event and has been moved to secondary storage.
🔹 Definition: The Process Control Block (PCB) is a data structure in the operating system that
stores all important information about a process.
Components of PCB
The PCB contains multiple fields that store different types of process information.
Component Description
3. Program Counter (PC) Stores the memory address of the next instruction to be executed.
6. Memory Management Stores base and limit registers, page tables, and segment tables (for
Information memory allocation).
7. I/O Status Information Tracks allocated I/O devices, open files, and pending I/O requests.
Records CPU usage, execution time, process creation time, and user
8. Accounting Information
ID.
Role of PCB in Context Switching
When switching processes, the OS saves the current process's state in its PCB.
The OS loads the new process's PCB and restores its state.
This allows the OS to resume execution from where it left off.
🔹 Definition: A process address space is the range of memory addresses that a process can use
during execution.
🔹 Each process has its own separate address space, preventing interference between processes.
💡 Example: A process might use logical addresses 0x1000 to 0x2000, but the OS maps them to
physical addresses 0xA000 to 0xB000 in RAM.
1. Text (Code)
Stores executable program code (instructions).
Segment
✅ Each process gets a private address space → Prevents one process from modifying another’s
memory.
✅ Address translation (Logical → Physical) ensures memory protection.
✅ Stack and Heap can dynamically grow and shrink as needed.
🔹 Definition: Process Identification Information refers to the unique identifiers and details that the
OS maintains to track each process.
🔹 It is stored in the Process Control Block (PCB) and helps in process management, scheduling,
and resource allocation.
2. Parent Process ID (PPID) ID of the process that created (forked) this process.
3. User ID (UID) & Group ID (GID) Identifies the user and group that owns the process.
What is a Thread?
Overhead High (requires context switching) Low (switching between threads is faster)
Example A web browser is a process Each tab in the browser runs as a thread
Types of Threads
1. User-Level Threads
Managed without OS kernel intervention.
Faster to create and switch.
If one thread blocks, all threads in the process may block.
Example: Java threads managed by the JVM.
2. Kernel-Level Threads
Advantages of Multithreading
Advantages:
Disadvantages:
Disadvantages:
Advantages:
Disadvantages:
❌ Complex to implement.
❌ Thread scheduling must be managed at two levels (user & kernel).
Context Switching
Faster (no kernel mode switching) Slower (requires kernel intervention)
Speed
❌ If one thread blocks, all threads in the ✅ One thread blocking does not affect
Blocking Behavior
process block others
System Calls ❌ Cannot make system calls directly ✅ Can make system calls directly
🔹 Thread cancellation is the process of terminating a thread before it has finished execution.
🔹 Used when a thread is no longer needed, is consuming too many resources, or needs to be stopped
due to errors.
Pros:Immediate termination.
❌ Cons: Unsafe; may leave resources locked
🔹 CPU Scheduling is the process of selecting a process from the ready queue to execute on the CPU.
🔹 It ensures efficient CPU utilization, reduces waiting time, and improves system performance.
💡 Example
Process Arrival Time Burst Time Execution Order
P1 0 ms 5 ms 1st
P2 1 ms 3 ms 2nd
P3 2 ms 8 ms 3rd
P2 1 ms 4 ms 1st
P3 2 ms 8 ms 3rd
P2 1 ms 3 ms P2 (3 ms)
4. Priority Scheduling 🎖️
Each process has a priority value, and the CPU executes higher-priority processes first.
Can be preemptive or non-preemptive.
✅ Pros: Useful for real-time systems.
❌ Cons: Starvation (low-priority processes may never run).
P2 1 1 ms 1st
P3 3 2 ms 3rd
Divides processes into multiple queues, each with a different priority (e.g., foreground vs.
background).
Scheduling occurs within and between queues.
💡 Example:
Given Processes:
P2 2 3
P3 4 2
| P1 | P1 | P1 | P1 | P1 | P2 | P2 | P2 | P3 | P3 |
0 1 2 3 4 5 6 7 8 9 10
Calculations
P1 finishes at 5 ms.
P2 starts at 5 ms, finishes at 8 ms.
P3 starts at 8 ms, finishes at 10 ms.
P1 5 0 5-0=5
P2 8 2 8-2=6
P3 10 4 10 - 4 = 6
P1 5 5 5-5=0
P2 6 3 6-3=3
P3 6 2 6-2=4
AWT=(0+3+4)/3=7/3≈2.33 ms
Advantages of FCFS ✅
Disadvantages of FCFS ❌
🔹 Shortest Job Next (SJN), also known as Shortest Job First (SJF), is a scheduling algorithm that
selects the process with the shortest burst time to execute next.
🔹 It minimizes waiting time and improves system performance.
🔹 Types of SJN:
1. The process with the shortest burst time is selected for execution.
2. In non-preemptive SJN, once a process starts, it runs until completion.
3. In preemptive SJN (SRTF), if a new process arrives with a shorter burst time, the CPU
switches to it.
Given Processes:
P1 0 ms 7 ms
P2 2 ms 4 ms
P3 4 ms 1 ms
P4 5 ms 3 ms
Step-by-Step Execution:
| P1 | P1 | P3 | P4 | P4 | P4 | P2 | P2 | P2 | P2 |
0 2 4 5 6 7 8 9 10 11
Calculations
P1 0 ms 7 ms 7 ms
P2 2 ms 4 ms 11 ms
P3 4 ms 1 ms 5 ms
P4 5 ms 3 ms 8 ms
P1 7 0 7-0=7
P2 11 2 11 - 2 = 9
P3 5 4 5-4=1
P4 8 5 8-5=3
P1 7 7 7-7=0
P2 9 4 9-4=5
P3 1 1 1-1=0
P4 3 3 3-3=0
Advantages of SJN ✅
❌ Starvation – Long processes may never execute if short processes keep arriving.
❌ Difficult to implement in real-time – Requires accurate burst time prediction.
❌ Not suitable for time-sharing systems.
✔ If a new process with a shorter burst time arrives, it preempts the running process.
✔ It further reduces waiting time but adds more context switching overhead.
🔹 Round Robin (RR) is a preemptive CPU scheduling algorithm designed for time-sharing
systems.
🔹 Each process is assigned a fixed time slice (also called a time quantum).
🔹 If a process does not complete within its time quantum, it is pre-empted and moved to the back
of the ready queue.
Given Processes
P1 0 ms 5 ms
P2 1 ms 3 ms
P3 2 ms 8 ms
Time Quantum = 3 ms
Step-by-Step Execution
Calculations
P1 11 ms
P2 6 ms
P3 16 ms
P1 11 0 11 - 0 = 11
P2 6 1 6-1=5
P3 16 2 16 - 2 = 14
P1 11 5 11 - 5 = 6
P2 5 3 5-3=2
P3 14 8 14 - 8 = 6
❌ High context switching overhead – More frequent switching increases CPU overhead.
❌ Performance depends on time quantum – If the time quantum is too short, there’s excessive
switching. If too long, it behaves like FCFS.
Priority Scheduling 🚦
What is Priority Scheduling?
🔹 Priority Scheduling is a CPU scheduling algorithm where each process is assigned a priority,
and the CPU executes the highest-priority process first.
🔹 If multiple processes have the same priority, FCFS (First-Come, First-Served) is used.
🔹 Priority can be preemptive or non-preemptive.
Given Processes:
Process Arrival Time (AT) Burst Time (BT) Priority (Lower = Higher)
P1 0 ms 5 ms 2
Process Arrival Time (AT) Burst Time (BT) Priority (Lower = Higher)
P2 1 ms 3 ms 1
P3 2 ms 8 ms 4
P4 3 ms 6 ms 3
Step-by-Step Execution:
Calculations
P1 9 ms
P2 4 ms
P3 23 ms
P4 15 ms
P1 9 0 9-0=9
P2 4 1 4-1=3
P3 23 2 23 - 2 = 21
P4 15 3 15 - 3 = 12
3. Waiting Time (WT) = TAT - BT
Process TAT BT WT= (TAT - BT)
P1 9 5 9-5=4
P2 3 3 3-3=0
P3 21 8 21 - 8 = 13
P4 12 6 12 - 6 = 6
Starvation
More likely Less likely
Issue
🔹 Multi-Level Queue (MLQ) Scheduling divides the ready queue into multiple separate queues
based on process characteristics (e.g., priority, type, CPU burst time).
🔹 Each queue has its own scheduling algorithm (e.g., FCFS, SJF, RR).
🔹 Processes cannot move between queues (in traditional MLQ).
1. Processes are classified into different queues based on priority, user type, or CPU burst
requirements.
2. Each queue has a fixed priority (higher-priority queues get CPU first).
3. CPU executes processes from the highest-priority queue first.
4. If a queue becomes empty, lower-priority queues get CPU time.
5. Scheduling within each queue follows a separate policy (e.g., RR for foreground, FCFS for
background).
Example Execution
Given Processes
P1 0 ms 5 ms Q2 (RR)
P2 1 ms 8 ms Q1 (FCFS)
P3 2 ms 3 ms Q3 (FCFS)
Step-by-Step Execution
✔ Efficient for different process types (e.g., system vs. user processes).
✔ Higher-priority tasks get CPU first.
✔ Custom scheduling for each queue improves overall system performance.
❌ Starvation – Low-priority processes may never execute if high-priority queues are always full.
❌ Rigid structure – Processes cannot move between queues.
❌ Complex implementation – Requires proper classification of processes.
Solution to Starvation? 🚀
✔ Multi-Level Feedback Queue (MLFQ) – Allows processes to move between queues based on
execution history.
🔹 MLFQ is an advanced CPU scheduling algorithm that allows processes to move between
multiple priority queues based on their behavior.
🔹 It is designed to optimize response time for interactive processes while ensuring CPU-bound
processes also get executed.
🔹 Unlike Multi-Level Queue (MLQ) scheduling, where processes are fixed in one queue, MLFQ
adjusts process priority dynamically.
Example Execution
Given Processes
P1 0 ms 20 ms
P2 1 ms 5 ms
P3 2 ms 10 ms
Step-by-Step Execution
❌ Complex implementation – Requires careful tuning of time quanta and queue rules.
❌ Higher overhead due to frequent process movements between queues.
❌ May not guarantee fairness if queue parameters are not optimized.
Queue Movement Processes stay in a fixed queue Processes can move between queues
Conclusion
📌 MLFQ is an advanced scheduling algorithm that optimizes CPU usage for different process
types.
📌 It balances interactive process response time with CPU-bound process execution efficiency.
📌 Used in modern operating systems to ensure fair and efficient scheduling.
Multiprocessor Scheduling
What is Multiprocessor Scheduling?
🔹 In multiprocessor systems, multiple CPUs share the workload, requiring efficient scheduling.
🔹 Unlike single-processor scheduling, where only one process runs at a time, multiprocessor
scheduling distributes processes among multiple CPUs for better performance.
🔹 The goal is to maximize CPU utilization, balance load, and reduce execution time.
1. Load Sharing
🔹 Processes are placed in a common queue, and any available CPU executes them.
🔹 Advantages: Simple to implement.
🔹 Disadvantages: Might cause imbalance if some CPUs get more workload.
2. Load Balancing
Push Migration – A dedicated process moves tasks from overloaded CPUs to underloaded
ones.
Pull Migration – Idle CPUs pull tasks from overloaded CPUs.
🔹 Used in modern OS to optimize performance.
3. Processor Affinity
Soft Affinity – The OS tries to keep a process on the same CPU but can migrate if necessary.
Hard Affinity – The OS strictly binds a process to a specific CPU.
4. Gang Scheduling
Real-World Applications
✅ Modern OS: Windows, Linux, macOS use SMP with load balancing.
✅ Datacenters & Cloud Computing: Efficient task distribution in multi-core servers.
✅ Parallel Computing: Scientific computing, AI, and simulations benefit from gang scheduling.
Conclusion
🔹 A deadlock is a situation where a set of processes are waiting indefinitely for resources that are
held by each other.
🔹 It occurs when multiple processes hold some resources and request additional ones that are
occupied by other processes.
1️⃣ Mutual Exclusion – Only one process can use a resource at a time.
2️⃣ Hold and Wait – A process holding at least one resource is waiting for additional resources.
3️⃣ No Preemption – A resource cannot be forcibly taken from a process.
4️⃣ Circular Wait – A set of processes {P1, P2, ..., Pn} exists, where each process waits for a
resource held by the next process, forming a circular chain.
Deadlock System Model
Example of Deadlock
🔹 Processes: P1, P2
🔹 Resources: R1, R2
🔹 Initial Allocation:
Deadlock Situation:
P1 → (Holds R1) → Requests R2
P2 → (Holds R2) → Requests R1
1. Deadlock Avoidance
What is Deadlock Avoidance?
🔹 Deadlock avoidance ensures that the system never enters an unsafe state where a deadlock could
occur.
🔹 Unlike deadlock prevention, which removes conditions causing deadlock, deadlock avoidance
allows requests but decides whether to grant them based on system safety.
🔹 Requires additional information about processes (e.g., maximum resource needs).
✅ Safe State – The system can allocate resources in some order without leading to a deadlock.
❌ Unsafe State – If resources are allocated in a certain way, deadlock may occur.
If all processes hold some resources and request more without the possibility of completion,
deadlock might occur.
🔹 Modification of RAG where a dashed claim edge (→) shows possible future requests.
🔹 If granting a request creates a cycle in the RAG, it is denied to avoid deadlock.
1️⃣ Each process declares its maximum resource need at the start.
2️⃣ Whenever a process requests resources, the system:
Deadlock Prevention
What is Deadlock Prevention?
🔹 Deadlock prevention is a strategy to ensure that a deadlock never occurs by eliminating at least
one of the four Coffman’s conditions.
🔹 Unlike deadlock avoidance, which dynamically checks system states, prevention actively
removes conditions that lead to deadlocks.
Condition Description
1. Mutual
Only one process can hold a resource at a time.
Exclusion
2. Hold and Wait A process holds resources while waiting for additional ones.
A cycle of processes exists where each process is waiting for a resource held by
4. Circular Wait
another.
🔹 Require processes to request all required resources at once and allocate them before execution
starts.
✔ Example: Process must acquire all resources before execution begins (one-shot allocation).
3. Breaking No Pre-emption
🔹 Impose a strict resource request order where processes must request resources in a predefined
sequence.
✔ Example: Enforce an increasing order of resource numbering (R1, R2, R3, …).
Conclusion
🔹 Deadlock detection is a technique that allows deadlocks to occur but periodically checks the
system to identify them.
🔹 Deadlock recovery involves taking corrective actions to remove deadlocks and continue
execution.
🔹 Used in systems where deadlocks are rare, and prevention or avoidance is too expensive (e.g.,
databases, OS process scheduling).
Once a deadlock is detected, the system must recover using one of the following strategies:
Deadlock Detection & Detects and resolves No need to modify Requires recovery
Recovery deadlocks after they occur request behavior mechanisms
🔹 Used in real-world OS (e.g., Windows, Linux) where deadlocks are rare, and restarting a process is
easier than implementing complex handling.
Conclusion
Resource Resources are not available (held by other Resources are available, but never
Availability deadlocked processes). allocated to the low-priority process.
Recovery Process termination, pre-emption, rollback, Use aging (increase process priority
Methods or deadlock prevention/avoidance strategies. over time) to ensure execution.
Two processes (P1 and P2) hold resources A low-priority process waits indefinitely
Example
(R1 and R2) and are waiting for each other’s in a priority queue because high-priority
Scenario
resources, creating a cycle. processes keep getting scheduled first.
Solved Numerical on Banker's Algorithm (Safe Allocation)
Problem Statement:
A system has 5 processes (P0 to P4) and 3 resource types (A, B, C). The total available resources
are:
A = 10, B = 5, C = 7
The Maximum Need, Allocated Resources, and the Available Resources are given in the tables
below.
P0 753 010
P1 322 200
P2 902 302
P3 222 211
P4 433 002
Need=Max Need−Allocated
Process Need (A B C)
P0 743
P1 122
P2 600
P3 011
P4 431
We check whether we can satisfy the need of processes one by one while updating the available
resources.
Iteration 1
Available = (3,3,2)
Find a process whose Need ≤ Available
✔ P1 (1,2,2) ≤ (3,3,2) ✅
Allocate resources to P1, complete it, and update available resources:
New Available = (3+2, 3+0, 2+0) = (5,3,2)
✔ Safe Sequence: P1
Iteration 2
Available = (5,3,2)
✔ P3 (0,1,1) ≤ (5,3,2) ✅
Allocate resources to P3, complete it, and update available:
New Available = (5+2, 3+1, 2+1) = (7,4,3)
✔ Safe Sequence: P1 → P3
Iteration 3
Available = (7,4,3)
✔ P4 (4,3,1) ≤ (7,4,3) ✅
Allocate resources to P4, complete it, and update available:
New Available = (7+0, 4+0, 3+2) = (7,4,5)
✔ Safe Sequence: P1 → P3 → P4
Iteration 4
Available = (7,4,5)
✔ P0 (7,4,3) ≤ (7,4,5) ✅
Allocate resources to P0, complete it, and update available:
New Available = (7+0, 4+1, 5+0) = (7,5,5)
✔ Safe Sequence: P1 → P3 → P4 → P0
Iteration 5
Available = (7,5,5)
✔ P2 (6,0,0) ≤ (7,5,5) ✅
Allocate resources to P2, complete it, and update available:
New Available = (7+3, 5+0, 5+2) = (10,5,7)
✔ Safe Sequence: P1 → P3 → P4 → P0 → P2 ✅
✅ P1 → P3 → P4 → P0 → P2
✔ System is in a safe state!
Conclusion