Sei sulla pagina 1di 29

CSC 302

Operating
Systems
PART TWO: PROCESS MANAGEMENT:
Lecture8: CPU Scheduling:
Lecture 8:

Scope:
CPU Scheduling.
Goals:
Examine how resources are allocated to
processes by the operating system.
Discuss different types of scheduling algorithms.

Lecture 8 CSC302 Operating Syste 2


CPU Scheduling:
Until now we have talked about processes, from
now on we will talk about resources, the things
operated upon by processes. Resources range from
cpu time to disk space to channel I/O time.
Resources fall into two classes:
Preemptible: processor or I/O channel. Can take resource
away, use it for something else, then give it back later.
Non-preemptible: once given, it cannot be reused until
process gives it back. Examples are file space, terminal,
and maybe memory.

Lecture 8 CSC302 Operating Syste 3


CPU Scheduling:

It is often that at any given time there are two


processes that can be run.
When more than one process can be run, the
operating system must decide which one to
run first.
The part of the operating system that makes
this decision is known as the scheduler and
the algorithm it uses is called the scheduling
algorithm.
Lecture 8 CSC302 Operating Syste 4
CPU Scheduling:
OS makes two related kinds of decisions about
resources:
Allocation: who gets what. Given a set of requests for
resources, which processes should be given which
resources in order to make most efficient use of the
resources? Implication is that resources are not easily
preemptible.
Scheduling: how long can they keep it. When more
resources are requested than can be granted immediately,
in which order should they be serviced? Examples are
processor scheduling (one processor, many processes),
memory scheduling in virtual memory systems. Implication
is that resource is preemptible.

Lecture 8 CSC302 Operating Syste 5


CPU Scheduling:

Processes may be in any one of three


general scheduling states:
Running.
Ready. That is, waiting for CPU time. Scheduler
and dispatcher determine transitions between this
and running state.
Blocked. Waiting for some other event: disk I/O,
message, semaphore, etc. Transitions into and
out of this state are caused by various processes.

Lecture 8 CSC302 Operating Syste 6


CPU Scheduling:

There are two parts to CPU scheduling:


The dispatcher provides the basic mechanism for
running processes.
The scheduler is a piece of OS code that decides
the priorities of processes and how long each will
run.
The scheduler is usually concerned with
deciding the policy and not the mechanism.

Lecture 8 CSC302 Operating Syste 7


CPU Scheduling:
There are various consideration when deciding a
good scheduling algorithm. Some of them include:
Fairness making sure that each process gets its fair
share of the CPU
Efficiency keep the CPU busy 100% of the time
Response time minimize response time for interactive
users
Turnaround time minimize the time batch users must wait
for output
Throughput maximize the number of jobs processed per
unit time.

Lecture 8 CSC302 Operating Syste 8


CPU Scheduling:

Some of these goals are contradictory.


Any scheduling algorithm that favors some
class of jobs hurts another class of jobs.
The CPU time is finite and to give one user
more you have to give another user less.
It should be noted that it is also difficult to
predict the behavior of all jobs. Some may be
CPU-intensive while others are I/O-intensive
and you cannot for sure determine when one
process will block.
Lecture 8 CSC302 Operating Syste 9
CPU Scheduling:

All computers have an electronic time that


causes interrupts periodically. This could be
used to ensure that no process runs for too
long.
At each clock interrupt, the operating system
decides whether the currently running
process should be allowed to continue or
whether it has had enough CPU time for the
moment.

Lecture 8 CSC302 Operating Syste 10


CPU Scheduling:
The strategy of allowing processes that are logically runnable to
be temporarily suspended is called pre-emptive scheduling as
opposed to non-preemptive scheduling where a process is
allowed to run to completion.
As seen earlier, a process can be suspended at any time and
without warning so that another process can run.
This leads to race conditions that necessitate the use of
semaphores, monitors and messages.
The non-preemptive scheduling though easy to implement, it is
not suitable for general-purpose systems with multiple competing
users because letting one process to run for as long as it wanted
would mean denying service to other processes indefinitely.

Lecture 8 CSC302 Operating Syste 11


Scheduling Algorithms

Lecture 8 CSC302 Operating Syste 12


FCFS (also called FIFO):
Non-preemptive:- run until finished.
In the simplest case this means uniprogramming.
Usually, "finished" means "blocked". One process
can use CPU while another waits on a semaphore.
Go to back of run queue when ready.
Problem: one process can monopolize CPU.
Solution: limit maximum amount of time that a
process can run without a context switch. This time
is called a time slice.

Lecture 8 CSC302 Operating Syste 13


FIFO Scheduling Algorithm

Lecture 8 CSC302 Operating Syste 14


