CPU Scheduling

advertisement
CPU Scheduling
OS switches the CPU among processes
1. Basic Concepts
1.1 Motivations
•
•
•
# of CPU Bursts
•
Process execution consists of CPU execution (CPU Burst) and I/O wait (I/O
Burst)1
Process execution is an alternating sequence of CPU Burst and I/O Burst.
(Begin) CPU Burst I/O Burst
CPU Burst I/O Burst …
CPU Burst (Terminated)
Generally, a process has a large number of short CPU bursts and a small # of long
CPU burst (see Figure 5.1)
Duration of CPU Burst
Figure 5.1 Histogram of CPU Burst times
•
•
CPU Scheduler select a process from the ready queue when the current process
releases the CPU
Since the scheduler executes very frequently, it must be
o Very fast to avoid wasting CPU time
o Very efficient (i.e., the problem of selecting a CPU algorithm for a
particular system)
1
While the CPU bound process has a larger number of longer CPU bursts, the I/O bound process has a
larger number of I/O bursts. Because there is a CPU burst between two I/O burst, the I/O bound process has
a larger number of short CPU bursts.
1
Note that all the PCBs (Process Control Blocks) in the ready queue represent the
processes in CPU burst state2.
1.2 Some Measures Comparing Various CPU Scheduling Algorithms
1)
2)
3)
4)
CPU utilization: 0-100%. In real systems, it should range 40-90%.
Throughput: # of processes that are completed per time unit.
Turnaround time: the time of completion - the time of submission
Waiting time: the amount of time that a process spends waiting in the ready
queue during turnaround time
5) Response time: the time from the submission of a request until the first response
is produced.
•
•
•
Generally, we try to maximize the averages of 1) and 2) and minimize the
averages of other ones.
To guarantee that all users get good service, we try to minimize the maximum
response time.
In time-sharing (multi-tasking) systems, we try to minimize the variance in the
response time.
2. Basic Scheduling Algorithms
Assumptions: One CPU burst per process; the measure of comparison is the average
waiting time.
2.1 Type of Scheduling Algorithms
•
Non-preemptive Scheduling: The current process releases the CPU either by
terminating or by switching to the waiting state. (Used in MS Windows family)
o Pros:
Decreases turnaround time
Does not require special HW (e.g., timer)
o Cons
Limited choice of scheduling algorithm
•
Preemptive Scheduling: The current process needs to involuntarily release the
CPU when a more important process is inserted into the ready queue or once an
allocated CPU time has elapsed. (Used in Unix and Unix-like systems)
o Pros:
No limitation on the choice of scheduling algorithm
2
Dispatcher switches the context so as to start the execution of the next process, which is selected by the
CPU scheduler.
2
o Cons:
Additional overheads (e.g., more frequent context switching, HW
timer, coordinated access to data, etc.)
2.2 FCFS Scheduling (First-Come First-Served): Non-Preemptive
•
CPU is allocated to the process that requested it first.
Assumptions: The durations of the CPU bursts of P1, P2, and P3 are 19, 10, and 7
respectively. P1, P2, and P3 have only one CPU burst.
Processes’ arrival time: P1 (0), P2 (2), P3 (4):
P1
P2
P3
0
19
29
36
Waiting times: P1=0; P2=19-2=17; P3=29-4=25
Avg. Waiting time = (0+17+25)/3=14
Processes’ arrival time: P3 (0), P2 (2), P1 (4):
P3
P2
P1
0
7
17
36
Waiting times: P1=17-4=13; P2=7-2=5; P3=0
Avg. Waiting time = (13+5+0)/3=6
Figure 5.2 Examples of FCFS algorithm
•
•
•
Convey effect: several processes that have very short CPU bursts follow one big
CPU bound process. Let’s assume that all processes have similar
I/O bursts.
Pros:
o Simple to understand and implement
o Very fast selection of the next process
Cons:
o Avg. Waiting time is long and may vary substantially, depending on the
process input order
o Not suitable for time-sharing OS
3
2.3 SJF Scheduling (Shortest Job First): Preemptive or Non-Preemptive
•
CPU is allocated to the process that has the smallest next CPU burst.
o Non-preemptive: once the CPU has been given to a process, it cannot be
preempted until the process completes its CPU burst.
o Preemptive: when a new process Pi whose next CPU burst is less than the
remaining CPU burst of the current process is inserted into
the ready queue, the current process relinquishes the CPU
involuntarily and the CPU is allocated to Pi. (Also known as
Shortest-Remaining-Time First (SRTF) algorithm)
Assumptions: The durations of the CPU bursts of P1, P2, and P3 are 19, 10, and 7
respectively. P1, P2, and P3 have only one CPU burst. Processes’ arrival time: P1 (0), P2
(2), P3 (4).
Non-Preemptive:
P1
P3
0
P2
19
P2
26
36
Waiting times: P1=0; P2=26-2=24; P3=19-4=15
Avg. Waiting time = (0+24+15)/3=13
P1
Preemptive:
P3
024
P2
11
P1
19
36
Waiting times: P1=0+(19-2)=17; P2=0+(11-4)=7; P3=0
Avg. Waiting time = (17+7+0)/3=8
Figure 5.3 Examples of SJF & SRTF
•
Pros:
o Yields minimum avg. waiting time for a given set of processes.
•
Cons:
o Difficult to estimate CPU burst time.
o Starvation (indefinite blocking)
•
Predicting the next burst time based on the previous bursts (exponential avg. of
the lengths of previous CPU bursts).
4
o
o
o
o
tn: length of the nth CPU burst
τn+1: predicted length of the next CPU burst
0≤α≤1
τn+1 = α tn + (1-α
α)ττn (Note, α + (1-α) = 1)
α is the weight of the most recent CPU burst length tn
(1-α) is the weight of the past history of CPU burst lengths τn
The weight of tn-x is α(1- α)x. Since (1-α) < 1, as x increases, the weight of
tn-x is decreased.
2.4 Priority Scheduling: Preemptive or Non-Preemptive
Assumptions: The durations of the CPU bursts of P1, P2, and P3 are 19, 10, and 7
respectively. P1, P2, and P3 have only one CPU burst. Processes’ arrival time: P1 (0), P2
(2), P3 (4), and their priorities are 3 (lowest), 2, and 1 (highest), respectively.
Non-Preemptive:
P1
P3
0
P2
19
P2
26
36
Waiting times: P1=0; P2=26-2=24; P3=19-4=15
Avg. Waiting time = (0+24+15)/3=13
P1
Preemptive:
P3
024
P2
11
P1
19
36
Waiting times: P1=0+(19-2)=17; P2=0+(11-4)=7; P3=0
Avg. Waiting time = (17+7+0)/3=8
Figure 5.4 Examples of NP & P Priority Scheduling Algorithms
•
•
A priority is associated with each process, and the CPU is allocated to the process
with the highest priority. Equal-priority processes are scheduled in FCFS.
o Non-preemptive: once the CPU has been given to a process, it cannot be
preempted until the process completes the CPU burst.
o Preemptive: when a process with the priority that is higher than the
priority of the current process is inserted into the ready
queue, the current process relinquishes the CPU involuntarily
and the CPU is allocated to Pi.
Priority values are defined either internally or externally (relative to the system)
5
•
SJF and SRTF are the variants of Priority Scheduling (e.g., p=τ)
•
Pros:
o More reactive
o Can be used for soft real-time systems
•
Cons:
o Can cause indefinite blocking or starvation
Solution: gradually increase priority of Pi as time progresses (also
known as aging).
2.5 RR Scheduling (Round Robin): Preemptive
•
Preemptive version of FCFS: Basically FCFS except that each process gets a
small unit of CPU time called time quantum (also known as time slice). After
time quantum has elapsed, the process is preempted and added to the end of the
ready queue. While switching the context, the scheduler (or dispatcher) sets the
HW timer to interrupt after 1 time quantum (usually 10 – 100 milliseconds).
•
If the current CPU burst is smaller than one time quantum, the current process
releases the CPU voluntarily.
•
Designed for time-sharing systems
•
The performance depends heavily on the time quantum value
o Infinite time quantum RR is the same as FCFS
o Too small time quantum too many context switches
o Too large time quantum inherits the problems of FCFS
o Experimental results recommend that 80% of the CPU bursts should be
shorter than the time quantum.
•
Pros:
o Makes system more responsive
o Guarantees fairness (Bounded waiting time and no starvation)
•
Cons:
o Average waiting time is long, added overhead of additional context
switches when many CPU bursts are larger than the time quantum.
o Requires HW timer
6
Download