Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Process Management - Lesson 2 – Process scheduling

Process scheduling
Process scheduling is the act of the process scheduler determining which process in the ready
state should be moved to the running state, i.e. deciding which process should be allocated for
the processor next. Process scheduling is an essential part of a multiprogramming operating
system since such operating systems allow more than one process to be loaded into the
executable memory at a time and loaded process shares the processor using time multiplexing.
The main objective of multiprogramming is to keep processes organized in such a manner that
there is always one executing in the CPU or processor. This ensures efficient utilization of the
CPU or processor by reducing its idle time. However, when two or more processes compete or
contend for the CPU or processor then a choice has to be made as to which process to allocate
the CPU or processor next. This procedure of determining the next process to be executed on the
CPU is known as process scheduling and the module or component of the operating system that
makes this decision is known as the scheduler.
Scheduling objectives or goals
A process is scheduled through a scheduling discipline with an aim of achieving the following
goals or objectives;
 Maximize throughput: Scheduling maximizes the number of processes that complete
per unit time.
 Maximize latency: Scheduling minimize the time each process waits before executing.
 Avoid indefinite postponement: Scheduling ensures that a process does not experience
an unbounded wait time before or while receiving service.
 Maximize resource utilization: Scheduling ensures that computer system resources are
busy and fully utilized.
 Enforce priorities: Where priorities are assigned to processes, scheduling favours the
higher priority processes, processes whose requests can be satisfied quickly or whose
completion frees other processes to run.
Scheduling criteria, schedulers performance criteria or scheduling methodology
The scheduler must consider the following performance measures and optimization criteria in
order to maximize the performance of the system.
 Fairness: This is the degree to which each process gets an equal chance to execute. The
scheduler must ensure that each process gets a fair share of CPU (processor) time i.e. no

1 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

process suffers starvation. However, it may treat different categories of processes (batch,
real time or interactive) in a different manner.
 CPU utilization: This is the percentage of time the CPU (processor) is busy in executing
processes. For efficient CPU (processor) utilization, the CPU (processor) must be kept as
busy as possible, i.e., there must be some process running at all times.
 Balanced utilization: This is the percentage time in which all system resources are busy.
It considers not only the CPU (processor) utilization but the utilization of I/O devices,
memory and on=her resource also. To get more work done by the system, the CPU
(processor) and I/O devices must be kept running simultaneously. Thus, it is desirable to
load a mixture of CPU bound and I/O bound processes in the main memory.
 Throughput: This is the total number of processes that a system can execute per unit of
time. It depends on the average length of the processes to be executed. For the systems
running long process, throughput will be less as compared to the systems running short
processes.
 Turnaround time: This is the amount of time that elapses from the time a process was
created to the time it terminates i.e. the life time of a process or a process’s life cycle. To
put it differently, it is the difference between the time a process enters the system and the
time it exits from the system. It includes all the time the process has spent waiting to join
the ready queue, the time spent in the ready queue waiting to be allocated for the CPU
(processor), the time spent in the CPU (processor) executing and in I/O queues. It is
inversely proportional to the throughput, i.e. the more the turnaround time the less the
throughput.
 Waiting time: This is the time spent by a process while waiting in the ready queue. It
does not include the execution time or time spent performing I/O operations. Thus, the
waiting time of a process is the difference between the process’s turnaround time and it
execution time (processing time).
 Response time: This is the time that elapses between the time when a user initiates a
request and the time the system responds to the request.
The scheduling concept
In a single-processor system, only one process can run at a time. Others must wait until the CPU
is free and can be rescheduled. The objective of multiprogramming is to have some process

2 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

running at all times, to maximize CPU utilization. Process is executed until it must wait,
typically for the completion of some I/O request.
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. Every time one process has to wait,
another process can take over use of the CPU.
CPU–I/O Burst Cycle
The success of CPU scheduling depends the way a process behaves during its execution. 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, which
is followed by another CPU burst, then another I/O burst, and so on. Eventually, the final CPU
burst ends with a system request to terminate execution. Thus, process execution comprises
alternate cycles of CPU and I/O burst as shown in the diagram below

Figure 1: Alternate cycles of CPU and I/O burst

3 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

