0% found this document useful (0 votes)
3 views22 pages

Lecture 4

The document discusses CPU scheduling in operating systems, outlining basic concepts, scheduling criteria, and various algorithms such as FCFS, SJF, and Round Robin. It explains the differences between preemptive and nonpreemptive scheduling, as well as the importance of metrics like CPU utilization, turnaround time, and waiting time. Additionally, it provides examples and calculations for average waiting times across different scheduling scenarios.

Uploaded by

ccd2022e030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Lecture 4

The document discusses CPU scheduling in operating systems, outlining basic concepts, scheduling criteria, and various algorithms such as FCFS, SJF, and Round Robin. It explains the differences between preemptive and nonpreemptive scheduling, as well as the importance of metrics like CPU utilization, turnaround time, and waiting time. Additionally, it provides examples and calculations for average waiting times across different scheduling scenarios.

Uploaded by

ccd2022e030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Operating Systems (CC404)

Lecture 04

CPU Scheduling

Prof. Dr. Ali Gamal


Control and Computer Eng. Dept.
Faculty of Engineering Al Maaqal University
Outlines

•Basic Concepts
•Scheduling Criteria
•Scheduling Algorithms
Objectives

•Describe various CPU scheduling


algorithms

•Assess CPU scheduling algorithms based


on scheduling criteria
Basic Concepts
• Maximum CPU utilization
obtained with
multiprogramming
• CPU–I/O Burst Cycle – Process
execution consists of a cycle of
CPU execution and I/O wait
• CPU burst followed by I/O burst
• CPU burst distribution is of
main concern
CPU Scheduler
• 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
Preemptive and
Nonpreemptive Scheduling
• When scheduling takes place only under circumstances 1
and 4, the scheduling scheme is nonpreemptive.
• Otherwise, it is preemptive.
• Under Nonpreemptive scheduling, once the CPU has been
allocated to a process, the process keeps the CPU until it
releases it either by terminating or by switching to the
waiting state.
• Virtually all modern operating systems including Windows,
MacOS, Linux, and UNIX use preemptive scheduling
algorithms.
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
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.
Scheduling Algorithm Optimization
Criteria
•Max CPU utilization
•Max throughput
•Min turnaround time
•Min waiting time
•Min response time
CPU Scheduling Algorithms
1st Case : FCFS (First Come First Served)
Suppose that the processes arrive at time 0, in the order: P1 , P3 , P2 , P4
Draw Gantt Chart and calculate the average waiting time using the given table ??

Process Burst Time Waiting time :


P1 3 P1 = 0
P2 = 8
P2 9
P3 = 3
P3 5 P4 = 17
P4 7

P1 P3 P2 P4
0 3 8 17 24

Average waiting time = (0 + 8 + 3 + 17) / 4 = 7


2nd Case : FCFS (First Come First Served)
Draw Gantt Chart and calculate the average waiting time using the given table ??

Burst Arrival Waiting time : start time – arrival time


Process P1 = 0 – 0 = 0
Time Time
P2 = 24 – 3 = 21
P1 20 0
P3 = 20 – 2 = 18
P2 12 3 P4 = 36 – 5 = 31
P3 4 2
P4 9 5

P1 P3 P2 P4
0 20 24 36 45

Average waiting time = (0 + 21 + 18 + 31) / 4 = 70 / 4


3rd Case : SJF (short job first) non-Preemptive
Draw Gantt Chart and calculate the average waiting time using the given table ??

Burst Arrival Waiting time : start time – arrival time


Process P1 = 30 – 10 = 20
Time Time
P2 = 0 – 0 = 0
P2 12 0
P3 = 22 – 3 = 19
P3 8 3
P4 = 12 – 5 = 7
P4 4 5 P5 = 16 – 12 = 4
P1 10 10
P5 6 12

P2 P4 P5 P3 P1
0 12 16 22 30 40

Average waiting time = (20 + 0 + 19 + 7 + 4) / 5 = 50 / 5 = 10


4th Case : SJF (short job first) Preemptive
Draw Gantt Chart and calculate the average waiting time using the given table ??

