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

Lecture 3 CPU Scheduling

This document outlines a lecture on CPU scheduling in operating systems. It discusses basic concepts like CPU utilization and the CPU-I/O burst cycle. It then covers various CPU scheduling criteria and algorithms, including first-come first-served, shortest job first, priority, round robin, and real-time scheduling. Examples are provided to illustrate how each scheduling algorithm works. The document also discusses process dispatching and concepts like preemptive versus non-preemptive scheduling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Lecture 3 CPU Scheduling

This document outlines a lecture on CPU scheduling in operating systems. It discusses basic concepts like CPU utilization and the CPU-I/O burst cycle. It then covers various CPU scheduling criteria and algorithms, including first-come first-served, shortest job first, priority, round robin, and real-time scheduling. Examples are provided to illustrate how each scheduling algorithm works. The document also discusses process dispatching and concepts like preemptive versus non-preemptive scheduling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

College of Computers & IT

Dep. of IT

Operating
Systems
Lecture 3: CPU
Scheduling
2019/2020 Prepared by: Dr. Rasha Bin-Thalab
Lecture Out Lines
2

 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
 Thread Scheduling
 Multiple-Processor Scheduling
 Real-Time CPU Scheduling
 Operating Systems Examples
 Algorithm Evaluation
Basic Concepts
3

 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
4

 Short-term scheduler selects from among the processes in ready queue,


and allocates the CPU to one of them
preemptive
 Queue may be ordered in various ways
The scheduling in which a
 CPU scheduling decisions may take place when arunning
process:
process can be
interrupted if a high priority
Switches from running to waiting state.1 process enters the queue and
Switches from running to ready state.2 is allocated to the CPU
Switches from waiting to ready.3
4. Terminates

 Scheduling under 1 and 4 is nonpreemptive Non- preemptive


The scheduling in which a
 All other scheduling is preemptive running process cannot be
interrupted by any other
 Consider access to shared data
process
 Consider preemption while in kernel mode
 Consider interrupts occurring during crucial OS activities
Dispatcher
5

 Dispatcher module gives control of the CPU to the


process selected by the short-term scheduler; this
involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to
restart that program


Scheduling Criteria
6

 CPU utilization – keep the CPU as busy as possible


(Max)
 Throughput – # of processes that complete their
execution per time unit (Max)
 Turnaround time – amount of time to execute a
particular process (Min)
 Waiting time – amount of time a process has been
waiting in the ready queue (Min)
 Response time – amount of time it takes from when a
request was submitted until the first response is
produced, (for time-sharing environment) (Min)
Scheduling algorithms
7

1. First-Come, First-Served Scheduling

2. Shortest Job First Scheduling

3. Round Robin Scheduling

4. Priority Scheduling

5. Multilevel Queue Scheduling

6. Multilevel Feedback Queue Scheduling


First- Come, First-Served (FCFS)
8
Scheduling
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:
P 1
P 2
P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
9

Suppose that the processes arrive in the order:


P2 , P3 , P1
 The Gantt chart for the schedule is:
P 2
P 3
P 1

0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect - short process behind long process
 Consider one CPU-bound and many I/O-bound processes
Shortest-Job-First (SJF) Scheduling
10

 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
 The difficulty is knowing the length of the next CPU request
 Could ask the user
Example of SJF
11

ProcessArriva Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
 SJF scheduling chart
P 4
P 1
P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


Determining Length of Next CPU
12
Burst
 Can only estimate the length – should be similar to the
previous one
 Then pick process with shortest predicted next CPU burst
 Can be done by using1.the length of previous
t n  actual length of n CPU burst
th CPU
bursts, using exponential averaging
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define : n 1   t n  (1   ) n

 Commonly, α set to ½
 Preemptive version called shortest-remaining-time-
Example of Shortest-remaining-time-
13
first
 Now we add the concepts of varying arrival times and
preemption to the analysis
ProcessA Arrival TimeT Burst Time
P1 0 8
P1 P2 P4 P1 P3
P2 1 4
0 1 5 10 17 26
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart
 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 =
26/4 = 6.5 msec
Priority Scheduling
14

 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
Example of Priority Scheduling
15

ProcessAarri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
 Priority scheduling Gantt Chart

 Average waiting time = 8.2 msec


Round Robin (RR)
16

 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.
 If there are n processes in the ready queue and the
time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once. No
process waits more than (n-1)q time units.
 Timer interrupts every quantum to schedule next
process
 Performance
Example of RR with Time Quantum =
17
4
Process Burst Time
P1 24
P2 3
P3 3
 The Gantt chart is:
P 1
P 2
P 3
P 1
P 1
P 1
P 1
P1
0 4 7 10 14 18 22 26 30

Typically, higher average turnaround than SJF, but


better response
 q should be large compared to context switch time
:Exercise
18

 Consider the following table of arrival time and burst


time for three processes P0, P1 and P2. (GATE-CS-2011)
Process Arrival time Burst Time
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
 The pre-emptive shortest job first scheduling algorithm
is used. Scheduling is carried out only at arrival or
completion of processes. What is the average waiting
time for the three processes?
Solution
19

 Process P0 is allocated processor at 0 ms as Process Arrival time Burst Time


there is no other process in the ready P0 0 ms 9 ms
P1 1 ms 4 ms
queue. P0 is preempted after 1 ms as P1 P2 2 ms 9 ms
arrives at 1 ms and burst time for P1 is less
than remaining time of P0. P1 runs for 4ms.
P2 arrived at 2 ms but P1 continued as
burst time of P2 is longer than P1. After P1
completes, P0 is scheduled again as the
remaining time for P0 is less than the burst
time of P2.
P0 waits for 4 ms, P1 waits for 0 ms and P2
waits for 11 ms. So average waiting time is
(0+4+11)/3 = 5.
Time Quantum and Context Switch
20
Time
Turnaround Time Varies With The
21
Time Quantum

of CPU bursts should be 80%


shorter than q
Multilevel Queue
22
 Ready queue is partitioned into separate queues, e.g.:
 foreground (interactive)

 background (batch)

 Process permanently in a given queue


 Each queue has its own scheduling algorithm:
 foreground – RR

 background – FCFS

 Scheduling must be done between the queues:


 Fixed priority scheduling; (i.e., serve all from foreground then from

background). Possibility of starvation.


 Time slice – each queue gets a certain amount of CPU time which it can

schedule amongst its processes; i.e., 80% to foreground in RR


 20% to background in FCFS
Multilevel Queue Scheduling
23
Multilevel Feedback Queue
24

 A process can move between the various queues; aging


can be implemented this way
 Multilevel-feedback-queue scheduler defined by the
following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will enter
when that process needs service
Example of Multilevel Feedback
Queue
25 Three queues:
Q0 – RR with time quantum 8
milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS

Scheduling
A new job enters queue Q0 which is
served FCFS
When it gains CPU, job receives 8
milliseconds
If it does not finish in 8
milliseconds, job is moved to
queue Q1

You might also like