The time period that elapses during the processing of a process before performing the next I/O
operation is known as CPU burst time and the time period that elapses while performing an I/O
operation before the CPU burst is known as I/O burst time.
The length of the CPU burst and the I/O burst varies from process to process depending on
whether the process is CPU bound or I/O bound. If the process is CPU bound, it will have longer
CPU bursts as compared to I/O bursts and vice versa in the case that the process is I/O bound.
During scheduling, only the length of the CPU burst is taken into consideration and not the
length of the I/O burst.
Scheduling queues
The operating system maintains all Process Control Blocks (PCBs) in process scheduling queues.
It maintains a separate queue for each of the process states and PCBs of all processes in the same
execution state are placed in the same queue. When the state of a process is changed, its PCB is
unlinked from its current queue and moved to its new state queue. The operating system
maintains three process scheduling queues namely:
Job or batch queue
This queue maintains all the processes in the system. As processes enter the system for
execution, they are accumulated into a job, batch or input queue on a mass storage device such as
hard disk.
Ready queue
This queue maintains a set of all processes residing in main memory, ready and waiting to
execute. A new process is always put in this queue. From the job queue, the processes which are
ready for execution are moved into the main memory. In the main memory, these processes are
kept into the ready queue. In other words, the ready queue contains all those processes that are
waiting for the processor,

4 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Figure 2: Process scheduling queues

Device, blocked or I/O waiting queue


This queue maintains processes which are blocked due to unavailability of an I/O request. For
each I/O device in the system, a separate device queue is maintained. The process that needs to
perform an I/O operation is added into the queue of that specific I/O device and waits until it is
served by the device.
Generally, both the ready queue and device are maintained as linked lists that contain PCBs of
the processes in the queue as their nodes as shown in the diagram below

Figure 3: Ready queue and device queue maintained as a linked list

Each PCB contains a pointer to the PCB of the next process in the queue. In addition the header
node of the queue contains pointers to the PCBs of the first and the last process in the queue as
shown in the diagram above.
Whenever a process in the job queue becomes ready to execute, it is brought into the ready queue
where it waits for CPU or processor allocation. Once the CPU or processor is allocated to it, i.e.
the process switches to the running state, the following transitions may occur,

5 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

 If the process needs to perform some I/O operations during its execution, it is removed
from the ready queue and put into the appropriate device queue. After the process
completes its I/O operation and is ready for execution, it is moved from the device queue
to the ready queue as show in the diagram below

Figure 4: Process scheduling queue.

 If an interrupt occurs, the CPU or processor can be taken away from the currently
executing process forcibly and the process has to wait until the interrupt is handled. After
that the process completes its I/O operation and is ready for the execution, it is switched
from the device queue to ready queue.
 If the time slice of the process has expired, the process is pit back into the ready queue.
 If a parent process creates a child process and has to wait until the child process
terminates, the parent process is suspended. After the execution of the child process, the
parent process is put back into the ready queue.
 If the process has successfully completed its task, it is terminated. The PCB and all the
resources allocated to the process are deallocated.
Note: In a single processor system, since there can be only one running process at a time, there is
no need to maintain a queue for the running processes.
Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected by the short-
term scheduler. This function involves the following:
6 Complied by Mr. P.K Munene
Process Management - Lesson 2 – Process scheduling

 Switching context
 Switching to user mode
 Jumping to the proper location in the user program to restart that program
The dispatcher should be as fast as possible, since 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
Process scheduler
A scheduler is an operating system module that selects the next process to be admitted into the
system and the next process to run. The aim of process scheduling is to assign the processor to
processes to be executed by the processor over time, in a way that meets scheduling objectives.
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 a process from the processes in memory that are ready to
execute and allocates the CPU to that process.
When to schedule
CPU-scheduling decisions may take place under the following four circumstances:
 When a process switches from running to waiting state: This transition occurs if the
process has to wait for an I/O event to occur or due to the termination of its child process
or some other reason. In such situations, the scheduler has to select a ready process for
execution.
 When a process switches from running to ready state due to occurrence of an
interrupt: In such a situation, the scheduler may decide to run a process from the ready
queue. If the interrupt was caused by some I/O device that has now completed its task,
the scheduler may choose the process that was blocked waiting for an I/O event.
 When a process switches from waiting state to ready state: This situation may occur
when the process has completed its I/O operation. In such a situation, the scheduler may
select either the process that has now come to the ready state or the current process may
be continued.
 When a process terminates and exits the system: In this case, the scheduler has to
select a process for execution from the set of ready processes.

7 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Scheduling levels
There three levels of scheduling namely:
 High level or long term scheduling
 Intermediate level or medium term scheduling
 Lower level scheduling