Burst Arrival Waiting time : start time – arrival time


Process P1 = 30 – 10 = 20
Time Time
P2 = (0 – 0) + (21 - 3) = 18
P2 12 9 0
P3 = (3 – 3) + (9 - 5) = 4
P3 8 6 3
P4 = (5 – 5) = 0
P4 4 5 P5 = 15 – 12 = 3
P1 10 10
P5 6 12

P2 P3 P4 P3 P5 P2 P1
0 3 5 9 15 21 30 40

Average waiting time = (20 + 18 + 4 + 0 + 3) / 5 = 45 / 5 = 9


Shortest-Job-First (SJF) Scheduling
• Associate with each process the length of its next CPU burst
– Use these lengths to schedule the process with the shortest time
• SJF is optimal – gives minimum average waiting time for a
given set of processes
• Preemptive version called shortest-remaining-time-first
• How do we determine the length of the next CPU burst?
– Could ask the user
– Estimate
5th Case : Priority Scheduling non-Preemptive
Draw Gantt Chart and calculate the average waiting time using the given table ??

Burst Arrival Waiting time :


Process Priority start time – arrival time
Time Time
P1 10 3 All P1 = 6
P2 1 1 Processes P2 = 0
Arrived at P3 = 16
P3 2 4 The P4 = 18
P4 1 5 Same P5 = 1
P5 5 2 Time

P2 P5 P1 P3 P4
0 1 6 16 18 19

Average waiting time = (6 + 0 + 16 + 18 + 1) / 5 = 41 / 5 = 8.2


6th Case : Priority Scheduling Preemptive
Draw Gantt Chart and calculate the average waiting time using the given table ??

Burst Arrival Waiting time :


Process Priority start time – arrival time
Time Time
P1 10 9 7 3 0.0 P1 = (0 - 0)+(2 - 1)+(9 - 4) = 6
P2 1 1 1.0
P2 = 1 – 1 = 0
P3 = 16 – 2 = 14
P3 2 4 2.0
P4 = 18 – 3 = 15
P4 1 5 3.0 P5 = 4 – 4 = 0
P5 5 2 4.0

P1 P2 P1 P5 P1 P3 P4
0 1 2 4 9 16 18 19

Average waiting time = (6 + 0 + 14 + 15 + 0) / 5 = 35 / 5 = 7


Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority
(smallest integer  highest priority)
– Preemptive
– Nonpreemptive

• SJF is priority scheduling where priority is the inverse of predicted


next CPU burst time
• Problem  Starvation – low priority processes may never execute
• Solution  Aging – as time progresses increase the priority of the
process
7th Case : Round Robin (RR)
Draw Gantt Chart and Calculate The Average Waiting Time , where Quantum = 5 ms
Process Burst Time Waiting time :
P1 12 7 2 P1 = 0 + (24 - 5) + (37 - 29) = 27
P2 8 3 P2 = 5 + (29 - 10) = 24
P3 4
P3 = 10
P4 = 14 + (32 - 19) = 27
P4 10 5
P5 = 19
P5 5

P1 P2 P3 P4 P5 P1 P2 P4 P1

0 5 10 14 19 24 29 32 37 39

Average waiting time = (27 + 24 + 10 + 27 + 19) / 5 = 107 / 5 = 21.4


RR with Q=2 and arrival time are given
Consider the set of 5 processes whose arrival time and
burst time are given below:
Process Arrival Burst
Time Time

P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
RR with Q=3 and arrival time are given
Consider the set of 5 processes whose arrival time and
burst time are given below:
Process Arrival Burst
Time Time

P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
Round Robin (RR).. General notes
• Each process gets a small unit of CPU time (time quantum
q), usually 10-100 milliseconds. After this time has
elapsed, the process is preempted and added to the end of
the ready queue.
• Timer interrupts every quantum to schedule next process
• Performance
• q large  FIFO (FCFS)
• q small  RR
• Note that q must be large with respect to context switch,
otherwise overhead is too high
Thanks for your
attention

You might also like