OS Module 2
OS Module 2
Module-2
Types of process scheduling: Long-term scheduling, short-term scheduling, medium-
term scheduling. Scheduling algorithms: FCFS, SJF, Round Robin, Priority, SPN, SRT,
and HRRN Feedback. T2: Chapter 9 (Sections 9.1, 9.2)
Chapter 9
Uniprocessor Scheduling
The primary goal of scheduling is the efficient allocation of processor time to processes. This is done
to satisfy specific performance criteria:
● Response Time: Minimizing the delay for interactive users.
● Throughput: Maximizing the number of processes completed over time.
● Processor Efficiency: Keeping the CPU busy as much as possible.
EWIT
1
Operating Systems Internal designs and Principles ACSC404
● Triggers: Invoked by events that might block the current process or allow for preemption,
such as:
○ Clock interrupts (time-slice expiration).
○ I/O interrupts.
○ Operating system calls.
○ Signals (like semaphores).
Short-Term Very Frequent Which process gets the CPU right now? (Dispatching)
Scheduling Algorithm:
Short-term scheduling criteria:These are used to decide how CPU time should be given to
processes so that the system performs well. These criteria are classified in two ways:
EWIT
2
Operating Systems Internal designs and Principles ACSC404
1. User-oriented criteria
These focus on what the user experiences.
Example: response time — the time between submitting a request and seeing the first output.
Goal: give users fast and satisfactory service.
2. System-oriented criteria
These focus on overall system efficiency.
Example: throughput — the number of processes completed in a given time.
Goal: use the processor effectively and complete more work.
3. Performance-related criteria
These are measurable and quantitative.
Examples: response time, throughput, processor utilization.
4. Non-performance-related criteria
These are qualitative and harder to measure.
Example: predictability — the system should behave consistently over time, even when workload
changes
In many operating systems, each process is assigned a priority level. The CPU scheduler always
selects the process with the highest priority for execution. Instead of one ready queue, the system
may maintain multiple ready queues arranged by priority, such as:
Drawbacks:
● A drawback of pure priority scheduling is starvation.
EWIT
3
Operating Systems Internal designs and Principles ACSC404
● This happens when low-priority processes never get CPU time because higher-priority
processes keep arriving.
● To prevent starvation, the system may change priorities over time.
For example, the priority of a process may increase as it waits longer in the system (called
aging).
Example:
max[w] → means the process that has waited the longest is selected, which corresponds to FCFS
scheduling.
The decision mode determines when the scheduling algorithm selects the next process to run. It
specifies the points in time when the selection function is applied. There are two main types:
1. Non-Preemptive Scheduling
Advantages
● Simple to implement
● Low overhead (fewer context switches)
Disadvantages
2. Preemptive Scheduling
● The operating system can interrupt a running process and move it back to the Ready state.
EWIT
4
Operating Systems Internal designs and Principles ACSC404
Key Terms:
● Service Time(s): It refers to the total CPU time required by a [Link] batch jobs, it
represents the total execution [Link] interactive systems, it may represent CPU time required
for one processing cycle.
● Turnaround Time (TAT): Turnaround time is the total time a process spends in the system.
TAT=Waiting Time+Service Time
In queueing theory, it is also called residence time (Tr).
● Normalized Turnaround Time: It measures the relative delay experienced by a
process.
Normalized TAT=Service Time Turn around Time
Thus, the first process that enters the ready queue is the first one to run.
Example:
Suppose processes arrive in this order:
EWIT
5
Operating Systems Internal designs and Principles ACSC404
P1 → P2 → P3
Execution order will be:
P1 → P2 → P3
No process can interrupt another once it starts running. Therefore, FCFS is a non-preemptive
scheduling algorithm.
Advantages of FCFS
Disadvantages of FCFS
1. Convoy Effect (Problem for Short Processes)
FCFS performs better for long processes than short processes.
If a long process arrives first, all short processes must wait behind it.
Example:
If a long job runs first, short jobs that arrive later experience very large waiting and turnaround times.
This leads to poor service for short jobs.
2. Large Normalized Turnaround Time
Short processes may experience a very high normalized turnaround time, meaning they wait much
longer compared to their actual execution time.
3. Poor Handling of I/O-Bound Processes
FCFS tends to favor CPU-bound processes over I/O-bound processes.
Consider:
● One CPU-bound process (uses CPU heavily)
● Many I/O-bound processes (frequently perform I/O)
When the CPU-bound process runs:
● I/O-bound processes must wait in the ready queue
● Many I/O devices may remain idle
Later:
● I/O-bound processes quickly execute and block for I/O again
● If all processes are waiting for I/O, the CPU becomes idle
Thus FCFS may cause inefficient utilization of CPU and I/O devices. Because of these problems,
FCFS alone is not an ideal scheduling policy for a uniprocessor system. However, it is often used in
combination with priority scheduling.
Example:
● The system maintains multiple priority queues
● Each queue uses FCFS internally
Shortest Job First (SJF)is a CPU scheduling algorithm in which the process with the smallest
CPU burst time is selected for execution first.
EWIT
6
Operating Systems Internal designs and Principles ACSC404
P1 0 6
P2 0 2
P3 0 8
P4 0 3
Since all processes arrive at the same time, the scheduler selects the shortest burst time first.
Execution Order
Burst times: 2, 3, 6, 8
Order:
P2 → P4 → P1 → P3
| P2 | P4 | P1 | P3 |
0 2 5 11 19
P2 0
P4 2
P1 5
P3 11
Advantages of SJF
1. Minimum Average Waiting Time compared to many scheduling algorithms.
2. Efficient for batch processing systems.
3. Short processes are completed quickly.
4. Improves system throughput.
Disadvantages of SJF
1. Starvation Problem
Long processes may wait indefinitely if short processes keep arriving.
EWIT
7
Operating Systems Internal designs and Principles ACSC404
Round Robin (RR) is a preemptive CPU scheduling algorithm designed to reduce the waiting time of
short processes in FCFS scheduling.
Because each process gets only a limited time, this technique is called time slicing.
Working Principle
Example: P1 → P2 → P3 → P1 → P2 → P3
EWIT
8
Operating Systems Internal designs and Principles ACSC404
The time quantum (q) is the most important parameter in Round Robin scheduling.
If the quantum is very small
● Short processes finish quickly
● But many context switches occur
● Leads to high overhead
If the quantum is very large
● Preemption rarely happens
● RR behaves like FCFS
Therefore, the quantum should be slightly larger than the average CPU burst time.
Advantages of Round Robin
2. Unfairness between CPU-bound and I/O-bound processes: Round Robin may treat processes
differently depending on their behaviour
CPU-bound processes: Use the entire time quantum and Immediately return to the ready queue
I/O-bound processes: Use the CPU briefly and Quickly block for I/O
As a result:
Virtual Round Robin (VRR): To improve fairness, a modified version called Virtual Round Robin
(VRR) is used. An auxiliary queue is added.
Process movement:
Execution Rule: A process from the auxiliary queue runs only for the remaining portion of its time
quantum.
EWIT
9
Operating Systems Internal designs and Principles ACSC404
Example:
Time quantum = 4
Process used 1 unit before I/O → remaining quantum = 3
Round Robin works best in:Time-sharing systems, Interactive systems & Transaction processing
systems
Priority Scheduling
Priority Scheduling is a CPU scheduling algorithm in which each process is assigned a priority.
The CPU is allocated to the process with the highest priority first.
EWIT
10
Operating Systems Internal designs and Principles ACSC404
● Priority can be assigned internally (by the system) or externally (by the user).
P1 10 3
P2 1 1
P3 2 4
P4 1 2
Execution Order
Priority order:
1→2→3→4
P2 → P4 → P1 → P3
Gantt Chart
| P2 | P4 | P1 | P3 |
0 1 2 12 14
P2 0
EWIT
11
Operating Systems Internal designs and Principles ACSC404
P4 1
P1 2
P3 12
Average WT=40+1+2+12=3.75
Advantages
1. Important processes are executed first.
2. Suitable for real-time systems.
3. Provides flexibility by assigning priorities based on importance.
4. Improves system control over task execution.
Disadvantages
1. Starvation Problem
Low-priority processes may wait indefinitely.
2. Priority Inversion
A low-priority process may hold resources needed by a high-priority process.
● The scheduler selects the process with the shortest expected processing time.
● Short processes are allowed to jump ahead of longer processes in the ready queue.
Example queue:
P1 8
EWIT
12
Operating Systems Internal designs and Principles ACSC404
P2 4
P3 2
Execution order:
P3 → P2 → P1
Advantages of SPN
1. Reduces bias of FCFS: Unlike FCFS, short jobs do not wait behind long jobs.
2. Improved response time:Short processes are completed earlier, improving average turnaround time.
3. Efficient for batch systems: Works well when execution times are known.
Disadvantages of SPN
The system must know or estimate the execution time of each process.
Possible approaches:
The operating system estimates the next burst time using past execution times.
Simple Average
EWIT
13
Operating Systems Internal designs and Principles ACSC404
Where:
Interpretation of α
If α is large (close to 1)
● Recent observations get more importance
● Prediction changes quickly
If α is small
● Older observations influence prediction
● Prediction changes slowly
Example when α = 0.8:
Sn+1=0.8Tn+0.16Tn−1+0.032Tn−2+...
Older values have less influence.
2. Lack of Preemption: Since SPN is non-preemptive, a long process that starts executing cannot be
interrupted.
EWIT
14
Operating Systems Internal designs and Principles ACSC404
Short processes benefit greatly, while long processes may suffer large delays.
Shortest Remaining Time (SRT) is a preemptive CPU scheduling algorithm and the preemptive
version of Shortest Process Next (SPN).
● The scheduler always selects the process with the shortest remaining execution time.
● If a new process arrives with a shorter remaining time than the currently running process, the
OS preempts the current process and runs the new one.
Working Principle
Advantages of SRT
Disadvantages of SRT
Highest Response Ratio Next (HRRN) is a non-preemptive CPU scheduling algorithm that selects the
process with the highest response [Link] aim is to reduce waiting time and avoid starvation, while
still giving preference to shorter jobs.
EWIT
15
Operating Systems Internal designs and Principles ACSC404
● w increases
● Therefore R increases
Since s is in the denominator, processes with shorter service time tend to have a higher response ratio
and are executed earlier.
Advantages
Disadvantages
EWIT
16
Operating Systems Internal designs and Principles ACSC404
Feedback scheduling is used when the system does not know the execution time of processes.
Instead of predicting the remaining time (as in SPN, SRT, or HRRN), the system gives preference to
shorter jobs by penalizing processes that use the CPU for a long [Link] method is also called
Multilevel Feedback Queue Scheduling (MLFQ).
Example queues:
Working Mechanism
Step 1: Process Entry: When a new process enters the system, it is placed in the highest priority queue
(RQ₀).
Step 2: Preemption: If the process uses its entire time quantum and is not finished:
● It is preempted
● Moved to the next lower priority queue
EWIT
17
Operating Systems Internal designs and Principles ACSC404
Step 3: Further Execution: Each time the process is preempted, it moves down to a lower-priority
[Link]:
● Within each queue (except the lowest one), scheduling is done using FCFS.
● The lowest-priority queue uses Round Robin scheduling.
Short process:
● Executes quickly
● Finishes in higher-priority queues
Long process:
● After each execution or preemption, the process is fed back into a queue
● Its priority changes dynamically
A simple version uses the same time quantum for all [Link], this can cause long processes
to experience very large turnaround times.
RQ₀ 1 unit
RQ₁ 2 units
EWIT
18
Operating Systems Internal designs and Principles ACSC404
RQ₂ 4 units
RQ₃ 8 units
General rule:
Problem: Starvation
Even with increasing time quantum, long processes may still suffer starvation if many new processes
arrive.
To avoid starvation:
Advantages
Disadvantages
Problems:
EWIT
19
Operating Systems Internal designs and Principles ACSC404
P1 0 8
P2 1 4
P3 2 2
P4 3 1
Formulae
0 | P1 | 8 | P2 | 12 | P3 | 14 | P4 | 15
Completion Times
● P1 = 8
● P2 = 12
● P3 = 14
● P4 = 15
P1 0 8 8 8 0
P2 1 4 12 11 7
P3 2 2 14 12 10
P4 3 1 15 12 11
2) RR – Round Robin
EWIT
20
Operating Systems Internal designs and Principles ACSC404
Step-by-step execution
Gantt Chart
0 | P1 | 2 | P2 | 4 | P3 | 6 | P1 | 8 | P4 | 9 | P2 | 11 | P1 | 13 | P1 | 15
Completion Times
● P1 = 15
● P2 = 11
● P3 = 6
● P4 = 9
Process AT BT CT TAT WT
P1 0 8 15 15 7
P2 1 4 11 10 6
P3 2 2 6 4 2
P4 3 1 9 6 5
SPN is non-preemptive. It selects the process with the shortest expected burst time among ready
processes.
EWIT
21
Operating Systems Internal designs and Principles ACSC404
● Shortest is P4
● Then P3
● Then P2
Gantt Chart
0 | P1 | 8 | P4 | 9 | P3 | 11 | P2 | 15
Completion Times
● P1 = 8
● P4 = 9
● P3 = 11
● P2 = 15
Process AT BT CT TAT WT
P1 0 8 8 8 0
P2 1 4 15 14 10
P3 2 2 11 9 7
P4 3 1 9 6 5
SRT is the preemptive version of SPN. A newly arrived shorter process can preempt the running
process.
Gantt Chart
0 | P1 | 1 | P2 | 2 | P3 | 4 | P4 | 5 | P2 | 8 | P1 | 15
EWIT
22
Operating Systems Internal designs and Principles ACSC404
Completion Times
● P1 = 15
● P2 = 8
● P3 = 4
● P4 = 5
Process AT BT CT TAT WT
P1 0 8 15 15 7
P2 1 4 8 7 3
P3 2 2 4 2 0
P4 3 1 5 2 1
HRRN is non-preemptive. It chooses the ready process with the highest value of:
R = w+s/s
where (w) is waiting time and (s) is service time.
At time 8:
At time 9:
EWIT
23
Operating Systems Internal designs and Principles ACSC404
So choose P3.
Gantt Chart
0 | P1 | 8 | P4 | 9 | P3 | 11 | P2 | 15
Completion Times
● P1 = 8
● P4 = 9
● P3 = 11
● P2 = 15
Process AT BT CT TAT WT
P1 0 8 8 8 0
P2 1 4 15 14 10
P3 2 2 11 9 7
P4 3 1 9 6 5
6) Feedback Scheduling
Feedback scheduling uses multiple queues and dynamic priorities. New processes start in the highest-
priority queue and are demoted after preemption.
● Q0 quantum = 1
● Q1 quantum = 2
● Q2 = FCFS
● New processes enter Q0
Q0
EWIT
24
Operating Systems Internal designs and Principles ACSC404
Q1
Q2
Gantt Chart
0 | P1 | 1 | P2 | 2 | P3 | 3 | P4 | 4 | P1 | 6 | P2 | 8 | P3 | 9 | P1 | 14 | P2 | 15
Completion Times
● P1 = 14
● P2 = 15
● P3 = 9
● P4 = 4
Process AT BT CT TAT WT
P1 0 8 14 14 6
P2 1 4 15 14 10
P3 2 2 9 7 5
P4 3 1 4 1 0
EWIT
25
Operating Systems Internal designs and Principles ACSC404
EWIT
26
Operating Systems Internal designs and Principles ACSC404
Sample Questions:
1. Explain the criteria used for short-term scheduling in operating systems.
2. Discuss the decision modes in CPU scheduling with examples.
3. Explain the First-Come-First-Served (FCFS) scheduling algorithm with advantages and
disadvantages.
4. Describe the Round Robin scheduling algorithm and explain how the time quantum affects
system performance.
5. Explain the Shortest Process Next (SPN) scheduling algorithm. Why does it require burst
time estimation?
6. Discuss the Shortest Remaining Time (SRT) scheduling algorithm and its advantages over
SPN.
7. Explain the Highest Response Ratio Next (HRRN) scheduling algorithm with the response
ratio formula.
8. Describe the multilevel feedback queue scheduling algorithm and its working mechanism.
EWIT
27
Operating Systems Internal designs and Principles ACSC404
Problems:
1)Consider the following processes:
Process Arrival Time Burst Time
P1 0 5
P2 1 3
P3 2 8
P4 3 6
Using FCFS scheduling:
● Draw the Gantt chart
● Calculate waiting time and turnaround time.
2)For the following processes:
Process Arrival Time Burst Time
P1 0 6
P2 1 4
P3 2 2
P4 3 1
Using Round Robin scheduling with time quantum = 2:
● Draw the Gantt chart
● Calculate average waiting time.
3)Using SPN scheduling, schedule the following processes:
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Calculate waiting time and turnaround time.
EWIT
28
Operating Systems Internal designs and Principles ACSC404
EWIT
29