0% found this document useful (0 votes)
1 views

OS Week 7 CPU Scheduling

CPU scheduling is a key function of operating systems that manages processor time among tasks to ensure efficiency and fairness. It involves selecting processes from a ready queue based on various algorithms, including preemptive and non-preemptive scheduling methods. Key concepts include arrival time, burst time, and different scheduling algorithms such as First-Come, First-Served, Shortest Job First, and Round Robin.

Uploaded by

i233029
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

OS Week 7 CPU Scheduling

CPU scheduling is a key function of operating systems that manages processor time among tasks to ensure efficiency and fairness. It involves selecting processes from a ready queue based on various algorithms, including preemptive and non-preemptive scheduling methods. Key concepts include arrival time, burst time, and different scheduling algorithms such as First-Come, First-Served, Shortest Job First, and Round Robin.

Uploaded by

i233029
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Week 7 : CPU Scheduling

Operating
systems
CPU Scheduling
•CPU scheduling is a crucial feature of operating systems.

•It governs the sharing of processor time among numerous tasks.

•Ensures efficiency and fairness in executing processes.

•Helps the system meet users' performance and responsiveness requirements.


CPU Scheduling
The CPU scheduler selects from among the processes in ready queue, and allocates a
CPU core to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
•Situations 1 and 4 are non-preemptive cases where the CPU must select a new process.
•Situations 2 and 3 involve scheduling decisions where preemption is possible.
CPU Dispatcher
 Dispatcher module gives control of the CPU to the process selected by the

CPU scheduler; this involves:

 Switching context

 Switching to user mode

 Jumping to the proper location in the user program to restart that program

 Dispatch latency – time it takes for the dispatcher to stop one process and

start another running


CPU Dispatcher
Context Switching

•The process of saving the state of a currently running process and

loading the state of the next process.

•Necessary for multitasking and time-sharing operating systems.

•Involves storing CPU registers, program counter, and other process attributes.

•Adds overhead to the system, as no useful work is done during the switch.

•Faster context switching improves system efficiency and responsiveness.


Preemptive scheduling
•A process can be interrupted and moved to the ready queue before it completes

execution.

•The operating system can forcibly take the CPU away from a running process.

•Ensures better responsiveness and fairness by allowing high-priority tasks to

execute sooner.

•Used in real-time and multitasking systems.

•Examples: Round Robin, Shortest Remaining Time First (SRTF),

Priority Scheduling (Preemptive).


Non-Preemptive scheduling

•A process keeps the CPU until it completes execution or voluntarily releases it


(e.g., I/O operation).
•No forced interruption by the operating system.
•Simpler to implement but can lead to longer wait times for some processes.
•Suitable for batch processing and systems where context switching overhead
should be minimized.
•Examples: First-Come, First-Served (FCFS), Shortest Job Next (SJN),
Priority Scheduling (Non-Preemptive).
Basic Concepts
 Arrival Time: The time a process enters the ready queue.

 Burst Time : The CPU time required for a process to complete execution.

 Completion Time : The time a process finishes execution.

 Turnaround Time: Total time from process arrival to completion.

Formula: Turnaround Time=Completion Time−Arrival Time

 Waiting Time: Time a process spends in the ready queue before execution.

Formula: Waiting Time=Turnaround Time−Burst Time


CPU Scheduling
First Come First Serve

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.
FCFS : Processes with Same Arrival Time
FCFS: Processes with Same Arrival Time
FCFS: Processes with Same Arrival Time
FCFS: Processes with Same Arrival Time
FCFS: Processes with Same Arrival Time
FCFS: Calculation
FCFS Scenario 2: Different Arrival times
FCFS Scenario 2: Different Arrival times
FCFS Scenario 2: Different Arrival times
FCFS Scenario 2: Different Arrival times
FCFS Scenario 2: Different Arrival times
Shortest Job First
•Step 1: Sort by Arrival Time
•First, consider only the processes that have arrived so far
(i.e., those available in the ready queue).
•Among these, select the process with the shortest burst time
(not necessarily the one with the earliest arrival time).

•Step 2: Execution and Ready Queue Update


•Once a process finishes execution, update the ready queue with all processes
that have arrived during that time.
•Again, pick the process with the shortest burst time from the available ones.
First Come First Serve
Advantages Disadvantages

 A simple and fundamental CPU scheduling  Being non-preemptive, it can lead to long
