0% found this document useful (0 votes)
29 views

4.2 Scheduling Algorithm

Uploaded by

hrajaa16
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

4.2 Scheduling Algorithm

Uploaded by

hrajaa16
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Operating System

By
Mr. Parag R. Sali
Lecturer
Department of Computer Technology
SNJB’s Shri. Hiralal Hastimal ( Jain Brothers)
Polytechnic, Chandwad
Program Name: Computer Engineering Group
Program Code : CO/CM/IF/CW
Semester : Fifth
Course Title : Operating System
Course Code : 22516

4.2 Types of Scheduling Algorithms


Scheduling Algorithms

CPU scheduling deals with the problem of deciding which of the processes in
the ready queue is to be allocated the CPU. There are many different CPU
scheduling algorithms. In this section, we describe several of them.
1 First-Come, First-Served Scheduling
By far the simplest CPU-scheduling algorithm is the first-come, first-served (FCFS)
scheduling algorithm. With this scheme, the process that requests the CPU first is
allocated the CPU first. The implementation of the FCFS policy is easily managed
with a FIFO queue. When a process enters the ready queue, its PCB is linked onto
the tail of the queue. When the CPU is free, it is allocated to the process at the
head of the queue. The running process is then removed from the queue. The
code for FCFS scheduling is simple to write and understand.
On the negative side, the average waiting time under the FCFS policy is often quite
long. Consider the following set of processes that arrive at time 0, with the length
of the CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3

If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we
get the result shown in the following Gantt chart, which is a bar chart that
illustrates a particular schedule, including the start and finish times of each of
the participating processes:
The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2,
and 27 milliseconds for process P3. Thus, the average waiting time is (0 + 24 +
27)/3 = 17 milliseconds. If the processes arrive in the order P2, P3, P1, however,
the results will be as shown in the following Gantt chart:

The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds.


The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds. This
reduction is substantial. Thus, the average waiting time under an FCFS
policy is generally not minimal and may vary substantially if the processes’
CPU burst times vary greatly.
2 Shortest-Job-First Scheduling

A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling


algorithm. This algorithm associates with each process the length of the
process’s next CPU burst. When the CPU is available, it is assigned to the process
that has the smallest next CPU burst. If the next CPU bursts of two processes are
the same, FCFS scheduling is used to break the tie. Note that a more appropriate
term for this scheduling method would be the shortest-next- CPU-burst
algorithm, because scheduling depends on the length of the next CPU burst of a
process, rather than its total length. We use the term SJF because most people
and textbooks use this term to refer to this type of scheduling.
As an example of SJF scheduling, consider the following set of processes,
with the length of the CPU burst given in milliseconds:
Process Burst Time
P1 6
P2 8
P3 7
P4 3

Using SJF scheduling, we would schedule these processes according to the


following Gantt chart:
The waiting time is 3 milliseconds for process P1, 16 milliseconds for process
P2, 9 milliseconds for process P3, and 0 milliseconds for process P4. Thus, the
average waiting time is (3 + 16 + 9 + 0)/4 = 7 milliseconds. By comparison, if
we were using the FCFS scheduling scheme, the average waiting time would
be 10.25 milliseconds.

The SJF scheduling algorithm is provably optimal, in that it gives the minimum
average waiting time for a given set of processes. Moving a short process
before a long one decreases the waiting time of the short process more than it
increases the waiting time of the long process. Consequently, the average
waiting time decreases.
The real difficulty with the SJF algorithm is knowing the length of the next
CPU request.
3 Priority Scheduling
The SJF algorithm is a special case of the general priority-scheduling algorithm. A
priority is associated with each process, and the CPU is allocated to the process
with the highest priority. Equal-priority processes are scheduled in FCFS order. An
SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of
the (predicted) next CPU burst. The larger the CPU burst, the lower the priority,
and vice versa.
Note that we discuss scheduling in terms of high priority and low priority.
Priorities are generally indicated by some fixed range of numbers, such as 0 to 7
or 0 to 4,095. However, there is no general agreement on whether 0 is the
highest or lowest priority. Some systems use low numbers to represent low
priority; others use low numbers for high priority. This difference can lead to
confusion. In this text, we assume that low numbers represent high priority.
As an example, consider the following set of processes, assumed to have arrived
at time 0 in the order P1, P2, · · ·, P5, with the length of the CPU burst given in
milliseconds:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Using priority scheduling, we would schedule these processes according to the
following Gantt chart: The average waiting time is 8.2 milliseconds.
Priorities can be defined either internally or externally. Internally defined
priorities use some measurable quantity or quantities to compute the priority
of a process. For example, time limits, memory requirements, the number of
open files, and the ratio of average I/O burst to average CPU burst have been
used in computing priorities. External priorities are set by criteria outside the
operating system, such as the importance of the process, the type and amount
of funds being paid for computer use, the department sponsoring the work, and
other, often political, factors.
Priority scheduling can be either preemptive or non-preemptive. When a
process arrives at the ready queue, its priority is compared with the priority of
the currently running process. A preemptive priority scheduling algorithm will
preempt the CPU if the priority of the newly arrived process is higher than the
priority of the currently running process. A non-preemptive priority scheduling
algorithm will simply put the new process at the head of the ready queue.
major problem with priority scheduling algorithms is indefinite blocking, or
starvation. A process that is ready to run but waiting for the CPU can be
considered blocked. A priority scheduling algorithm can leave some low
priority processes waiting indefinitely.

