ln-os-ch05 cpu scheduling
TRANSCRIPT
-
8/7/2019 ln-os-ch05 CPU Scheduling
1/18
1
ECE/IUPUI RTOS & APPS 1
CPU Scheduling
Electrical and Computer Engineering
Stephen Kim ([email protected])
ECE/IUPUI RTOS & APPS 2
CPU Schedulingn Basic Concepts
n Scheduling Criteria
n Scheduling Algorithms
n Multiple-Processor Scheduling
n Real-Time Scheduling
n Algorithm Evaluation
-
8/7/2019 ln-os-ch05 CPU Scheduling
2/18
2
ECE/IUPUI RTOS & APPS 3
Basic Concepts
n Maximum CPU utilization obtained withmultiprogramming by making some process running at alltimes.
n CPU Burst vs. I/O Burst Process execution consists of a
cycle of CPU execution and I/O wait.
n CPU burst distribution
ECE/IUPUI RTOS & APPS 4
Alternating Sequence of CPU And
I/O Bursts
CPU Burst IO Burst
IO Request
IO Complete
-
8/7/2019 ln-os-ch05 CPU Scheduling
3/18
3
ECE/IUPUI RTOS & APPS 5
Histogram of CPU-burst Times
n exponential or hyperexponential
n IO bound program many very short CPU bursteg.)
n CPU bound program a few very long CPU burst
ECE/IUPUI RTOS & APPS 6
CPU Scheduler
n Selects from among the processes in memory that areready to execute, and allocates the CPU to one of them.
n 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.
n Scheduling under 1 and 4 is nonpreemptive. A new ready
process must be selected for execution.
n All other scheduling ispreemptive.
-
8/7/2019 ln-os-ch05 CPU Scheduling
4/18
4
ECE/IUPUI RTOS & APPS 7
Nonpreemptive vs. Preemptive
n Nonpreemptive
u A process keeps the CPU until it release the CPU either by
terminating, or by switching to the waiting state.
u eg) MS Windows 3.1, Apple Macintosh
t limited by hardware support.
n Preemptive
u A process can be interrupted any time.
u Possible Inconsistency
t A process can be interrupted while it modifies some data, which other
process tries to modify or read.
t Frequent between a system call and a user process.
t A simple solution is to disallow to interrupt in a kernel mode.
t The solution is not adequate for real-time systems.
ECE/IUPUI RTOS & APPS 8
Dispatcher
n Dispatcher module gives control of the CPU to theprocess selected by the short-term scheduler; thisinvolves:
u switching context
u switching to user mode
u jumping to the proper location in the user program to
restart that program
n Dispatch latency time it takes for the dispatcher
to stop one process and start another running.
-
8/7/2019 ln-os-ch05 CPU Scheduling
5/18
5
ECE/IUPUI RTOS & APPS 9
Scheduling Criteria
n CPU utilization keep the CPU as busy as possible. Areasonable range is from 40% to 90%.
n Throughput # of processes that complete their execution
per time unit.
n Turnaround time amount of time to execute a particular
process from submission to completion.
n Waiting time amount of time a process has been waiting
in the ready queue.
n 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).
ECE/IUPUI RTOS & APPS 10
Optimization Criteria
n What we want to
u Maximize CPU utilization
u Maximize throughput
u Minimize turnaround time
u Minimize waiting time
u Minimize response time
n In most cases
u optimize the average measure
n In the following, we will use the average waiting
time
-
8/7/2019 ln-os-ch05 CPU Scheduling
6/18
6
ECE/IUPUI RTOS & APPS 11
First-come, First-served (FCFS)Scheduling
Process Burst Time
P1 24
P2 3
P3 3
n Suppose that the processes arrive in the order: P1 , P2 , P3The Gantt Chart for the schedule is:
n Waiting time for P1
= 0; P2
= 24; P3
= 27
n Average waiting time: (0 + 24 + 27)/3 = 17
P1
P2
P3
24 27 300
ECE/IUPUI RTOS & APPS 12
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 ,p3 ,p1 .
n The Gantt chart for the schedule is:
n Waiting time for P1= 6; P
2= 0
;P
3= 3
n Average waiting time: (6 + 0 + 3)/3 = 3
n Much better than previous case.
n Convoy effect: short processes behind long process
P1
P3
P2
63 300
-
8/7/2019 ln-os-ch05 CPU Scheduling
7/18
7
ECE/IUPUI RTOS & APPS 13
Shortest-Job-First (SJR)
Schedulingn Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with theshortest time.
n Two schemes:
u nonpreemptive once CPU given to the process, it cannot be
preempted until completes its CPU burst.
u preemptive if a new process arrives with CPU burst length less
than remaining time of current executing process, preempt.
n SJF is optimal gives minimum average waiting time for a
given set of processes.
ECE/IUPUI RTOS & APPS 14
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
n SJF (non-preemptive)
n Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Example of Non-Preemptive SJF
P1
P3
P2
73 160
P4
8 12
-
8/7/2019 ln-os-ch05 CPU Scheduling
8/18
8
ECE/IUPUI RTOS & APPS 15
Example of Preemptive SJFProcess Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
n SJF (preemptive)
n Average waiting time = (9 + 1 + 0 +2)/4 = 3n Even the SJF is optimal, it cannot be implemented because
we cannot see future.
P1P2P3P4
t
ECE/IUPUI RTOS & APPS 16
Predicting Length of Next CPU
Burstn Can only estimate the length.
n Can be done by using the length of previous CPU
bursts, using exponential averaging.
1. tn = actual length of nth CPU burst
2. n+1 = predicted value for the next CPU burst
3. 014. Define:
n+1 = tn+ (1-) n
-
8/7/2019 ln-os-ch05 CPU Scheduling
9/18
9
ECE/IUPUI RTOS & APPS 17
Predicting Length of Next CPU Burst,cont
ECE/IUPUI RTOS & APPS 18
Examples of Exponential
Averagingn =0
u n+1 = n
u Recent history does not count.
n =1
u n+1 = tn
u Only the actual last CPU burst counts.
n If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 +
+(1 - )j tn -1 +
+(1 - )n=1 tn 0
n Since both and (1 - ) are less than or equal to 1, eachsuccessive term has less weight than its predecessor.
-
8/7/2019 ln-os-ch05 CPU Scheduling
10/18
10
ECE/IUPUI RTOS & APPS 19
Implementation Consideration
n The coefficient of exponential average is a floating number
n The computation of floating number is burden in designing
a kernel.
n Even worse, many microprocessor used for real-time
operating systems do not have floating point unit (FPU).
n What we can do for that type of processors?
ECE/IUPUI RTOS & APPS 20
Priority Scheduling
n A priority number (integer) is associated with each process
u UNIX: -20 (highest) to 19 (lowest), default is 10
u microC/OS-II: 0 (highest) to 63 (lowest)
n The CPU is allocated to the process with the highest
priority
u Preemptive
u nonpreemptive
n SJF is a priority scheduling where priority is the predicted
next CPU burst time.
n Starvation low priority processes may never execute.u Solution: Aging as time progresses increase the priority of the
process.
-
8/7/2019 ln-os-ch05 CPU Scheduling
11/18
11
ECE/IUPUI RTOS & APPS 21
Round Robin (RR)
n Each process gets a small unit of CPU time (time quantumor time slice), usually 10-100 milliseconds. After this timehas elapsed, the process is preempted and added to the endof the ready queue.
n 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.
n Performance
u q large FIFO
u q small q must be large with respect to context switch,otherwise overhead is too high.
ECE/IUPUI RTOS & APPS 22
Example of RR with Time Quantum =4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
n Average waiting time of RR = (6+4+7)/3=5.66
n Average waiting time of SJF = (6+0+3)/3=3n The average waiting time of RR is often quite long.
P1 P2 P3 P1 P1 P1 P1 P1
P1P2 P3 P1 P1 P1 P1 P1
RR
SJF
-
8/7/2019 ln-os-ch05 CPU Scheduling
12/18
12
ECE/IUPUI RTOS & APPS 23
Time Quantum and Context SwitchTime
n The average turnaround of RR is longer than SJFbecause CPU bursts of all processes interleave.
n But, better response.
ECE/IUPUI RTOS & APPS 24
Turnaround Time Varies With The Time
Quantum
-
8/7/2019 ln-os-ch05 CPU Scheduling
13/18
13
ECE/IUPUI RTOS & APPS 25
Multilevel Queue
n Ready queue is partitioned into separate queues:u foreground (interactive)
u background (batch)
n Each queue has its own scheduling algorithm,
u foreground RR
u background FCFS
n Scheduling must be done between the queues.
u Fixed priority scheduling; (i.e., serve all from foreground then
from background). Possibility of starvation.
u Time slice each queue gets a certain amount of CPU time which
it can schedule amongst its processes; i.e.,t 80% to foreground in RR
t 20% to background in FCFS
Is this true?
ECE/IUPUI RTOS & APPS 26
Multilevel Queue Scheduling
-
8/7/2019 ln-os-ch05 CPU Scheduling
14/18
14
ECE/IUPUI RTOS & APPS 27
Multilevel Feedback Queue
Schedulingn A process can move between the various queues
u If a process uses to much CPU time, i t will be moved to a lower-
priority queue, but leaves IO-bound and interactive processes in
the higher priority queue.
u Aging can be implemented with the same way.
n Multilevel-feedback-queue scheduler defined by the
following parameters:
u number of queues
u scheduling algorithms for each queue
u method used to determine when to upgrade a process
u method used to determine when to demote a processu method used to determine which queue a process will enter when
that process needs service
ECE/IUPUI RTOS & APPS 28
Example of Multilevel Feedback
Queuen Three queues:u Q0 time quantum 8 milliseconds
u Q1 time quantum 16 milliseconds
u Q2 FCFS
n Scheduling
u A 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 queue Q1.
u At Q1job is again served FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted and
moved to queue Q2.
-
8/7/2019 ln-os-ch05 CPU Scheduling
15/18
15
ECE/IUPUI RTOS & APPS 29
Multilevel Feedback Queues
ECE/IUPUI RTOS & APPS 30
Example, Scheduling
n For the given processes, answer the following question forFCFS, SJF, RR(q=1), RR(q=4), 3Level Feedback(q=1),and 3Level Feedback(q=1&2).
n What is the average waiting times for each scheduling?
n What is the turnaround time for each scheduling?
512E
59D
23C
51B
30A
Processing TimeArrival TimePrcess Name
-
8/7/2019 ln-os-ch05 CPU Scheduling
16/18
16
ECE/IUPUI RTOS & APPS 31
Real-time Scheduling
n Hard real-time systems required to complete a criticaltask within a guaranteed amount of time
n Soft real-time computing requires that critical processes
receive priority over less fortunate ones
n Priority inversion
u When a higher-priority process needs to access kernel data
currently being accessed by another lower-priority process. The
higher-priority process must wait for the lower-priority process to
finish
u Solution: priority inheritance scheme
all processes accessing resources that a higher-priority processneeds, inherit the high priority until they are done with the
resources. When they are completed the resource access, their
priority reverts to its original value
ECE/IUPUI RTOS & APPS 32
Dispatch Latency
n The conflict phase consists of
Preemption of any process running in the kernelRelease by low-priority processes resources needed by the high-priority
process.
n If the preemption is not allowed, the dispatch latency is quite long, somost of real-time OSs support preemption.
-
8/7/2019 ln-os-ch05 CPU Scheduling
17/18
17
ECE/IUPUI RTOS & APPS 33
Rate Monotonic Scheduling
(RMS)n In an RT system, how can we assign priorities to
critical tasks?
n Rate monotonic scheduling
u Task priorities are assigned based on how often tasksexecutes
u Tasks with the highest rate of execution are given thehighest priority
u Assumption
t All tasks are periodic
t Tasks do not synchronize with one another, share resouces, orexchange data
t OS supports preemption
ECE/IUPUI RTOS & APPS 34
RMS Theorem
n Given n tasks, all task hard real-time deadlines arealways met if the inequality hold.
)12(/1
n
i
i nT
E
whereEi is the maximum execution time of task i, and Ti isthe execution period of taski. Note thatEi is always smallerthan Ti andEi/Ti corresponds to the fraction of CPU timerequired to execute task i.
eg) 2(21/2-1)=0.828, 100(21/100-1)=0.695If the system has 100 tasks, the total CPU utilization must beless than 69.5% to meet the real-time deadline.
-
8/7/2019 ln-os-ch05 CPU Scheduling
18/18