OS CH 3
OS CH 3
CPU Scheduling
By Kindu T.
cpu scheduling 1
CPU Scheduling
Outlines
Cpu Scheduling
Scheduling Criteria
Scheduling Algorithms
FCFS
Shortest Job First
Priority
Round Robin
Multiple Processor Scheduling
Real-time Scheduling
cpu scheduling 2
CPU Scheduling
Scheduling?
When a computer has many programs, it frequently has multiple
processes opposing for the CPU at the same time.
This situation occurs whenever two or more processes are
simultaneously in the ready state.
If only one CPU is available, a choice has to be made which process
to run next.
The operating system that makes the choice is called the scheduler
and the algorithm is called the scheduling algorithm.
cpu scheduling 3
CPU Scheduler
• Selects from among the processes in memory that are ready to execute,
and allocates the CPU to one of them.
• 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.
• Scheduling under 1 and 4 is non preemptive.
• All other scheduling is preemptive.
cpu scheduling 4
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their execution per time
unit
• Turnaround time – amount of time to execute a particular process
• Waiting time – amount of time a process has been waiting in the ready
queue
• Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time-
sharing environment)
cpu scheduling 5
CPU Scheduling
1.Non-preemptive Scheduling
•Once CPU has been allocated to a process, the process
keeps the CPU until
• Process exits OR
•Process switches to waiting state
cpu scheduling 6
CPU Scheduling
2.Preemptive Scheduling
Process can be interrupted and must release the CPU.
Need to coordinate access to shared data
Event completes: process can moves from blocked
to ready
Timer interrupts
cpu scheduling 7
CPU Scheduling
Goals for CPU scheduling:
• Maximize throughput and resource utilization.
• Need to overlap CPU and I/O activities.
• Minimize
Response time
Waiting time
Turnaround time.
• Share CPU in a fair way.
cpu scheduling 8
CPU Scheduling
Therefore our goal is to:
Maximize CPU Utilization
Maximize Throughput
Minimize Turnaround time
Minimize Waiting time
Minimize response time
cpu scheduling 9
CPU Scheduling
Scheduling Algorithms
There are different types of Algorithms:
1.First-Come First-Serve (FCFS)
2.Shortest Job First (SJF)
• Non-preemptive
• Pre-emptive
3.Priority
4.Round-Robin
cpu scheduling 10
First Come First Serve (FCFS) Scheduling
• Rule: Process that requests the CPU first is allocated the CPU
first.
• FCFS is a non-preemptive algorithm.
• Implementation - using FIFO queues
• Incoming process is added to the tail of the queue.
• Process selected for execution is taken from head of
queue.
• Performance metric - Average waiting time in queue.
• Gantt Charts are used to visualize schedules.
cpu scheduling 11
First Come First Serve (FCFS) Scheduling
• Example: Process Burst Time
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
0 3 6 30
cpu scheduling 14
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)
P1 P3 P2 P4
0 3 7 8 12 16
• Process Wait Time : Service Time - Arrival Time
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
cpu scheduling 15
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
cpu scheduling 19
Example: RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
cpu scheduling 21
Multilevel Queue
• Multiple-level queues are not an independent scheduling algorithm. They
make use of other existing algorithms to group and schedule jobs with
common characteristics.
Multiple queues are maintained for processes with common characteristics.
Each queue can have its own scheduling algorithms.
Priorities are assigned to each queue.
• For example, CPU-bound jobs can be scheduled in one queue and all I/O-
bound jobs in another queue. The Process Scheduler then alternately selects
jobs from each queue and assigns them to the CPU based on the algorithm
assigned to the queue.
cpu scheduling 22
Real-Time Scheduling
• Hard real-time systems – required to complete a critical task within a
guaranteed amount of time.
• Soft real-time computing – requires that critical processes receive
priority over less fortunate ones.
cpu scheduling 23
Example-2 Using the ff table;
1. Draw Gantt chart for preemptive SJF?
2.Waiting time for each process?
3.Average waiting time? Process Arrival time Burst time
P1 8 1
P2 5 1
P3 2 7
P4 4 3
P5 2 8
P6 4 2
P7 3 5
OS-5th [By Lake F.] 24
•