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

CPU Scheduling - I: Roadmap

This document provides an overview of CPU scheduling concepts and algorithms that will be covered in an operating systems class. It includes: - An outline of topics to be covered related to CPU scheduling, including basic concepts, scheduling criteria and metrics, and different scheduling algorithms like FCFS, SJF, priority, and round robin. - Descriptions of basic CPU scheduling concepts like processes, process state changes, and the CPU scheduler and dispatcher. - Explanations of scheduling criteria like CPU utilization, throughput, turnaround time, waiting time, and response time. - Details on specific scheduling algorithms like FCFS, SJF, and how they work with examples.
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)
81 views

CPU Scheduling - I: Roadmap

This document provides an overview of CPU scheduling concepts and algorithms that will be covered in an operating systems class. It includes: - An outline of topics to be covered related to CPU scheduling, including basic concepts, scheduling criteria and metrics, and different scheduling algorithms like FCFS, SJF, priority, and round robin. - Descriptions of basic CPU scheduling concepts like processes, process state changes, and the CPU scheduler and dispatcher. - Explanations of scheduling criteria like CPU utilization, throughput, turnaround time, waiting time, and response time. - Details on specific scheduling algorithms like FCFS, SJF, and how they work with examples.
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/ 5

CSE 421/521 - Operating Systems Roadmap

Fall 2011

• CPU Scheduling
Lecture - V – Basic Concepts
– Scheduling Criteria & Metrics
CPU Scheduling - I – Different Scheduling Algorithms
• FCFS
• SJF
• Priority
• RR

Tevfik Koşar

University at Buffalo
September 13th, 2011 1 2

Alternating Sequence of CPU And I/O Bursts


Basic Concepts
• Multiprogramming is needed for efficient CPU
utilization
• CPU Scheduling: deciding which processes to
execute when
• Process execution begins with a CPU burst,
followed by an I/O burst
• CPU–I/O Burst Cycle – Process execution consists
of a cycle of CPU execution and I/O wait

3 4

Histogram of CPU-burst Durations Process State


• As a process executes, it changes state
– new: The process is being created
– ready: The process is waiting to be assigned to a process
– running: Instructions are being executed
– waiting: The process is waiting for some event to occur
– terminated: The process has finished execution

5 6
CPU Scheduler Dispatcher
• Selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them • Dispatcher module gives control of the CPU to the
! short-term scheduler process selected by the short-term scheduler;
• CPU scheduling decisions may take place when a process: Its function involves:
1. Switches from running to waiting state
2. Switches from running to ready state
– switching context
3. Switches from waiting to ready – switching to user mode
4. Terminates – jumping to the proper location in the user program to
5. A new process arrives restart that program
• Scheduling under 1 and 4 is nonpreemptive/cooperative • Dispatch latency – time it takes for the dispatcher
– Once a process gets the CPU, keeps it until termination/switching to to stop one process and start another running
waiting state/release of the CPU
• All other scheduling is preemptive
– Most OS use this
– Cost associated with access to shared data
– i.e. time quota expires

7 8

Scheduling Criteria Optimization Criteria


• CPU utilization – keep the CPU as busy as possible • Maximize CPU utilization
--> maximize • Maximize throughput
• Throughput – # of processes that complete their
• Minimize turnaround time
execution per time unit -->maximize
• Minimize waiting time
• Turnaround time – amount of time passed to finish
execution of a particular process --> minimize • Minimize response time
– i.e. execution time + waiting time
• Waiting time – total amount of time a process has
been waiting in the ready queue -->minimize
• 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) -->minimize
9 10

Scheduling Metrics First-Come, First-Served (FCFS) Scheduling


! Scheduling metrics
" processes are assigned the CPU in the order they request it
" arrival time ta = time the process became “Ready” (again)
" when the running process blocks, the first “Ready” is run next
" wait time Tw = time spent waiting for the CPU " when a process gets “Ready”, it is put at the end of the queue
" service time Ts = time spent executing in the CPU
" turnaround time Tr = total time spent waiting and executing Arrival times
A
B
C

ta = Tw + Ts D
E

Arrival times
#5 arrived

Tw Ts
A B C D E Mean
Execution times
#5 executed Tr / Ts = 2.5
Tr FCFS scheduling policy Stallings, W. (2004) Operating Systems:
Internals and Design Principles (5th Edition).

11 12
FCFS Scheduling - Example
FCFS Scheduling - Example
Process Burst Time
P1 24 Suppose that the processes arrive in the order
P2 3 P2 , P3 , P1
P3 3 • The Gantt chart for the schedule is:
• Suppose that the processes arrive in the order: P1 ,
P2 , P3 P2 P3 P1
The Gantt Chart for the schedule is:
0 3 6 30
P1 P2 P3

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


0 24 27 30
• Average waiting time: (6 + 0 + 3)/3 = 3
• Much better than previous case
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Convoy effect short process behind long process
• Average waiting time: (0 + 24 + 27)/3 = 17
13 14

Shortest-Job-First (SJF) Scheduling Non-Preemptive SJF