Round Robin:

Preemptive:- run process for one time slice,


then move to back of queue.
Each process gets equal share of the CPU.
This is one of the oldest, simplest, fairest and
most widely used algorithms.
To implement this algorithm, the scheduler
maintains a list of runnable processes. When
a process uses up its quantum it is put on the
end of the list.
Lecture 8 CSC302 Operating Syste 15
Each process is assigned a time interval,
called its quantum.
If the process is still running at the expiry of
its quantum, the CPU is preempted and given
to another process.
If the process is blocked or finished before
the expiry of its quantum the CPU is given to
another process.

Lecture 8 CSC302 Operating Syste 16


Round Robin Scheduling
Algorithm:

Lecture 8 CSC302 Operating Syste 17


The major consideration in this algorithm is
the length of the quantum.
Since context switching also takes time, if the
length of the quantum is short, the CPU will
spend more time doing context switching
rather than doing actual processing work.
To improve the CPU efficiency, we should set
a longer quantum time, but not too long since
it may cause poor response time.
Lecture 8 CSC302 Operating Syste 18
Most systems use some variant of this.
What happens if the time slice is not chosen carefully?
Originally, Unix had 1 sec. time slices. Too long. Most
timesharing systems today use time slices of 10,000 - 100,000
instructions.
Implementation of priorities: run highest priority processes first,
use round-robin among processes of equal priority. Re-insert
process in run queue behind all processes of greater or equal
priority.
Even round-robin can produce bad results occasionally. Go
through example of ten processes each requiring 100 time slices.
What is the best we can do?

Lecture 8 CSC302 Operating Syste 19


Shortest Job First:
When several equally important jobs are sitting in
the input queue waiting to be started, the scheduler
should use the shortest job first.
In the figure below, we find four jobs A, B, C and D
with run times of 8,4,4 and 4 minutes respectively.
By running them in that order, the turnaround time
for A is 8 minutes, B is 12 minutes, for C is 16
minutes and for D is 20 minutes for an average of
14 minutes.
Now let us consider running these jobs using the
shortest job first as shown in figure 16(b). The turn
around times is now 4,8,12 and 20 minutes for an
average of 11 minutes. Shortest job first is optimal.
Lecture 8 CSC302 Operating Syste 20
Shortest Job First:

Lecture 8 CSC302 Operating Syste 21


Lecture 8 CSC302 Operating Syste 22
Shortest Job First:
STCF: shortest time to completion first with preemption. This
minimizes the average response time.
As an example, show two processes, one doing 1 ms
computation followed by 10 ms I/O, one doing all computation.
Suppose we use 100 ms time slice: I/O process only runs at
1/10th speed, effective I/O time is 100 ms. Suppose we use 1 ms
time slice: then compute-bound process gets interrupted 9 times
unnecessarily for each valid interrupt. STCF works quite nicely.
Unfortunately, STCF requires knowledge of the future. Instead,
we can use past performance to predict future performance.

Lecture 8 CSC302 Operating Syste 23


Priority Scheduling

Round robin makes an assumption that all


processes are equally important.
The need to take external factors into consideration
leads to priority scheduling.
Each process is assigned a priority and the runnable
process with the highest priority is allowed to run.
In a computer system there are multiple processes,
some more important than others. For example a
daemon process sending electronic mail should be
assigned a lower priority than a process displaying
video on the screen in real time.

Lecture 8 CSC302 Operating Syste 24


To prevent high priority processes from running indefinitely, the
scheduler may decrease the priority of currently running process
at each clock interrupt.
If this action causes the its priority to drop below that of the next
highest priority process, a process switch occurs.
Alternatively, each process may be assigned a maximum
quantum that it is allowed to hold the CPU continuously.
Priorities can be assigned to process statically or dynamically by
the system to achieve certain goals.
It is often convenient to group processes into priority classes and
use priority scheduling among the classes but round robin
scheduling within each class.

Lecture 8 CSC302 Operating Syste 25


Lecture 8 CSC302 Operating Syste 26
Exponential Queue:
Exponential Queue (also called "multi-level feedback queues"):
attacks both efficiency and response time problems.
Give newly runnable process a high priority and a very short time
slice. If process uses up the time slice without blocking then
decrease priority by 1 and double time slice for next time.
Go through the above example, where the initial values are 1ms
and priority 100.
Techniques like this one are called adaptive. They are common
in interactive systems.
The CTSS system (MIT, early 1960's) was the first to use
exponential queues.

Lecture 8 CSC302 Operating Syste 27


Lecture 8 CSC302 Operating Syste 28
Two main problems:

Long wait
Starvation

Lecture 8 CSC302 Operating Syste 29

Potrebbero piacerti anche