Академический Документы
Профессиональный Документы
Культура Документы
: CPU Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Multiple-Processor Scheduling
• Real-Time Scheduling
• Algorithm Evaluation
6.1
Basic Concepts
6.2
Alternating Sequence of CPU And I/O Bursts
6.3
Histogram of CPU-burst Times
• Selects from among the processes in memory that are ready to execute,
and allocates the CPU to one of them.
• CPU scheduling decisions may take place when a process:
1. Runs until it Switches from running to waiting state … stop
executing only when a needed resource or service is currently
unavailable.
2. Switches from running to ready state in the middle of a burst –
can stop execution at any time
3. Switches from waiting to ready. … ?
4. Runs until it Terminates.
• Scheduling under 1 and 4 is nonpreemptive.
• All other scheduling is preemptive.
• Under nonpremptive scheduling, once a CPU is assigned to a
process, the process keeps the CPU until it releases the CPU either
by terminating or switching to wait state - “naturally” stop
execution.
6.5
Dispatcher
6.6
Scheduling Criteria
6.7
Optimization Criteria
6.8
First-Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
6.9
FCFS Scheduling (Cont.)
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
6.10
Algo for FCFS
6.11
• Find waiting time for all other
processes i.e. for process i -> wt[i] =
bt[i-1] + wt[i-1] .
• 5- Find turnaround time =
waiting_time + burst_time for all
processes.
• 6- Find average waiting time =
total_waiting_time /
no_of_processes.
6.12
• 7- Similarly, find average
turnaround time =
total_turn_around_time /
no_of_processes.
6.13
Algo for FCFS with arrival time
6.14
• 3- Now find turn around time =
waiting_time + burst_time for all
processes
• 4- Average waiting time =
total_waiting_time /
no_of_processes
• 5- Average turn around time =
total_turn_around_time /
no_of_processes
6.15
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.
• Two schemes:
– nonpreemptive – once CPU given to the process it cannot
be preempted until 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.
6.16
Example of Non-Preemptive SJF
P1 P3 P2 P4
0 3 7 8 12 16
• FCFS is “tie breaker” if burst times the same.
• Average waiting time = (0 + 6 + 3 + 7)/4 - 4
6.17
Example of Preemptive SJF
(Also called Shortest-Remaining-Time-First (SRTF) )
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
6.18
Determining Length of Next CPU Burst
6.19
Examples of Exponential Averaging
• =0
– n+1 = n
– Recent history does not count.
• =1
– n+1 = tn
– Only the actual last CPU burst counts.
• If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -1 + …
+(1 - )n=1 tn 0
• Since both and (1 - ) are less than or equal to 1, each
successive term has less weight than its predecessor.
6.20
Priority Scheduling
6.21
Round Robin (RR)
6.22
Example: RR with Time Quantum = 20
FCFS is tie breaker Process Burst Time Assume all arrive at 0 time
P1 53 in the order given.
P2 17
P3 68
P4 24
• The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
6.23
How a Smaller Time Quantum Increases Context Switches
6.24
Turnaround Time Varies With The Time Quantum
TAT can be improved if most processes finish each burst in one quanta
EX: if 3 processes each have burst of 10, then
for q = 1, avg_TAT = 29, but for q = burst = 10, avg_TAT = 20.
==> design tip: tune quanta to average burst.
6.25
Multilevel Queue (no Feedback )
6.26
Multilevel Queue Scheduling
6.27
Multilevel Feedback Queue
6.28
Multilevel Feedback Queues
Queue 0 -
High priority
Queue 1
Queue 2
Low priority
• Three queues:
– Q0 – time quantum 8 milliseconds
– Q1 – time quantum 16 milliseconds
– Q2 – FCFS
– Q2 longest jobs, with lowest priority, Q1 shortest jobs
with highest priority.
• Scheduling
– 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(demoted).
– At Q1 job is again served FCFS and receives 16 additional
milliseconds. If it still does not complete, it is preempted
and moved to queue Q2.
– Again, Qn is not served until Qn-1 empty
6.30
Multiple-Processor Scheduling
6.31
Real-Time Scheduling
6.32
Dispatch Latency
6.33
Solaris 2 Thread Scheduling
• 3 scheduling priority classes
– Timesharing/interactive – lowest – for users
Within this class: Multilevel feedback queues longer time
slices in lower priority queues
– System – for kernel processes
– Real time – Highest
• Local Scheduling – How the threads library decides which thread to put
onto an available LWP. Remember threads are time multiplexed on
LWP’s
• Global Scheduling – How the kernel decides which kernel thread to run
next.
– The local schedules for each class are “globalized” from the
scheduler's point of view – all classes included.
– LPW’s scheduled by kernel
6.34
Solaris 2 Scheduling
Only a few
in this class
Real time
User processes
Go here
6.35
Java Thread Scheduling
6.36
Java Thread Scheduling (cont)-omit
* Note – the JVM Does Not Specify Whether Threads are Time-
Sliced or Not.
6.37
Time-Slicing (Java) - omit
while (true) {
// perform CPU-intensive task
...
Thread.yield();
}
6.38
Java Thread Priorities - omit
• Thread Priorities:
Priority Comment
Thread.MIN_PRIORITY Minimum Thread Priority
Thread.MAX_PRIORITY Maximum Thread Priority
Thread.NORM_PRIORITY Default Thread Priority
6.39
Algorithm Evaluation
6.40
Evaluation of CPU Schedulers by Simulation
-need good models
- drive it with field data and/or
statistical data
- could be slow.
6.41