High level scheduling
High level scheduling also known as job scheduling or long term scheduling, is performed
when a new process is created. The "long-term scheduler" managers this. It determines which
process shall be allowed to compete actively for system resources. This level is sometimes called
admission scheduling since it determines which process gains admission to the system. The
high level scheduling policy dictates the degree of multiprogramming, the total number of
processes in a system at a given time.
Intermediate level scheduling
The intermediate level scheduling, also known as medium term scheduling determines which
process shall be allowed to compete for the processor after being admitted into the system by the
high level scheduling
Lower level scheduling
Lower level scheduling determines which active process will be assigned for a processor when
one next becomes available. Low level scheduling policies often assign a priority to each
process reflecting its importance, the more important a process, the more likely the scheduling
policy is to select it to execute next. The low-level scheduler also called the dispatcher also
assigns i.e. dispatches the selected process to a processor.

Figure 5: A five state transition diagram showing the positioning of all scheduling levels.

8 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Types of schedulers
In general, there are three different types of schedulers namely;
 Long term scheduler
 Medium term scheduler
 Short term scheduler
Long term scheduler
The long term scheduler, also known as the job scheduler or the admission scheduler, works
with the job queue. It selects the next process to be executed from the job queue and loads it into
the main memory for execution. The long term scheduler must select the processes in such a way
that some of the processes are CPU bound while others are I/O bound. This is because if all the
processes are COU bound, then the devices will remain unused most of the time. On the other
hand, if all the processes are I/O bound, then the CPU will remain idle most of the time.
Thus to achieve the best performance, a balance mix of CPU bound and I/O bound processes
must be selected. The main objective of this scheduler is to control the degree of
multiprogramming i.e., the number of processes in the ready queue, in order to keep the
processor utilization at the desired level. For this, the long term scheduler may admit new
processes in the ready queue in the case of poor processor utilization is high.
In addition, the long term scheduler is generally invoked only when a process exits from the
system. Thus, the frequency of invocation of the long term scheduler depends on the system and
workload and is much lower than that of the other two types of schedulers. The diagram below
shows the seven state process model showing the positioning of the three schedulers

9 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Figure 6: A seven state transition diagram showing the positioning of the three schedulers

Medium term scheduler


Medium term scheduler, also known as the swapper removes a suspended process from
memory or ready queue since it cannot make any progress toward completion. It does this so as
to create space for other processes. The suspended process is moved into secondary storage. This
process is called swapping. The term "swapping" refers to transferring a process out of main
memory and into secondary storage or vice-versa as shown in the diagram below

Figure 7: Queuing diagram with medium scheduler

10 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

This may occur when the operating system needs to make space for a new process, or in order to
restore a process to main memory that has previously been swapped out. Any process that is
inactive or blocked may be swapped into secondary memory and placed in a suspend queue until
it is needed again, or until space becomes available.
The swapped process is said to be swapped out or rolled out and is replaced in memory either
by a new process or by one of the previously suspended processes. The medium term scheduler
is in charge of handling the swapped out processes as shown in the diagram below

Figure 8: Types of schedulers

It selects a process among the partially executed or unexecuted swapped out processes and swaps
it in the main memory. It also removes suspended processes from the main memory and reduces
the degree of multiprogramming. The medium term scheduler is usually invoked when there is
some unoccupied space in the memory made by the termination of a process or if the supply of
ready processes reduces below a specified limit.
Short term scheduler
The short term scheduler also known as the CPU or processor scheduler or dispatcher
determines which active process among the pool of ready processes resident in memory will
execute next, i.e. which process the operating system assigns a processor to when one becomes
available. It selects a process from the ready queue and allocates the processor to it.

11 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Figure 9: Queuing diagram for scheduling

The short term scheduler is invoked frequently as compared to the long term scheduler since
processes execute for a short period of time and then wait for an event to occur, at which time the
processor must select some other process and allocate the processor to it.
It is invoked whenever an event occurs that may lead to the blocking of the current process or
that provides an opportunity to preempt a currently running process in favour of another.
Examples of such events include, clock interrupts, I/O interrupts, operating system calls, signals
etc. The time it takes for the short term scheduler to stop one process and start another is known
as dispatch latency.
The short term scheduler therefore, must be fast since it is invoked during every process switch
and is required to provide the least time gap between executions. Its main objectives are to
ensure efficient utilization of the processor and to provide an acceptable response time to users.
Short term schedulers are faster than long term schedulers.

12 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Comparison among schedulers


Long term scheduler Short term scheduler Medium term scheduler
It selects processes from pool It selects those processes It can re-introduce the process
and loads them into memory for which are ready to execute into memory and execution
execution can be continued.

It is a job scheduler It It is a processor scheduler It is a process swapping


