Process Scheduling (First Part)
Process Scheduling (First Part)
Process Scheduling (First Part)
1.3 Introduction
As operating systems allow for multiple processes sharing a single processor, the operating
system must establish a consistent scheme that chooses the next process to run on the CPU
in the event that the currently running process voluntarily or involuntarily relinquishes control
of the CPU.
An analogy for this concept (and we will be using this throughout this chapter) is the idea of
the CPU being a swing in a playground, with processes as children all lining up to use the
swing, which only one child at a time can use. All the children must take turns using the
swing in a way that effectively minimizes swing idle time and would allow all the children to
ride on the swing.
We will now introduce some scheduling concepts.
Operating Systems 1
CPU – I/O Burst cycle
Again, with our playground analogy, eventually a child on the swing will tire and would have to
rest for a while. To make sure that the swing is always used, a different child takes the place
in the swing. After a while, the child would be finished with his rest and would go back to the
line on the swing.
Operating Systems 2
time to produce user output. Most interactive applications such as word processors
and spreadsheets are usually I/O bound.
This distinction is used by the long-term scheduler to optimize CPU performance. We
will discuss the long-term scheduler later on.
1.3.3 Schedulers
Primarily, when we talk about process scheduling, we consider what is called the short-term
scheduler. The purpose of the short-term scheduler is to choose which process is to run
next on the CPU.
There are four possible times when the process selection is made.
● First, a process's CPU burst period ends and begins to wait for I/O. As discussed earlier, this
process is removed from the CPU to allow the CPU burst of other processes to run.
● A second scenario is when a process starts. Often, processes are created by other
processes. When you double click an application icon, the mouse control process
spawns the application process. When invoking an application from the command line,
the command line process starts your application process. A decision must be made
on which process to run, the parent process or the child process.
● Decisions are also made when a process ends.
● A fourth scenario occurs in certain types of scheduling algorithms where the OS
itself revokes a process' turn on the CPU in what is termed as preemption. We will
discuss this in detail in a later section.
For all of these scenarios, the scheduler always tries to maximize CPU use as well as give
all the processes a chance to run. The focus of the rest of this chapter is on what
scheduling algorithms are used by the short term scheduler.
It is worth mentioning that some operating systems come with a long-term scheduler. The
long-term scheduler is responsible for fine-tuning the mix of processes in the ready queue.
As was discussed earlier, processes are either CPU bound or I/O bound. If the processes in
the ready queue are mostly CPU bound, then most of the time a lot of the processes would
be waiting for their turn on the CPU. If most of the processes are I/O bound on the other
hand, then the CPU would be mostly idle as most of the processes would be waiting for I/O to
finish instead of running on the CPU. The long-term scheduler tries to prevent these
extremes from happening. Sometimes, the long-term scheduler may temporarily swap out
certain processes from memory (effectively making these processes halt) in order to
maintain an optimal mix of CPU and I/O bound processes.
1.3.4 Preemption
Some scheduling routines may suddenly forcibly evict a currently running process from the
CPU, even if that process is still not finished with its CPU burst. If this happens, we say
that the scheduling algorithm has a preempted the process.
Preemption may occur when a process with a higher priority enters the ready queue. This
will be discussed in greater detail later on.
Operating Systems 3
locations, goes to the latest executing instruction of the incoming process in order to pick
up where the process left off.
While this is being done, the CPU is left totally idle. In essence , the CPU does not do any
work while processes are being switched. This is why doing too much process switching
actually decreases CPU utilization, and has a derogatory effect in some scheduling algorithms.
Another way is through a process execution table. Here is an example process execution table:
Operating Systems 4
Processes: P1 with CPU burst 4, P2 with CPU burst 5, P3 with CPU burst 2 arriving at time
0 P4 with CPU burst 1 arriving at time 2.
Each row of the process execution table shows what the current state of the CPU and the
ready queue is at each second (indicated by the Time column). The CPU column indicates what
process is currently running on the CPU while the ready queue shows the processes in line,
sorted in an order described by the scheduling algorithm. The number in parenthesis next to
the processes indicates burst time left.
The process execution table is a great way to gather our metrics. It should follow that the
process running on the CPU has its burst time left reduced at every time cycle. A process with
4 seconds of burst time should appear for 4 rows on the CPU. The waiting time of a process is
the amount of time it spends in the ready queue. For example, the waiting time of P2 is 5
seconds as it appears for 5 rows in the process execution table. The waiting time for P1 is 0
as it is immediately chosen to run on the CPU.
To make things simple, most of discussion would not consider a context switch time,
although context switch time plays a pivotal role in some scheduling algorithms and will be
mentioned as such.
However, a problem comes with the FCFS algorithm. Consider a process P1 with CPU burst
time of 100 seconds, while 4 process P2 to P5 with burst time of 1 second each. If process
execution is done in the order P1, P2, P3, P4, P5, then the Gantt chart would look like this:
The average waiting time would be: (0 + 100 + 101 + 102 + 103) / 5 = 81.2 seconds
If however, we made P1 run last, then the Gantt chart would look like this:
Operating Systems 6