Вы находитесь на странице: 1из 9

Operating Systems Implementation Introduction 2

COMP3300 CPU scheduling is at the heart of a multiprogrammed operating


system.
The CPU scheduler maintains the list of ready processes and
CPU Scheduling determines when and which process is to be allocated to a CPU.

Department of Computer Science


The Australian National University

Eric McCreath

2004

CPU-I/O Burst Cycle 3


Histogram of CPU-burst times 4

A process during its life will go through a cycle of CPU CPU Burst times will vary greatly from process to process.
execution and I/O waiting. However, generally there will be considerably more short CPU
bursts.
reset variables
CPU Burst read from file frequency

I/O Burst

CPU Burst count data elements


read from file

I/O Burst

CPU Burst sort elements


write to file
I/O Burst

CPU burst duration


time
Scheduling 5 Scheduling 6

There are two types of schedulers: A nonpreemptive scheduler may switch processes when one of
nonpreemptive - The CPU is not 'forcefully' taken from the the following events occur:
process. A running process completes its CPU burst and is waiting on
preemptive - The CPU may be forcefully taken from the some event.
process and switched to another process. A running process terminates.
A preemptive scheduler, in addition to the above conditions,
may switch processes when :
A timer interrupt occurs.
A waiting process completes its waiting.

Scheduling 7 Dispatcher 8

With a nonpreemptive scheduler a process keeps the CPU until The dispatcher must:
it either terminates or switches to a waiting state. switch context,
Both Microsoft Windows 3.1 and the Apple Macintosh OS(old change back to user mode, and
ones) used a nonpreemptive scheduler.
restart the switched process at the correct location.
One advantage of using a nonpreemptive scheduler is it
simplifies access to shared data structures. This is because the The dispatcher should be as fast as possible. The dispatch
CPU will not be removed from the process while shared data is latency is the time it takes to stop one process and start another.
in an inconsistent state.
Operating Systems with preemptive schedulers require
mechanisms to coordinate shared data.
Scheduling Criteria 9 Scheduling Criteria 10

A variety of criteria are used to compare different scheduling It is desirable to maximize:


algorithms. These include:
CPU utilization, and
CPU utilization - The percentage of time the CPU is in use.
throughput.
Throughput - The rate at which processes are being completed.
And minimize :
Turnaround time - The interval of time from the starting a
turnaround time,
process to completing the process.
waiting time, and
Wait time - The amount of time a process spends in the ready
queue. response time.
Response time - The amount of time it takes a process to start In some cases you may wish to minimize the maximum value
responding. This does not include the time to output the rather than just minimize the average case.
response. Reducing the variance may also be desirable.

FCFS Scheduling 11 SJF Scheduling 12

The simplest scheduling algorithm is the First Come First Serve The shortest-job-first (SJF) algorithm schedules the process
(FCFS) scheduler. This is a nonpreemptive scheduler. with the shortest next CPU burst time.
It works by maintaining a FIFO queue. When the running If the next CPU burst time of two process is identical then a
process has completed its CPU burst the next process on the FCFS scheduler will be used to break the tie.
ready queue is dispatched. Process that are either new or have
completed their IO burst are placed on the end of the queue. The SJF scheduler may be nonpreemptive or preemptive.
A disadvantage of a FCFS scheduler is IO bound processes will
build up behind CPU bound process. This is known as the
convoy effect. This will reduce both IO and CPU utilization.

CPU CPU
15
6 8 15 8 6
3 3
SJF Scheduling 13 SJF scheduling 14

The SJF scheduler is provably optimal with respect to There is generally no way of knowing the length of the next
minimizing average waiting time for a set of processes. CPU burst. Hence, it is impossible to implement a SJF CPU
A preemptive SJF may preempt the running process. This will scheduler.
occur if a process that has just become ready to run has a shorter However, the operating system may attempt to predict the next
next CPU burst time than the time to complete the running CPU burst time of a process. This provides a method of
process' CPU burst. approximating a SJF scheduler.
The preemptive SJF scheduler is sometimes called the shortest An exponential average of the lengths of previous CPU bursts
remaining time first scheduler. may be used to predict the next CPU burst time.
Let be the measured length of the nth CPU burst.

Given 0 1 we may calculate (our prediction of the


n 1


n+1 CPU burst) by:


1








n 1 n n


Priority Scheduling 15 Priority Scheduling 16

A priority may be associated with each process. The process A major problem with priority scheduling is starvation.
with the highest priority is scheduled. FCFS algorithm may be Starvation is where processes with low priority never get a
used with processes with the same priority. chance to run. This is also a problem with SJF-scheduling.
Priorities may be defined either: Aging is one way of addressing starvation. Aging gradually
internally - These use measurable quantities to calculate a increases the priority of processes that are waiting in the ready
priority. queue.
10
externally - These are determined externally from the 2
operating system.
Priority scheduling may be either preemptive or nonpreemptive.
CPU
In preemptive priority scheduling the priority of a process that
became ready is compared with the priority of the running 1 6
8
process. If the process that became ready has a higher priority 10
then it is dispatched.
Round Robin Scheduling 17 Round Robin Scheduling 18