algorithm. waiting times if a long process arrives first
 Executes processes in arrival order, (convoy effect).
ensuring fairness.  Results in higher average waiting time
 Easy to implement without complex data compared to other scheduling algorithms.
structures.  Short processes may experience delays if
 Prevents starvation as every process they follow longer ones, reducing efficiency.
gets executed.  Processes at the end of the queue wait
 Suitable for batch systems where longer longer to execute.
execution times are acceptable.  Unsuitable for time-sharing systems, as it
doesn't ensure equal CPU time distribution.
Shortest Job First
•Step 1: Sort by Arrival Time
•First, consider only the processes that have arrived so far
(i.e., those available in the ready queue).
•Among these, select the process with the shortest burst time
(not necessarily the one with the earliest arrival time).

•Step 2: Execution and Ready Queue Update


•Once a process finishes execution, update the ready queue with all processes
that have arrived during that time.
•Again, pick the process with the shortest burst time from the available ones.
Shortest Job First
Shortest Job First estimation formula
The Shortest Job First (SJF) scheduling algorithm selects the process with the shortest
burst time for execution. However, if the exact burst time is unknown, an estimation
formula predicts the next burst time using previous burst times.
Shortest Job First estimation formula
Shortest Job First estimation formula
Shortest Job First Example
Shortest Job First Example
Shortest Job First Example
Shortest Job First Example
Shortest Job First
Advantages Disadvantages

 SJF is better than the First come first serve  SJF may cause very long turn-around times
(FCFS) algorithm as it reduces the average or starvation.
waiting time.  In SJF job completion time must be known
 SJF is generally used for long term earlier.
scheduling.  Many times it becomes complicated to
 It is suitable for the jobs running in batches, predict the length of the upcoming CPU
where run times are already known. request.
 SJF is probably optimal in terms of average
Turn Around Time (TAT).
Shortest Remaining time first

 In SRTF, the process with the least time left to finish is


selected to run.
 The running process will continue until it finishes or a new
process with a shorter remaining time arrives.
 This way, the process that can finish the fastest is always given
priority.
SRTF with same arrival times Example
SRTF with same arrival times Example
SRTF with same arrival times Example
SRTF with same arrival times Example
SRTF with different arrival times Example
SRTF with different arrival times Example
SRTF with different arrival times Example
SRTF with different arrival times Example
SRTF with different arrival times Example
Shortest Remaining time first
Advantages Disadvantages

 Minimizes Average Waiting Time: SRTF  Starvation of Long Processes: Longer


reduces the average waiting time by prioritizing processes may be delayed indefinitely if
processes with the shortest remaining shorter processes keep arriving.
execution time.  Difficult to Predict Burst Times: Accurate
 Efficient for Short Processes: Shorter prediction of process burst times is challenging
processes get completed faster, improving and affects scheduling decisions.
overall system responsiveness.  High Overhead: Frequent context switching
 Ideal for Time-Critical Systems: It ensures can increase overhead and slow down system
that time-sensitive processes are executed performance.
quickly.  Not Suitable for Real-Time Systems: Real-
time tasks may suffer delays due to frequent
preemptions.
Round Robbin
Round Robbin
• 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.
Round Robbin Example
Round Robbin Example
Round Robbin Example
Round Robbin
Advantages Disadvantages

 Fairness: Each process gets an equal share  Overhead: Switching between processes can

of the CPU. lead to high overhead, especially if the

 Simplicity: The algorithm is straightforward quantum is too small.

and easy to implement.  Underutilization: If the quantum is too large, it

 Responsiveness: Round Robin can handle can cause the CPU to feel unresponsive as it

multiple processes without significant delays, waits for a process to finish its time.

making it ideal for time-sharing systems.


Priority Scheduling Example
Priority Scheduling Example
• At Time 0: All processes arrive at the same time. P3 has the
highest priority (Priority 3), so it starts execution.

• At Time 6: P3 completes execution. Among the remaining


processes, P1 (Priority 2) has a higher priority than P2,
so P1 starts execution.

• At Time 13: P1 completes execution. The only remaining


process is P2 (Priority 1), so it starts execution.

• At Time 17: P2 completes execution. All processes are now


finished.
References

https://www.baeldung.com/cs/cpu-scheduling

https://www.geeksforgeeks.org/cpu-scheduling-in-operating-systems/

You might also like