The durations ofCPUbursts have been measured extensively. Althoughthey 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. Thecurve is generally characterized as exponential or hyperexponential, with alarge number of shortCPUbursts and a small number of longCPUbursts.AnI/O-bound program typically has many shortCPUbursts. ACPU-boundprogram might have a few longCPUbursts. This distribution can be importantwhen implementing aCPU-scheduling algorithm.5.1.2CPU SchedulerWhenever theCPUbecomes idle, the operating system must select one of theprocesses in the ready queue to be executed. The selection process is carried outby theCPUscheduler, which selects a process from the processes in memorythat are ready to execute and allocates theCPUto 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 readyqueue can be implemented as aFIFOqueue, a priority queue, a tree, or simplyan unordered linked list. Conceptually, however, all the processes in the readyqueue are lined up waiting for a chance to run on theCPU. The records in thequeues are generally process control blocks (PCBs) of the processes.burst durationfrequencyFigure 5.2Histogram of CPU-burst durations.
202Chapter 5CPU Scheduling5.1.3Preemptive and Nonpreemptive SchedulingCPU-scheduling decisions may take place under the following four circum-stances:1.When a process switches from the running state to the waiting state (forexample, as the result of anI/Orequest or an invocation ofwait()for thetermination of a child process)2.When a process switches from the running state to the ready state (forexample, when an interrupt occurs)3.When a process switches from the waiting state to the ready state (forexample, at completion ofI/O)4.When a process terminatesFor 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 achoice, however, for situations 2 and 3.When scheduling takes place only under circumstances 1 and 4, we say thatthe scheduling scheme isnonpreemptiveorcooperative. Otherwise, it ispre-emptive. Under nonpreemptive scheduling, once theCPUhas been allocatedto a process, the process keeps theCPUuntil it releases it either by terminatingor by switching to the waiting state. Virtually all modern operating systemsincluding Windows, macOS, Linux, andUNIXuse preemptive scheduling algo-rithms.Unfortunately, preemptive scheduling can result in race conditions whendata are shared among several processes. Consider the case of two processesthat share data. While one process is updating the data, it is preempted sothat the second process can run. The second process then tries to read thedata, which are in an inconsistent state. This issue will be explored in detailin Chapter 6.