A round robin scheduler is a FCFS scheduling where process About 80% of the CPU bursts should be shorter than the time
are preempted after a fix time slice(or time quantum) and placed quantum. This will keep the number of extra context switches
at the end of the FIFO queue. to a minimum.
The ready queue may be thought of as a circular queue. Question: Suppose there was two processes both with 100
CPU bursts of 1 ms each. How many context switches would
The average wait time for a RR scheduler is comparatively
occur if the time quantum was set to 0.9ms ? What if it was set
longer than SJF scheduling.
to 1.1 ms ?
A round robin scheduler is useful for time-sharing systems.
If the time quantum is infinite then RR scheduling is the same
as FCFS scheduling.
Determining the size of the time quantum is critical. The smaller
the time quantum the greater the context switching overhead.

Multilevel Queue Scheduling 19 Multilevel Queue Scheduling 20

Different types of processes have different scheduling A process will not migrate between queues during its life.
requirements or needs. One approach to cater for these different
There is a variety of ways the different levels may be scheduled
needs is to partition all the processes up into different groups.
between each other, these include:
Where each group has its own ready queue and scheduling
algorithm. There is also a scheduler that schedules between the Higher priority queues may have absolute priority over the
different groups. This approach is known as Multilevel Queue lower priority queues.
scheduling. The CPU time may be divided up between the different
queues.
System Processes - FCFS

Absolute
Priority Interactive Processes - RR

Batch Processes - FCFS


Multilevel Feedback Queue Scheduling 21 Multiple-Processor Scheduling 22

In multilevel feedback queue scheduling processes may migrate Having more than one processor introduces considerable
between queues. complexity into both the scheduler and the OS as a whole.
Systems may either be:
homogeneous - Processors are identical in terms of
functionality.
RR (quantum = 20)
heterogeneous - Processors are different in terms of
functionality.

FCFS

Multiple-Processor Scheduling 23 Real-Time Scheduling 24

Processes may go into a common ready queue and then be Hard real-time systems require guarantees on completion times
scheduled onto any available processor. To accomplish this two for critical tasks. This requires upper bounds on completion
approaches are used: times for operating system functions.
each processor is self-scheduled, (symmetric multiprocessing) This is impossible with virtual memory or secondary storage.
one processor is appointed the scheduler. (master slave, Generally hard real-time systems involve special purpose
asymmetric multiprocessing) software/hardware dedicated to the critical task.
Soft real-time systems are less demanding. Critical processes are
given priority by the scheduler over non-critical processes. The
priority of critical tasks must not degrade over time.
Real-Time Scheduling 25 Real-Time Scheduling 26

Many operating systems wait for system calls to either complete All the kernel data structures must be protected via
or I/O block, before doing a context switch. synchronisation to make the entire kernel preemptable.
This causes some problems for soft real-time schedulers. As a Suppose a low priority process B is preempted during a system
critical process may be ready to preempt a lower priority call which is accessing kernel data structure D. If a high priority
process, however, it is unable to as the lower priority process is process A also wishes to access D then A must be blocked until
in the middle of a long system call. B completes its access to D. This would mean a high priority
To keep the dispatch latency short critical processes must be process is waiting for a low priority process, which has little
able to preempt lower-priority processes during system calls. chance of being scheduled. This is know as priority inversion.
This may be achieved by: A priority-inheritance protocol may be used to address this
including preemption points within system calls(this creates problem.
'safe' points), or
make the entire kernel preemptable.

Evaluating a Scheduler 27 Deterministic Modeling 28

There are a number of different approaches that can be taken to Deterministic Modeling is an analytic evaluation approach. This
evaluate a scheduler. These include: approach calculates the performance of the scheduling
Deterministic Modeling, algorithm for a particular predetermined workload.

Queuing Models, Advantages include:


simple and fast,
Simulations, and
gives exact comparisons between algorithms,
Implementation.
helps describe and provide examples of algorithms, and
We will now look at these approaches in turn.
shows trends and helps people gain some intutions about different
algorithms.
Disadvantages include:
results are too specific,
requires too much exact knowledge.
Queuing Models 29 Queuing Models 30

The distribution of CPU and I/O bursts may be measured. From Little's formula:
these distributions average throughput, utilization, waiting time,
n W


etc for different scheduling algorithms may be calculated. Average wait time in the queue
In queuing-network analysis the computer system is described
Average queue length
as a network of servers. Knowing arrival and service rates of Average arrival rate
different servers we may compute average queue lengths, and
average wait time, etc for different algorithms.

ready queue

Queuing Models 31 Simulations 32

Disadvantages of queuing models: Important parts of the operating system may be simulated to
Limited classes of algorithms and distribution that may be access different scheduling algorithms. Statistics regarding a
handled. scheduler's performance may be gathered.

Often requires independence assumptions. Data to drive the simulator may be:

Only provides an approximation of a real system, hence, the distribution driven, or


accuracy of results may be questionable. trace tapes.
Implementation 33

The most accurate way of accessing a scheduling algorithm is to


implement it and evaluate ti with a real operating system.
Disadvantages include:
costly,
a computer systems environment will change for different sets
of users, and
the way people use a computer may change in response to the
scheduler.

Вам также может понравиться