Operating Systems - CPU Scheduling (Night Class)
Compiled
By Aurelie A. Peralta
CPU-I/O Burst Cycle
The success
of CPU scheduling depends on the following observed property of processes: Process
execution consists of a cycle of CPU execution and I/O wait. Processes alternate
between these two states. Process execution begins with a CPU burst. That is followed
by an I/O burst, then another CPU burst, then another I/O burst, and so on. Eventually,
the last CPU burst will end with a system request to terminate execution, rather
than with another I/O burst. The durations of these CPU bursts have been extensively
measured. Although they vary greatly by process and by computer, they tend to have
a similar frequency curve.
CPU Scheduler
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.
Preemtive Scheduling
CPU scheduling
decisions may take place under the following four circumstances:
1. When
a process switches from the running state to the waiting state.
2. When
a process switches from the running state to the ready state.
3. When
a process switches from the waiting state to the ready state.
4. When
process terminates.
In circumstances
1 and 4, there is no choice in terms of scheduling. A new process must be selected
for execution. There is a choice, however, in circumstances 2 and 3. When scheduling
takes place only under circumstances 1 and 4, we say the scheduling scheme is nonpreemptive;
otherwise, the scheduling scheme is preemptive.
Dispatcher
The dispatcher
is the module that gives control of the CPU to the process selected by the short-term
scheduler. This function involves:
1. Switching
context
2. Switching
to user mode
3. Jumping
to the proper location in the user program to restart that program.
The dispatcher
should be as fast as possible, given that it is invoked during every process switch.
The time it takes for the dispatcher to stop one process and start another running
is known as the dispatch latency.
Scheduling Criteria
1. CPU
Utilization - keep the CPU as busy as possible
2. Throughput
- number of processes completed per time unit
3. Turnaround
time - interval between time of submission to time of completion.
4. Waiting
time - sum of the periods spent waiting in the ready queue.
5. Response
time - time of submission of the request until the first response is produced.
CPU scheduling
is the task of selecting a waiting process from the ready queue and allocating the
CPU to it. The CPU is allocated to the selected process by the dispatcher.
First-come,
First-served (FCFS) scheduling is the simplest scheduling algorithm, but it can
cause short processes to wait for very long processes. Shortest-job-first (SJF)
scheduling is provably optimal, providing the shortest average waiting time. Implementing
SJF scheduling is difficult because predicting the length of the next CPU burst
is difficult. The SJF algorithm is a special case of the general priority-scheduling
algorithm, which simply allocates the CPU to the highest-priority process. Both
priority and SJF scheduling may suffer from starvation. Aging is a technique to
prevent starvation.
Round-robin
(RR) scheduling is more appropriate for a time-shared (interactive) system. RR scheduling
allocates the CPU to the first process in the ready queue for q time units, where
q is the time quantum. After q time units, if the process has not relinquished the
CPU, it is preempted and the process is put at the tail of the ready queue. The
major problem is the selection of the time quantum. If the quantum is too large,
RR scheduling degenerates to FCFS scheduling; if the quantum is too small, scheduling
overhead in the form of context-switch time becomes excessive.
The FCFS
algorithm is nonpreemptive, the RR algorithm is preemptive. The SJF and priority
algorithms may be
either preemptive or nonpreemptive.
Multilevel
queue algorithms allow different algorithms to be used for various classes of processes.
The most common is a foreground interactive queue which uses RR scheduling, and
a background batch queue, which uses FCFS scheduling. Multilevel feedback queues
allow processes to move from one queue to another.
Because
such a wide variety of scheduling algorithms are available, we need methods to select
among them. Analytic methods use mathematical analysis to determine the performance
of an algorithm. Simulation methods determine performance by imitating the scheduling
algorithm on a “representative” sample of processes, and computing the resulting
performance.
Operating
Systems supporting threads at the kernel level must schedule threads - not processes
- for execution. This is the case with Solaris 2 and Windows 2000 where both systems
schedule threads using preemptive priority based on scheduling algorithm including support for real-time threads. The Linux process scheduler also uses a priority-based
algorithm with real-time supports as well. The scheduling algorithms for these three
operating systems typically favor interactive over batch and CPU-bound processes.
Reference: Operating System Concepts by Silberschatz, Galvin, and Gagne, 2003