scheduler or swapper
It controls the degree of It provides lesser control It reduces the degree of
Multiprogramming over degree of Multiprogramming.
multiprogramming
It is almost absent or minimal in It is also minimal in time It is a part of Time sharing
time sharing system sharing system systems.
Its speed less than that of the It is very fast It is of moderate speed
short term scheduler
Table 1: Comparison among schedulers

Context switching
Transferring the control processor from one process to another demands saving the context of the
currently running process and loading the context of another ready process. What is a process
context? The context of a process also known as the state information is the portion of Process
Control Block that contains the values of CPU registers, program counter, scheduling
information, base and limit register value, currently used register, changed state, I/O state
information, accounting information etc.
The scheduler switches the processor from executing one process to execute another. This
mechanism of saving the state for the old process and loading the saved state of the new process
on a PCB is known as context switch.
A context switch is the process of storing and restoring the state of a process so that execution
can be resumed from the same point at a later time. It may occur due to a number of reasons
some of which include:
 The current process terminates and exits from the system.
 The time slice of the current process expires.
 The process has to wait for I/O or some other resource.
 Some higher priority process enters the system.
The process relinquishes the CPU by invoking some system call.
Context switching is performed in two steps, which are as follows:

13 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

1. Save context: In this step, the kernel saves the context of the currently executing process
in its PCB so that it may restore it context later when the suspension is over and the
process’s execution is to be resumed.
2. Restore context: In this step, the kernel loads the saved context of a different process
that is to be executed next. If the process to be executed is newly created and the CPU
has not yet been allocated to it, there will be no saved context. In this case, the kernel
loads the context of the new process. However, if the process to be executed was in
waiting state due to I/O or some other reason, there will be saved context that can be
restored.
When a context switch occurs, the kernel saves the context of the old process in its PCB and
loads the saved context of the new process scheduled to run. At that point, the second process
can start executing. The diagram below shows context switch between two processes P0 and P1

Figure 10: Context switching

When a currently executing process P0 is interrupted, the operating system saves its context into
PCB0 and loads the state of P1 from PCB1. At this point process P0 is idle as process P1 is
executing. After process P1 executes to completion, the operating system saves it current context

14 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

into PCB1 and reloads process P0’s context from PCB1. At this point process P0 idles as process
P1 executes.
Using this technique, a context switcher enables multiple processes to share a single processor
(multitasking). Context switch time is pure overhead since the system does no useful work when
switching. Therefore context switching should be generally be avoided.

Figure 11: A simplified context switching model

The use of priorities


Process schedulers often use priorities to determine how to schedule and dispatch processes. In
many operating systems, each process is assigned a priority. Priorities are of two types namely:
 Static priorities: These are priorities that remain fixed i.e. they are not responsive to
changes in the environment.

15 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

 Dynamic priorities: These are priorities that are responsive to changes in the
environment.
Priority queueing diagram
When a scheduling selection is to be made, the scheduler starts at the highest priority ready
queue RQ0. There is one or more processes in the queue, a process is selected using some
scheduling policy. If RQ0 is empty, then RQ1 is examined, and so on. One problem with a pure
priority scheduling scheme is that lower priority processes may suffer starvation.
This will happen if there is always a steady supply of higher priority ready processes. If this
behavior is not desirable, the priority of a process can change with its age or execution history,
i.e. the scheduler will gradually increase a process’s priority as the process waits for service.
Eventually, its priority becomes high enough that the scheduler selects that process to run.
This act is known as aging and its purpose is to prevent indefinite postponement of processes.
The scheduler can gradually increase a process’s priority as the process waits for service.

Figure 12: Priority scheduling

Event signals
In an attempt to maximize resource utilization and achieve interactivity, processors inform the
operating system of events such as program execution errors and changes in devices status. Most
devices send two types of signals to the processor when events occur. A processor may receive
two types of signals namely:
 Exceptions
16 Complied by Mr. P.K Munene
Process Management - Lesson 2 – Process scheduling

 Interrupts
Exceptions
These are signals that indicate that an error has occurred, either in hardware or as a result of
software instruction. Instead of causing the system to fail, the processor typically invokes the
operating system to determine how to respond, e.g. the operating system may determine that the
process causing the error should be terminated or that the system must be restarted. If the system
must fail, the operating system can do so gracefully, reducing the amount of lost work. There are
three types of exceptions namely:
 Fault: This is an exception that indicates an error that an exception handler can correct.
 Trap: This is an exception that indicates the occurrence of an overflow (when the value
stored by a register exceeds the capacity of the register) and when a program control
reaches a breaking point.
 Abort: This is an exception that indicates the occurrence of an error from which a
