Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CH 5. CPUscheduling

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Chapter 5

CPU Scheduling
CPU Scheduling
• Scheduling is a task of selecting a process from
a ready queue and letting it run on the CPU
– Done by a scheduler

• Types of Scheduling
– Non-preemptive scheduler
• Process remains scheduled until voluntarily relinquishes
CPU

– Preemptive scheduler
• Process may be descheduled at any time
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their
execution per time unit
• Turnaround time – The interval from the time of
submission of a process to the time of completion.
– Sum of the periods spent waiting to get into memory, waiting
in the ready queue, executing on the CPU, and doing I/O.
• Waiting time – amount of time a process has been
waiting in the ready queue
• 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)
Gantt Chart
• Illustrates how processes/jobs are scheduled
over time on CPU
A B C
0 10 12 16
Time
First- Come, First-Served (FCFS) Scheduling
• Jobs are executed on first come, first serve basis.
• Easy to understand and implement.
• Poor in performance as average wait time is high.
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:
P1 P2 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.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
• The Gantt chart for the schedule is:

P2 P3 P1
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
• Note also that the FCFS scheduling algorithm is nonpreemptive.
– Troublesome for timesharing systems
Shortest-Job-First (SJF) Scheduling
• 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

Processesl Ti mBurst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

• SJF scheduling chart


P4 P1 P3 P2
0 3 9 16 24

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


Shortest-Remaining-Time-First
• The SJF algorithm can be either preemptive or
nonpreemptive.
• A preemptive SJF algorithm will preempt the
currently executing process, if the newly arrived
process may be shorter than what is left of the
currently executing process.
• whereas a nonpreemptive SJF algorithm will allow
the currently running process to finish its CPU burst.
• Preemptive SJF scheduling is sometimes called
shortest-remaining-time-first scheduling
Example of Shortest-remaining-time-first

• Now we add the concepts of varying arrival times and


preemption to the analysis
ProcessA arr Arrival Time T Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
• Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26

• Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 =


6.5 ms
• What is AWT for nonprimitivF ?
Priority Scheduling
• 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

• Problem  Starvation – low priority processes


may never execute
• Solution  Aging – as time progresses increase
the priority of the process
Priority scheduling
• E.g, Consider the following set of processes, assumed
to have arrived at time 0 in the order P1, P2, · · ·, P5,
with the length of the CPU burst.
Round Robin (RR)
• 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
– q large  FIFO
– q small  q must be large with respect to context switch,
otherwise overhead is too high
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Multilevel Queue
• Ready queue is partitioned into separate queues, e.g.:
– foreground (interactive)
– background (batch)
• Process permanently stay in a given queue
• Each queue has its own scheduling algorithm:
– foreground – RR
– background – FCFS
• Scheduling must be done between the queues:
– Fixed priority preemptive 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;
• Like, 80% to foreground in RR
• 20% to background in FCFS
Multilevel Feedback Queue
• A process can move between the various queues;
• A process that waits too long in a lower-priority
queue may be moved to a higher-priority queue.
– Aging can be implemented to prevent starvation.
• 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
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS

• Scheduling
– A new job enters queue Q0
• When it gains CPU, job receives 8
milliseconds
• If it does not finish in 8 milliseconds, job is
moved to queue Q1
– At Q1 job is again served and receives 16
additional milliseconds
• If it still does not complete, it is preempted
and moved to queue Q2
Multiple-Processor Scheduling
• CPU scheduling becomes more complex when
multiple CPUs are Involved
• Approaches to Multiple-processor scheduling
– Asymmetric multiprocessing, in which one processor is
the master, controlling all activities and running all
kernel code, while the other runs only user code.
• This approach is relatively simple, as there is no need to share
critical system data.
– Symmetric multiprocessing, SMP, where each
processor schedules its own jobs, either from a
common ready queue or from separate ready queues
for each processor.
• Currently, most common (Windows, Linux, and Mac OS X)

Processor affinity
Processors contain cache memory, which speeds up
repeated accesses to the same memory locations.
• If a process has to switch from one processor to another
each time it got a time slice, the data in the cache ( for
that process ) would have to be invalidated and re-loaded
from main memory, thereby losing the benefit of the
cache.
• Therefore systems attempt to keep processes on the
same processor, via processor affinity.
–  Soft affinity - the system attempts to keep processes on the
same processor but makes no guarantees.
– Hard affinity - a process specifies that it is not to be moved
between processors .E.g. Linux and some other Oses  
NUMA and CPU Scheduling
• Main memory architecture can also affect
process affinity.
• The CPUs on a chip or board can access the
memory on that board faster than they can
access memory on other boards in the system.
• This is known as NUMA ( Non-Uniform Memory
Access)
• If a process has an affinity for a particular CPU,
then it should preferentially be assigned memory
storage in "local" fast access areas.
Multiple-Processor Scheduling – Load Balancing

• An important goal in a multiprocessor system is to balance the load


between processors,
– Therefore, one processor won't be sitting idle while another is
overloaded.
• Load balancing attempts to keep workload evenly distributed
• Balancing can be achieved through either push migration or pull
migration:
– Push migration – periodic task checks load on each processor, and if
found pushes task from overloaded CPU to other CPUs
– Pull migration – idle processors pulls waiting task from busy processor
– Push and pull migration are not mutually exclusive.
• Note that moving processes from CPU to CPU to achieve load
balancing works against the principle of processor affinity, and if not
carefully managed, the savings gained by balancing the system can be
lost in rebuilding caches.
• One option is to only allow migration when imbalance surpasses a
given threshold.
Algorithm Evaluation
• How to select CPU-scheduling algorithm for an OS?
• Determine criteria, then evaluate algorithms
• Deterministic modeling
– Type of analytic evaluation
– Takes a particular predetermined workload and
defines the performance of each algorithm for
that workload
• Consider 5 processes arriving at time 0:
Deterministic Evaluation
 For each algorithm, calculate minimum average
waiting time
 Simple and fast, but requires exact numbers for
input, applies only to those inputs
 FCS is 28ms:

 Non-preemptive SJF is 13ms:

 RR is 23ms:
Queueing Models
• A study of historical performance can often
produce statistical descriptions of certain
important parameters, such as
– the rate at which new processes arrive,
– the ratio of CPU bursts to I/O times,
– the distribution of CPU burst times and I/O burst
times, etc.

• Given those probability distributions and some


mathematical formulas, it is possible to calculate
certain performance characteristics of individual
waiting queues.
Little’s Formula
• n = average queue length
• W = average waiting time in queue
• λ = average arrival rate into queue
• Little’s law – in steady state, processes leaving
queue must equal processes arriving, thus:
n=λxW
– Valid for any scheduling algorithm and arrival
distribution
• For example, if on average 7 processes arrive
per second, and normally 14 processes in
queue, then average wait time per process = 2
seconds
Simulations
• Simulations are more accurate
– Programmed model of computer system
– Gather statistics indicating algorithm performance
– Data to drive simulation gathered via
• Random number generator according to probabilities
• Distributions defined mathematically or empirically
• Trace tapes record sequences of real events in real
systems
Evaluation of CPU Schedulers by Simulation
Implementation
 Even simulations have limited accuracy
 Just implement new scheduler and test in real systems
 High cost, high risk
 Environments vary
 Most flexible schedulers can be modified per-site or per-system
 Or APIs to modify priorities

You might also like