" nonpreemptive, assumes the run times are known in advance
• Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with " among several equally important “Ready” jobs (or CPU bursts),
the scheduler picks the one that will finish the earliest
the shortest time
• Two schemes:
– nonpreemptive – once CPU given to the process it cannot be A
B
preempted until completes its CPU burst Arrival times
C
D
– preemptive – if a new process arrives with CPU burst length less E
than remaining time of current executing process, preempt.
-->This scheme is know as the Shortest-Remaining-Time-First Shortest Job
(SRTF) First (SJF)

• SJF is optimal – gives minimum average waiting time for A B C D E Mean


a given set of processes SJF

SJF scheduling policy Stallings, W. (2004) Operating Systems:


Internals and Design Principles (5th Edition).

15 16

Non-Preemptive SJF - Example Preemptive SJF (SRT)


! Shortest Remaining Time (SRT)
Process Arrival Time Burst Time " preemptive version of SJF, also assumes known run time
P1 0.0 7 " choose the process whose remaining run time is shortest
P2 2.0 4 " allows new short jobs to get good service
P3 4.0 1 A
Arrival times B
P4 5.0 4 C
D
E
• SJF (non-preemptive) Gantt Chart
P1 P3 P2 P4

0 3 7 8 12 16 A B C D E Mean

SRT scheduling policy Stallings, W. (2004) Operating Systems:


Internals and Design Principles (5th Edition).
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
17 18
Example of Preemptive SJF Priority Scheduling

Process Arrival Time Burst Time • A priority number (integer) is associated with each
P1 0.0 7 process
P2 2.0 4 • The CPU is allocated to the process with the highest
priority (smallest integer ≡ highest priority)
P3 4.0 1 – Preemptive
P4 5.0 4 – nonpreemptive
• SJF (preemptive) Gantt Chart • SJF is a priority scheduling where priority is the
predicted next CPU burst time
P1 P2 P3 P2 P4 P1
• Problem ≡ Starvation – low priority processes may never
11 16
execute
0 2 4 5 7
• Solution ≡ Aging – as time progresses increase the
priority of the process
19 20

Example of Priority Round Robin (RR)


• Each process gets a small unit of CPU time
Process Arrival Time Burst Time Priority (time quantum), usually 10-100 milliseconds.
P1 0.0 7 2 After this time has elapsed, the process is
P2 2.0 4 1 preempted and added to the end of the ready
queue.
P3 4.0 1 4
• If there are n processes in the ready queue and
P4 5.0 4 3 the time quantum is q, then each process gets
• Priority (non-preemptive) 1/n of the CPU time in chunks of at most q
– P1 --> P2 --> P4 --> P3 time units at once. No process waits more
• Priority (preemptive) than (n-1)q time units.
– ?? • Performance
– q large ⇒ FIFO
– q small ⇒ q must be large with respect to context
21
switch, otherwise overhead is too high 22

Round Robin (RR) Round Robin (RR)


" a crucial parameter is the quantum q (generally ~10–100ms)
" preemptive FCFS, based on a timeout interval, the quantum q
# q should be big compared to context switch latency (~10µs)
" the running process is interrupted by the clock and put last in a # q should be less than the longest CPU bursts, otherwise RR
FIFO “Ready” queue; then, the first “Ready” process is run degenerates to FCFS
instead
A A
Arrival times B Arrival times B
C C
D D
E E

A B C D E Mean A B C D E Mean

RR (q = 1) scheduling policy Stallings, W. (2004) Operating Systems:


Internals and Design Principles (5th Edition). RR (q = 4) scheduling policy Stallings, W. (2004) Operating Systems:
Internals and Design Principles (5th Edition).

23 24
Example of RR with Time Quantum = 20 Time Quantum and Context Switch Time

Process Burst Time


P1 53
P2 17
P3 68
P4 24
• For q=20, the Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Typically, higher average turnaround than SJF,


but better response
25 26

Turnaround Time Varies With The Time Quantum


Exercise

• Draw gantt charts, find average turnaround and waiting


times for above processes, considering:
• 1) First Come First Served Scheduling
• 2) Shortest Job First Scheduling (non-preemptive)
• 3) Shortest Job First Scheduling (preemptive)
• 4) Round-Robin Scheduling
• 5) Priority Scheduling (non-preemptive)
• 6) Priority Scheduling (preemptive)
27 28

Summary Acknowledgements

• “Operating Systems Concepts” book and supplementary


• CPU Scheduling
– Basic Concepts
Hmm. material by A. Silberschatz, P. Galvin and G. Gagne
– Scheduling Criteria & Metrics .
– Different Scheduling Algorithms • “Operating Systems: Internals and Design Principles”
• FCFS book and supplementary material by W. Stallings
• SJF
• Priority
• RR • “Modern Operating Systems” book and supplementary
material by A. Tanenbaum
• Next Lecture: Project Overview
• Reading Assignment: Chapter 5 from Silberschatz. • R. Doursat and M. Yuksel from UNR

29 30

You might also like