process cannot recover, such as hardware failure.
Note: An expected exception is a trap while an unexpected exception is a fault. The main
difference between an interrupt and an exception is that, interrupts are caused by hardware
devices while exceptions are caused by software executing instruction.
Interrupts
These are signals that notify the processor that an event such as the passing of a timer interval,
has occurred or an external device’s status such as an I/O completion, has changed.
Types of interrupts
The set of interrupts a computer system supports is dependent on the system’s architecture. The
following are some of the types of interrupts recognized by the IA-32 architecture:
Hardware or I/O interrupts
These are interrupts initiated by the input/output hardware. They notify a processor that the
status of a channel or device has changed. I/O interrupts occur when an I/O operation completes.
Devices that generate interrupts, typically in the form of I/O signals and timer interrupts, are
external to a processor. These interrupts are caused by some event that is unrelated to a process’s
current instructions being executed by the processor and thus are said to be asynchronous.

17 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Types of hardware interrupts


There two types of hardware interrupts namely:
 Maskable interrupts: These are hardware interrupts whose request can be either
accepted or rejected by the processor. The processor has the facility for accepting or
rejecting hardware interrupts. Programming the processor to reject an interrupt is referred
to as masking or disabling and programming the processor to accept an interrupt is
referred to as unmasking or enabling.
 Non- Maskable interrupts: These are hardware interrupts whose requests have to be
definitely accepted (or cannot be rejected) by the processor. Whenever a request is made
by non Maskable interrupt, the processor has to definitely accept that request and service
that interrupt by suspending its current program and executing an Interrupt Service
Routine (ISR).
Software or program interrupts
Also known as traps, these are interrupts that occur as a result of executing a process’s
(program’s) instructions such as an illegal action such as dividing by zero, arithmetic overflow or
a memory handling error such as, referencing a protected memory location. Software generated
interrupts, such as system calls, are related to the process’s current instructions being executed
by the processor and thus are said to be synchronous with the running process since they are
generated in response to an instruction.
Types of software interrupts
 Timer interrupts: These are software interrupts generated by one of the processor’s
internal timer to allow the processor perform some time scheduled task. They are used for
tasks such as timekeeping and performance monitoring. Timers also enable the operating
system to determine if a process’s quantum has expired.
 Inter-processor interrupts: These are software interrupts that allow one processor to
send a message to another in a multiprocessor system.
Interrupt provide a low overhead means of gaining the attention of a processor. An alternative to
interrupts is for, a processor to repeatedly request the status of each device. This technique is
referred to as polling. Polling increases overhead as the complexity of the computer system
increases. Interrupts eliminate the need for a processor to repeatedly poll devices.

18 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

Interrupt handling
An interrupt is used to cause a temporary halt in the execution of a set of instructions (program).
As the processor is executing a program, an interrupt breaks the normal sequence of instruction
execution and diverts it to a program known as the Interrupt Service Routine (ISR). After
executing the ISR return control to the main program. The operating system handles an interrupt
as follows;
1. The processor receives an interrupt request (IRQ) or detects an exception condition when
running a program, the current running process is suspended and the processor saves its
current state of execution via a context switch.
2. Processor control is transferred to an Interrupt Service Routine (ISR) or an interrupt
handler. The operating system may specify a set of instruction (interrupt handler) to be
executed in response to each type of interrupt. This allows the operating system to gain
control of the processor to manage system resources.
3. The processor begins the execution of the Interrupt Service Routine (ISR) or an interrupt
handler.
4. When the execution of the ISR is complete, the operating system transfers the processor
control back to the interrupted process via another context switch and its execution
resumes without loss of continuity, unless recovery is not possible or the interrupt caused
its termination.

19 Complied by Mr. P.K Munene


Process Management - Lesson 2 – Process scheduling

References
 William Stallings (2009) Operating systems internals and design principles. Dorling
Kindersley publishing, Inc.
 H.M Deitel, P.J Deitel, D.R Choffnes (2007) Operating systems. Pearson Education, Inc.
and Dorling Kindersley publishing, Inc.
 D.M Dhamdhere (2003) Operating systems, a concept based approach. Tata McGraw Hill
Publishing Company Limited.
 Milan Milenkovic (2002) Operating systems, concepts and design. Tata McGraw Hill
Publishing Company Limited.
 A.S Tanenbaum, A.S Woodhull (2002) Operating systems design and implementation.
Pearson Education, Inc.
 Rohit Khurana (2014) Operating Systems, Vika publishing house PVT Limited.
 Iresh A.Dhotre (2009) Operating Systems, Technical Publish Pune.

20 Complied by Mr. P.K Munene

You might also like