05 - CPU Scheduling
05 - CPU Scheduling
05 - CPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Multiple-Processor Scheduling
Real-Time Scheduling
Algorithm Evaluation
Basic Concepts
2
CPU scheduling is the basis of multi-programmed operating systems. By switching the CPU
among processes, the operating system can make the computer more productive.
Maximum CPU utilization obtained with multiprogramming
The objective of multiprogramming is to have some process running at all times, in
order to maximize CPU utilization. In a uniprocessor system, only one process may
run at a time; any other processes must wait until the CPU is free and can be
rescheduled.
CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O
wait.
The idea of multiprogramming is relatively simple. A process is executed until it
must wait, typically for the completion of some I/O request. In a simple computer
system, the CPU would then sit idle; all this waiting time is wasted. With
multiprogramming, we try to use this time productively. Several processes are kept
in memory at one time. When one process has to wait, the operating system takes
the CPU away from that process and gives the CPU to another process. This
pattern continues.
Scheduling is a fundamental operating-system function. Almost all computer resources are
scheduled before use. The CPU is, of course, one of the primary computer resources. Thus,
its scheduling is central to operating-system design.
1
6/3/2012
2
6/3/2012
CPU Scheduler
5
Whenever the CPU becomes idle, the operating system must select one of
the processes in the ready queue to be executed. The selection process is
carried out by the short-term scheduler (or CPU scheduler). The scheduler
selects from among the processes in memory that are ready to execute,
and allocates the CPU to one of them.
The ready queue is not necessarily a first-in, first-out (FIFO) queue. As we
shall see when we consider the various scheduling algorithms, a ready
queue may be implemented as a FIFO queue, a priority queue, a tree, or
simply an unordered linked list. Conceptually, however, all the processes in
the ready queue are lined up waiting for a chance to run on the CPU. The
records in the queues are generally process control blocks (PCBs) of the
processes.
CPU Scheduler
6
3
6/3/2012
Dispatcher
7
Scheduling Criteria
8
4
6/3/2012
Scheduling Criteria
9
We want to keep the CPU as busy as possible. CPU utilization may range
from 0 to 100 percent. In a real system, it should range from 40 percent (for
a lightly loaded system) to 90 percent (for a heavily used system).
Throughput – # of processes that complete their execution per time unit.
If the CPU is busy executing processes, then work is being done. One
measure of work is the number of processes completed per time unit, called
throughput. For long processes, this rate may be 1 process per hour; for short
transactions, throughput might be 10 processes per second.
Turnaround time – amount of time to execute a particular process
From the point of view of a particular process, the important criterion is how
long it takes to execute that process. The interval from the time of submission
of a process to the time of completion is the turnaround time. Turnaround
time is the sum of the periods spent waiting to get into memory, waiting in
the ready queue, executing on the CPU, and doing I/O.
Scheduling Criteria
10
Waiting time – amount of time a process has been waiting in the ready
queue
The CPU-scheduling algorithm does not affect the amount of time during
which a process executes or does I/O; it affects only the amount of time that
a process spends waiting in the ready queue. Waiting time is the sum of the
periods spent 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)
In an interactive system, turnaround time may not be the best criterion.
Often, a process can produce some output fairly early, and can continue
computing new results while previous results are being output to the user.
Thus, another measure is the time from the submission of a request until the
first response is produced. This measure, called response time, is the amount
of time it takes to start responding, but not the time that it takes to output
that response. The turnaround time is generally limited by the speed of the
output device.
5
6/3/2012
Optimization Criteria
11
0 24 27 30
6
6/3/2012
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
7
6/3/2012
P1 P3 P2 P4
0 3 7 8 12 16
Average waiting time = (0 + 6 + 3 + 7)/4 - 4
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Average waiting time = (9 + 1 + 0 +2)/4 - 3
8
6/3/2012
τ n=1 = α tn + (1 − α )τ n .
α =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.
9
6/3/2012
Priority Scheduling
19
10
6/3/2012
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
22
11
6/3/2012
Multilevel Queue
23
12
6/3/2012
13
6/3/2012
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.
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.
Multiple-Processor Scheduling
28
14
6/3/2012
Real-Time Scheduling
29
15