The durations of
CPU
bursts have been measured extensively. Although
they vary greatly from process to process and from computer to computer,
they tend to have a frequency curve similar to that shown in Figure 5.2. The
curve is generally characterized as exponential or hyperexponential, with a
large number of short
CPU
bursts and a small number of long
CPU
bursts.
An
I/O
-bound program typically has many short
CPU
bursts. A
CPU
-bound
program might have a few long
CPU
bursts. This distribution can be important
when implementing a
CPU
-scheduling algorithm.
5.1.2
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
CPU
scheduler
, which selects a process from the processes in memory
that are ready to execute and allocates the
CPU
to that process.
Note that 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 can 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 (
PCB
s) of the processes.
burst duration
frequency
Figure 5.2
Histogram of CPU-burst durations.

202
Chapter 5
CPU Scheduling
5.1.3
Preemptive and Nonpreemptive Scheduling
CPU
-scheduling decisions may take place under the following four circum-
stances:
1.
When a process switches from the running state to the waiting state (for
example, as the result of an
I/O
request or an invocation of
wait()
for the
termination of a child process)
2.
When a process switches from the running state to the ready state (for
example, when an interrupt occurs)
3.
When a process switches from the waiting state to the ready state (for
example, at completion of
I/O
)
4.
When a process terminates
For situations 1 and 4, there is no choice in terms of scheduling. A new process
(if one exists in the ready queue) must be selected for execution. There is a
choice, however, for situations 2 and 3.
When scheduling takes place only under circumstances 1 and 4, we say that
the scheduling scheme is
nonpreemptive
or
cooperative
. Otherwise, it is
pre-
emptive
. Under nonpreemptive scheduling, once the
CPU
has been allocated
to a process, the process keeps the
CPU
until it releases it either by terminating
or by switching to the waiting state. Virtually all modern operating systems
including Windows, mac
OS
, Linux, and
UNIX
use preemptive scheduling algo-
rithms.
Unfortunately, preemptive scheduling can result in race conditions when
data are shared among several processes. Consider the case of two processes
that share data. While one process is updating the data, it is preempted so
that the second process can run. The second process then tries to read the
data, which are in an inconsistent state. This issue will be explored in detail
in Chapter 6.