A solution to the problem of indefinite blockage of low-priority processes


is aging. Aging involves gradually increasing the priority of processes that
wait in the system for a long time.
4 Round-Robin Scheduling

The round-robin (RR) scheduling algorithm is designed especially for timesharing


systems. It is similar to FCFS scheduling, but preemption is added to enable the
system to switch between processes. A small unit of time, called a time
quantum or time slice, is defined. A time quantum is generally from 10 to 100
milliseconds in length. The ready queue is treated as a circular queue. The CPU
scheduler goes around the ready queue, allocating the CPU to each process
for a time interval of up to 1 time quantum.
To implement RR scheduling, we again treat the ready queue as a FIFO queue of
processes. New processes are added to the tail of the ready queue. The CPU
scheduler picks the first process from the ready queue, sets a timer to interrupt
after 1 time quantum, and dispatches the process.
One of two things will then happen. The process may have a CPU burst of less
than 1 time quantum. In this case, the process itself will release the CPU
voluntarily. The scheduler will then proceed to the next process in the ready
queue. If the CPU burst of the currently running process is longer than 1 time
quantum, the timer will go off and will cause an interrupt to the operating
system. A context switch will be executed, and the process will be put at the tail
of the ready queue. The CPU scheduler will then select the next process in the
ready queue.
The average waiting time under the RR policy is often long. Consider the following
set of processes that arrive at time 0, with the length of the CPU burst given in
milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
If we use a time quantum of 4 milliseconds, then process P1 gets the first 4
milliseconds. Since it requires another 20 milliseconds, it is preempted after the first
time quantum, and the CPU is given to the next process in the queue, process P2.
Process P2 does not need 4 milliseconds, so it quits before its time quantum
expires. The CPU is then given to the next process, process P3. Once each process
has received 1 time quantum, the CPU is returned to process P1 for an additional
time quantum. The resulting RR schedule is as follows:
Multilevel Queue Scheduling
Another class of scheduling algorithms has been created for situations in which
processes are easily classified into different groups. For example, a common
division is made between foreground (interactive) processes and background
(batch) processes. These two types of processes have different response-time
requirements and so may have different scheduling needs. In addition, foreground
processes may have priority (externally defined) over background processes.
A multilevel queue scheduling algorithm partitions the ready queue into several
separate queues (Figure). The processes are permanently assigned to one queue,
generally based on some property of the process, such as memory size, process
priority, or process type. Each queue has its own scheduling algorithm. For
example, separate queues might be used for foreground and background
processes. The foreground queue might be scheduled by an RR algorithm, while
the background queue is scheduled by an FCFS algorithm.
In addition, there must be scheduling among the queues, which is commonly
implemented as fixed-priority preemptive scheduling. For example, the
foreground queue may have absolute priority over the background queue. Let’s
look at an example of a multilevel queue scheduling algorithm with five queues,
listed below in order of priority:

1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes
Each queue has absolute priority over lower-priority queues. No process in the
batch queue, for example, could run unless the queues for system processes,
interactive processes, and interactive editing processes were all empty. If an
interactive editing process entered the ready queue while a batch process was
running, the batch process would be preempted.

Another possibility is to time-slice among the queues. Here, each queue gets a
certain portion of the CPU time, which it can then schedule among its various
processes. For instance, in the foreground–background queue example, the
foreground queue can be given 80 percent of the CPU time for RR scheduling
among its processes, while the background queue receives 20 percent of the
CPU to give to its processes on an FCFS basis.

You might also like