Lecture 3 Questions? Friday, January 14 CS 470 Operating Systems - Lecture 3

advertisement
Lecture 3

Questions?
Friday, January 14
CS 470 Operating Systems - Lecture 3
1
Outline

CPU scheduling

Comparison criteria

Scheduling algorithms

First Come, First Serviced (FCFS)

Shortest Job First (SJF)

Shortest Remaining Time First (SRTF)

Priority
Friday, January 14
CS 470 Operating Systems - Lecture 3
2
CPU Scheduling


Most modern OS's multi-program, i.e., they
"run" more than one process "simultaneously",
even if there is only one CPU to maximize CPU
utilization. Goal is to keep CPU busy.
This works because programs typically
alternate between doing computations (a CPU
burst) and waiting for some I/O to be
performed (an I/O burst).
Friday, January 14
CS 470 Operating Systems - Lecture 3
3
CPU Scheduling

For typical applications, I/O bursts are much
longer than CPU bursts. Generally, CPU bursts
are very short. Programs with long CPU bursts
are said to be CPU-bound.
Friday, January 14
CS 470 Operating Systems - Lecture 3
4
CPU scheduling

Recall process state diagram. When does a
scheduling decision need to be made?
NEW
TERMINATED
dispatch
admitted
exit
READY
RUNNING
interrupt
I/O or event
completion
Friday, January 14
WAITING
I/O or event
wait
CS 470 Operating Systems - Lecture 3
5
CPU Scheduling

Scheduling decision must be made whenever
the RUNNING state is vacated.



Termination
Transition to WAITING state - I/O request, explicit
wait
Also, can make a scheduling decision when the
CPU is interrupted.

Timer interrupt

I/O or event completion interrupt.
Friday, January 14
CS 470 Operating Systems - Lecture 3
6
CPU Scheduling


If there is no scheduling done when the CPU is
interrupted, the current running process
continues in the RUNNING state. I.e., once a
process starts to run, it holds the CPU until it
terminates or waits. This is called nonpreemptive scheduling.
When an interrupt can cause the currently
running process to vacate the RUNNING state,
it is called preemptive scheduling, and the
preempted process transitions to the READY
state.
Friday, January 14
CS 470 Operating Systems - Lecture 3
7
CPU Scheduling


Examples of non-preemptive OS's: Mac OS 9
and earlier, DOS/Win 3.1 and earlier. Both had
programmer level "interrupts" that allowed
cooperating programs to simulate preemptive
scheduling.
Most modern OS's are preemptive. They are
generally better, but more complex/costly.


Need to synchronize access to shared data (Ch. 6)
Need to synchronize access to shared kernel data.
Atomic system calls would be slow.
Friday, January 14
CS 470 Operating Systems - Lecture 3
8
Comparison Criteria

How do we compare scheduling algorithms?
Some possible criteria:



CPU utilization: How busy is the CPU?
Throughput: Number of processes completed per
unit time. A measure of work done.
Turnaround time: Time from submission to
completion. A measure of how fast individual
processes complete.
Friday, January 14
CS 470 Operating Systems - Lecture 3
9
Comparison Criteria



Waiting time: Time process spends on the Ready
Queue. A measure of "wasted" time.
Response time: Time to first output, not
necessarily completion. E.g., for interactive
systems.
Goal is to try to optimize the criteria deemed
most important. Possibilities are the average,
the maximum, possibly the variance.
Friday, January 14
CS 470 Operating Systems - Lecture 3
10
Comparison Criteria


To simplify comparison, we look as scenarios
consisting of n processes, p1,...,pn, where each
process pi arrives at some time ti and has only
one CPU burst of size si. Often all of the arrival
times will be 0.
We primarily look at average waiting time and
average turnaround time.
Friday, January 14
CS 470 Operating Systems - Lecture 3
11
First Come, First Served (FCFS)

Here is a scheduling scenario:
Process
CPU burst time
Arrival time
p1
24
0
p2
3
0
p3
3
0
with arrival order p1, p2, p3

What would be the simplest scheduling
algorithm?
Friday, January 14
CS 470 Operating Systems - Lecture 3
12
First Come, First Served (FCFS)


First process to arrive gets the CPU and holds
it until it terminates or issues a wait. If the CPU
is busy on arrival, processes enter the Ready
Queue, which is serviced in FIFO order.
Algorithm is called First Come, First Service
(FCFS).
Draw a Gantt chart showing CPU usage (not to
scale):
p1 p2 p3
0 24 27 30
Friday, January 14
CS 470 Operating Systems - Lecture 3
13
First Come, First Served (FCFS)

Compute average waiting time

Compute average turnaround time


Suppose the arrival order is p 2, p3, p1?
Compute average waiting and turnaround times
again.
Clearly, FCFS generally is not optimal for
average waiting and turnaround times, and it
has high variance. Can get a convoy effect
where I/O jobs get stuck behind a CPU job.
Friday, January 14
CS 470 Operating Systems - Lecture 3
14
Shortest Job First (SJF)

As the second ordering for FCFS shows,
reordering jobs can result in lower average
waiting and turnaround times. In general, could
allow shortest job to go first (with FCFS if there
is a tie). Called Shortest Job First algoithm.
Friday, January 14
CS 470 Operating Systems - Lecture 3
15
Shortest Job First (SJF)

Here is another scenario:
Process
p1
p2
p3
p4
Friday, January 14
CPU burst time
8
4
9
5
CS 470 Operating Systems - Lecture 3
Arrival time
0
0
0
0
16
Shortest Job First (SJF)



Draw a Gantt chart and compute average
waiting time for the scenario using SJF.
Draw a Gantt chart and ompute average
waiting time for the same scenario using FCFS.
SJF is provably optimal for a fixed set of
processes. Unfortunately, generally cannot be
used in a dynamic situation, since cannot not
know burst size in advanced, though some
systems try to predict using exponential
averaging.
Friday, January 14
CS 470 Operating Systems - Lecture 3
17
Shortest Remaining Time First (SRTF)



SJF is non-preemptive. The currently running
process holds the CPU even if a shorter job is
admitted.
Can interrupt the CPU when new job arrives
and make a scheduling decision based on what
the current job has left. Called Shortest
Remaining Time First algorithm.
Using the previous scenario with arrival times at
0, 1, 2, 3, draw Gantt charts and compute
average waiting times for SJF and SRTF.
Friday, January 14
CS 470 Operating Systems - Lecture 3
18
Priority



SJF is a specifc example of a general priority
scheduling algorithm. For SJF, job length is
interpreted as the priority with shorter lengths
being higher priority. (Some systems use
higher numbers for higher priority. E.g., Unix)
Can even say that FCFS is priority based on
age, so that older processes have higher
priority than newer processes.
Priority scheduler can be either non-preemptive
or preemptive.
Friday, January 14
CS 470 Operating Systems - Lecture 3
19
Priority

Priority can be set internally or externally



Internal: measured quantities of the process. E.g.,
burst size, age, number of open files, etc.
External: quantities set outside the OS. E.g., cost,
department, importance to company, etc.
Major problem for all priority algorithms is
starvation. A continuous stream of high
priority jobs can prevent low priority jobs from
getting CPU. Solution is aging. Gradually
increase priority as process waits.
Friday, January 14
CS 470 Operating Systems - Lecture 3
20
Download