Smarter Scheduling (Priorities, Preemptive Priority Scheduling, Lottery and Stride Scheduling)
Chapter 5: Process Scheduling. Outline Basic Concepts CPU-I/O Burst Cycle CPU Scheduler ...
-
Upload
kellie-watts -
Category
Documents
-
view
237 -
download
0
Transcript of Chapter 5: Process Scheduling. Outline Basic Concepts CPU-I/O Burst Cycle CPU Scheduler ...
Chapter 5:
Process Scheduling
OutlineBasic Concepts
CPU-I/O Burst Cycle CPU Scheduler Preemptive Scheduling Dispatcher
Scheduling CriteriaScheduling Algorithms
First-Come, First-Serve Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling
Basic ConceptsCPU: One of the primary computer resourceMaximum CPU utilization obtained with
multiprogrammingSeveral processes are kept in memory
CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait.
CPU burst distribution determines how to schedule the processes.
Alternating Sequence of CPU And I/O Bursts
Histogram of CPU-burst Times
Normally a process starts with frequent short CPU bursts and then it stabilizes
and shifts to long but infrequent CPU bursts
I/O bound processes usually have many small CPU bursts
CPU bound programs usually have few but long CPU burstsCPU-bound program
CPU SchedulerSelects from among the processes in memory that
are ready to execute, and allocates the CPU to one of them.Is it the same as Short-term scheduler? … Yes
CPU scheduling decisions may take place when a process:1.Switches from running to waiting state.
2.Switches from running to ready state.
3.Switches from waiting to ready.
4.Terminates.(Own will)
Scheduling Scheme given at 1 and 4 is nonpreemptive (Cooperative).
All other scheduling is preemptive.
Preemptive vs Non PreemptiveScheduling
Non Preemptive scheduling If a process is being executed then it keeps the CPU
until one or four conditions of previous slide occur.MS Windows 3.x which was sort of multitasking system
used this methodNot at all efficient.
PreemptiveA process in running state can be brought back to ready
state without any of the conditions of previous slide being met.
Can you guess how it is done?
Problems with preemptive
Although better than non-preemptive still suffers from synchronization problems
E.g.Process P and Q share some data (can be
memory location or a file)P does some calculations changes some of the
data but before it finishes its time expiresQ reads the data but is the data valid?
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:switching contextswitching to user mode jumping to the proper location in the user program to
restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running.Should be minimum.
Scheduling CriteriaFollowing criteria is kept under consideration while designing a scheduler
CPU utilization – Keep the CPU as busy as possibleThroughput – # of processes that complete their execution
per unit time (Given Time). Turnaround time – Amount of time to execute a particular
process (Sum of ready, waiting, running, loading times,I/O)Waiting time – Amount of time a process has been waiting
in the ready queueResponse time – Amount of time it takes from when a
request was submitted until the first response is produced, not output (for interactive environment) Time it takes to start responding Not the time it takes to output the response
Optimization Criteria
So the best CPU Scheduling algorithm is which …
Maximizes CPU utilizationMaximizes throughputMinimizes turnaround time Minimizes waiting time Minimizes response time
OutlineBasic Concepts
CPU-I/O Burst Cycle CPU Scheduler Preemptive Scheduling Dispatcher
Scheduling CriteriaScheduling Algorithms
First-Come, First-Serve Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling
First-Come, First-Served (FCFS) Scheduling
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:
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
FCFS Scheduling (Cont.)Suppose that the processes arrive in the order
P2 , P3 , P1 .
The Gantt chart for the schedule is:
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3Much better than previous case.CPU bound processes will get and hold the CPUConvoy effect short process behind long process (Bad
CPU utilization)
P1P3P2
63 300
Shortest-Job-First (SJR) SchedulingAssociate with each process the length of its next CPU
burst. Use these lengths to schedule the process with the shortest time.
Two schemes: nonpreemptive – once CPU given to the process it cannot be preempted
until it completes its CPU burst. preemptive – if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF).
SJF is optimal – gives minimum average waiting time for a given set of processes.
Shortest-next-cpu-burst algorithm
Process Burst Time
P1 6
P2 8
P3 7
P4 3
SJF
Average waiting time = (3+16+9+0)/4=7 FCFS=10.25
Example of Non-Preemptive SJF
P4P1 P3
93 240
P2
8
16
Shortest-Job-First (SJR) SchedulingGood for long-term Scheduler in batch systems
User specifies process time during job submissionLower value may mean faster responseToo low a value will cause a time-limit-exceeded error
Difficult to implement at the level of short-term CPU scheduler
Determining Length of Next CPU BurstCan only predict/ estimate the length of next CPU burst.Can be done by using the length of previous CPU
bursts, using exponential averaging.
Examples of Exponential Averaging =0
n+1 = n
Recent history does not count.
=1 n+1 = tn
Only the actual last CPU burst counts.
Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor.
n+1 =αtn +(1- α) n
tn: length of the nth CPU burst
n+1 : Our predicted Value
α: weight of recent & past history
0<= α<=1
Prediction of the Length of the Next CPU Burst
Initial guess for T(1) = 10 ms Actual 6
Determination of 2 Cycle based on actual (6) and last guess (10)
T(2) = 0.5 x 6 + 0.5 x 10 = 8 ms
Determination of 3 cycle based on actual(4) and last guess (8)
T(3) = 0.5 x 4 + 0.5 x 8 = 6 ms
… Now you can do it
For example: Suppose a process p is given a default expected burst length of 5 time units. When it is run, the actually burst lengths are 10,10,10,1,1,1 (although this information is not known in advance to any algorithm). The prediction of burst times for this process works as follows.
Let e(1) = 5, as a default value.
When process p runs, its first burst actually runs 10 time units, so, a(1) = 10.
e(2) =0.5 * e(1) * 0.5 + a(1) = 0.5 * 5 + 0.5 * 10 = 7.5 This is the prediction for the second cpu burst
The actual second cpu burst is 10. So the prediction for the third cpu burst is:
e(3) = 0.5 * e(2) * 0.5 + a(2) = 0.5 * 7.5 + 0.5 * 10 = 8.75 e(4) = 0.5 * e(3) * 0.5 + a(3) = 0.5 * 8.75 + 0.5 * 10 = 9.38,
So, we predict that the next burst will be close to 10 (9.38) because the recent bursts have been of length 10. At this point, it happens that the process starts having shorter bursts, with a(4) = 1, so the algorithm gradually adjusts its estimated cpu burst (prediction)
e(5) = 0.5 * e(4) * 0.5 + a(4) = 0.5 * 9.38 + 0.5 * 1 = 5.19 e(6) = 0.5 * e(5) * 0.5 + a(5) = 0.5 * 5.19 + 0.5 * 1 = 3.10 e(7) = 0.5 * e(6) * 0.5 + a(6) = 0.5 * 3.10 + 0.5 * 1 = 2.05
Example of Preemptive SJF(Shortest Remaining Time first)
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5SJF (preemptive)
Average waiting time = 6.5Non-Preemptive Average waiting time = 6.5
P1 P2
51 170
P1
10
P4P3
26
OutlineBasic Concepts
CPU-I/O Burst Cycle CPU Scheduler Preemptive Scheduling Dispatcher
Scheduling CriteriaScheduling Algorithms
First-Come, First-Serve Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling
Priority SchedulingA priority number (integer) is associated with each processThe CPU is allocated to the process with the highest
priority (smallest integer highest priority). Preemptive Non-preemptive
SJF is a priority scheduling where priority is the predicted next CPU burst time.
Problem Starvation – low priority processes may never execute.
Solution Aging – as time progresses increase the priority of the process.
Round Robin (RR)Each process gets a small unit of CPU time (time
quantum), 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.
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 = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart is:
Typically, higher average turnaround than SJF, but better response.
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
Time Quantum and Context Switch Time
Remember: Context switch takes time
If context switching time is approx 10 percent of the time quantum, then about 10 percent of the CPU time will be spent in context switching
Most modern systems have time quanta ranging from 10 to 100 milliseconds
Context switch typically takes less than 10 microseconds
Turnaround Time Varies With The Time Quantum
Average Turnaround time doesn’t necessarily improve as the time-quantum size increasesAverage Turnaround time doesn’t necessarily improve as the time-quantum size increases))
80% of CPU bursts should be shorter than the time quantum
OutlineBasic Concepts
CPU-I/O Burst Cycle CPU Scheduler Preemptive Scheduling Dispatcher
Scheduling CriteriaScheduling Algorithms
First-Come, First-Serve Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling
Multilevel QueueReady queue is partitioned into separate queues:
foreground (interactive)background (batch)
Each queue has its own scheduling algorithm, foreground – RRbackground – FCFS
Scheduling must be done between the queues.Fixed priority 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; i.e., 80% to foreground in RR20% to background in FCFS
Multilevel Queue Scheduling
Multilevel Feedback Queue
A process can move between the various queues; aging can be implemented this way.
Multilevel-feedback-queue scheduler defined by the following parameters:number of queuesscheduling algorithms for each queuemethod used to determine when to upgrade a processmethod used to determine when to demote a processmethod used to determine which queue a process will
enter when that process needs service
Example of Multilevel Feedback QueueConsider a case if we have three queues:
Q0 – time quantum 8 milliseconds
Q1 – time quantum 16 milliseconds
Q2 – FCFS
SchedulingA new job enters queue Q0 which is served FCFS. When it
gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to tail of queue Q1.
At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to tail of queue Q2.
Multilevel Feedback Queues
Outline Basic Concepts
CPU-I/O Burst Cycle CPU Scheduler Preemptive Scheduling Dispatcher
Scheduling Criteria Scheduling Algorithms
First-Come, First-Serve Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling
Multi-Processor Scheduling
Multiple-Processor SchedulingCPU scheduling more complex when multiple
CPUs are available.Consider Homogeneous processors within a
multiprocessor system.Load sharing Asymmetric multiprocessing – only one
processor accesses the system data structures, alleviating the need for data sharing.
SMP: Each processor is self schedulingMay have separate ready queues
Multiple-Processor Scheduling –Process Affinity
Cache memory?The Data most recently accessed populates it
Process migration to another process Invalidation & Repopulation
Some OS attempt to keep a process running on the same processor –Affinity
Soft Affinity: Attempt to keep a process running on the same processor but not guaranteeing
Hard Affinity: Process cannot migrate to another processor
Multiple-Processor Scheduling –Load Balancing
Utilize the full benefits of multi-processorsLoad balancing: Distribute the workload evenly across all
processors.Necessary where each processors has its own private
queue of eligible processesNot necessary where processors have common run
queueModern OS --private queue or common?Push Migration: move processes from overloaded to idle
processorsPull Migration: Idle processor pulls a waiting process
from a busy processorAny link with Process Affinity?
Multiple-Processor Scheduling Symmetric Multithreading
Multiple logical rather than physical processors to run several threads concurrently (HPT)
Create logical processors on the same physical processor—a view of many Processors to OSEach has its own machine-state registersOwn interrupt handling
Logical CPU
Logical CPU
Physical CPU
Logical CPU
Logical CPU
Physical CPU
System Bus
Multiple-Processor Scheduling Symmetric Multithreading
SMT: a feature provided by hardwareOs needn’t be designed differentlyPerformance gains –If OS is aware.
But why?**Scheduler**
Outline Basic Concepts
CPU-I/O Burst Cycle CPU Scheduler Preemptive Scheduling Dispatcher
Scheduling Criteria Scheduling Algorithms
First-Come, First-Serve Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling
Multi-Processor Scheduling OS Examples –Self Study Algorithm Evaluation
Algorithm Evaluation How do we select the best algorithm?
Defining a criteria CPU Utilization, response time, throughput Criteria may include several measures
e.g. Maximize CPU utilization |and| Response time is 1 e.g. Maximize Throughput |and| turnaround time
Analytic Evaluation Method The algorithm and some system workload are used to
produce a formula or number which gives the performance of the algorithm for that workload.
Deterministic modeling Queuing models Simulations Implementation
Deterministic Modeling Predetermined workload –defines performance of
each algorithm for that workload. Use of Gantt charts.
Simple and fast Exact numbers for comparison Answers apply only for the cases considered. Performance figures may not be true in general Suitable: If we are running the same program over
and over again. We can easily select an algorithm. Over a set of examples certain trends can be
analyzed, e.g. If all the processes arrive at time 0, the SJF policy will always result in min waiting time If the Arriving time is different the SJF may not always
result in min average waiting time.
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
Gantt chart
Average waiting time = (9 + 1 + 0 +2)/4 = 3
P3P2
42 110
P4
5 7
P2 P1
16
P1
Deterministic Modeling
Queuing Modeling What if the processes that are run, vary from day to day? --
Is deterministic method useful? The distributions of CPU and I/O bursts can be determined/
approximated/ estimated. Mathematical formula: probability of a particular CPU burst Arrival times distribution can also be estimated.
Computer system viewed as a network of queues and servers: ready queue, I/O queue, event queues, CPUs, I/O device controllers, etc. e.g. CPU: ready queue, I/O system :device queue
Input: Arrival and service rates Output: CPU utilization, average queue length, average
waiting time, …
Queuing Modeling
Little’s Formula:n = λ* W
wheren = average queue lengthλ = average arrival rateW = average waiting time in a
queue
Queuing ModelingLet the average job arrival rate be 0.5
Algorithm Average Wait Time
W=tw
Average Queue Length(n)
FCFS 4.6 2.3
SJF 3.6 1.8
SRTF 3.2 1.6
RR (q=1) 7.0 3.5
RR (q=4) 6.0 3.0
Queuing Modeling
Complicated mathematics Distributions (uniform, exponential,
etc) for the arrival and departure rates can be difficult to work with
Assumptions may not be accurate Approximation of the real system
Simulation To get more accurate evaluation Workload generated by assuming some
distribution and a random number generator, or by collecting data from the actual system.
The distributions can be defined mathematically or empirically.
Simulation
Simulation
CharacteristicsExpensive: hours of programming and execution time
May be erroneous because of the assumptions about distributions
Implementation Even simulation is of limited accuracy Best way: Code and implement the scheduling
algorithm and see High cost –coding the algorithm Modify the OS to support it Reaction of the users to changing OS
Environment changes: if short processes are given high priority then user may break their larger processes into many short ones
For example: Design of an automatic Interactive/ non-interactive process classifier Algorithm