0% found this document useful (0 votes)
90 views7 pages

SJF

This document summarizes and compares three CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), and an enhanced version of SJF proposed by the authors. SJF provides the minimum average waiting time but can cause starvation of larger processes. FCFS has the maximum average waiting time as larger processes may monopolize the CPU if they arrive first. The enhanced SJF algorithm aims to provide average waiting times close to SJF while ensuring larger processes do not starve by running them after a set time interval. An example is provided to illustrate the execution of processes under each algorithm.

Uploaded by

Aditya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views7 pages

SJF

This document summarizes and compares three CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), and an enhanced version of SJF proposed by the authors. SJF provides the minimum average waiting time but can cause starvation of larger processes. FCFS has the maximum average waiting time as larger processes may monopolize the CPU if they arrive first. The enhanced SJF algorithm aims to provide average waiting times close to SJF while ensuring larger processes do not starve by running them after a set time interval. An example is provided to illustrate the execution of processes under each algorithm.

Uploaded by

Aditya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/234556241

OPTIMIZED SOLUTION TO SHORTEST JOB FIRST BY ELIMINATING THE


STARVATION

Conference Paper · November 2005

CITATIONS READS

8 2,985

2 authors:

Basit Shahzad Muhammad Tanvir Afzal


King Saud University Capital University of Science and Technology, Islamabad, Pakistan
78 PUBLICATIONS   484 CITATIONS    86 PUBLICATIONS   273 CITATIONS   

SEE PROFILE SEE PROFILE

Some of the authors of this publication are also working on these related projects:

ontology development View project

Information Security Mechanism for Real Wireless Mesh Network Scenario (E-healthcare) View project

All content following this page was uploaded by Basit Shahzad on 31 January 2017.

The user has requested enhancement of the downloaded file.


OPTIMIZED SOLUTION TO SHORTEST JOB FIRST BY
ELIMINATING THE STARVATION

BASIT SHAHZAD MUHAMMAD TANVIR AFZAL


COMSATS Institute of Information Technology COMSATS Institute of Information Technology
Ph: +92 (0) 300 5270145 Ph: +92 (0) 300 6013823
E-mail: [email protected] E-mail: [email protected]

Abstract Average waiting time in FCFS depends on


the order of arrival of Processes FCFS is a
Many algorithms are available for CPU relatively simple algorithm but it has its
scheduling including First Come First Serve problems.
(FCFS)[3], shortest job first (SJF)[3] and For example the Processes p1 (35), p2 (3)
Round Robin (RR)[3]. It has been observed and p3 (3) arrive in the order P1, P2, and P3.
that average waiting time in shortest job first P2 will have to wait 35ms and p3 for 38ms
is the minimum and it can not be reduced to execute. The Average waiting time is
further while average waiting time in the (35+38)/3 = 24.3ms. If the same processes
FCFS is the maximum. Along with arrives in the order P2, P3 and P1. Then
minimum average waiting time, SJF average waiting time is (3+6)/3=3ms.
introduces starvation for bigger processes,
which is a strong shortcoming of this smart So, if bigger processes arrive at the
technique. A new algorithm is suggested beginning of the queue in FCFS then these
which is called “Enhanced SJF” and gives processes can monopolize the CPU and
average waiting time which is convergent to average waiting time for the processes may
average waiting time of SJF and also ensures increase. FCFS deprives for the longer
that bigger processes are not going to starve average waiting time while SJF with smaller
and each process is executed in some average waiting time introduce starvation.
definite time. Along with surety that no job Enhanced SJF does not allow the bigger
will starve, the average waiting time is also processes to monopolize the CPU and gives
brought closer to average waiting time of chance to bigger processes to execute in
SJF in most cases and equal to average certain time, without delaying for indefinite
waiting time of SJF in some cases. period.

1. Introduction to CPU Scheduling We can consider an example for making a


Algorithms comparison between FCFS, SJF and
Enhanced SJF. All the following processes
The comparison of “Enhanced Shortest job arriving at time zero.
First” with FCFS and SJF is made. In SJF
average waiting time is minimum and in Processes CPU Burst Waiting Time
FCFS average waiting time is maximum. P1 15 0
P2 20 15
1.1 FCFS P3 3 35
P4 8 38
In FCFS CPU can be monopolized by a P5 9 46
single bigger process if bigger Processes are
run first, this can cause a large increase in By using FCFS let’s calculate average
the waiting time for other smaller Processes. waiting time for these Processes as shown in
figure 1.
P1 P2 P3 P4 P5
Let us take the same problem as discussed in
0 15 35 38 46 55 FCFS and let’s see the execution of SJF as
Figure No. 1: Execution of processes in shown in Figure No.2.
FCFS.
Processes CPU Burst Waiting Time
The average waiting time for FCFS is P1 15 20
(0+15+35+38+46) that is 134/5=26.8ms. P2 20 35
P3 3 0
Same processes are run by using SJF in P4 8 3
order to see the minimum average waiting P5 9 11
time for the processes. Before executing the
algorithm the shortest job first algorithm is P3 P4 P5 P1 P2
seen in detail.

1.2 SJF 0 3 11 20 35 55
Figure No. 2: Execution of processes in
On the other hand Shortest Job First selects SJF.
the process with shortest time of execution
and executes the process. For SJF the average waiting time is
(3+11+20+35)/5= 69/5=13.8ms which is the
When the CPU is available, the process with minimum average waiting time.
the shortest CPU burst is selected. If two or
more processes have the same CPU burst 1.3 Enhanced SJF algorithm
time, then the FCFS algorithm is used to
determine a tie. In Enhanced SJF, Shorter processes are
SJF is provably optimal. By moving shorter assigned the CPU as in SJF but this
processes to the front of the waiting queue, algorithm makes sure that after some
reduces the average waiting time up to interval bigger processes can get the CPU.
The interval is selected dynamically.
maximum extent.
In fact processes are sorted as soon as they
Major drawback of SJF is that shorter arrive in the ready queue based upon their
processes arrive in ready queue constantly execution time. Shorter processes are
and bigger processes continue growing arranged at the front of queue and bigger
towards tail of ready queues and their processes at the rear of the queue. Two
chance of getting CPU time even decreases. pointers are used, one at the front of the
SJF algorithm may cause a Process to block queue and second at the rear of the queue,
for indefinite time. which is used to pick a process either from
front or from rear of the queue.
When the IBM 7094 at MIT was shut down
in 1973, found that low priority Processes Then a process is picked from front of queue
were found that were submitted in 1967 and that is shortest process and its estimated
were still pending and had not yet been run time is saved in a variable ‘VarShorterJobs’
[1].Same the case may be with SJF if shorter and the estimated time of the biggest
processes arrive on regular basis and bigger processes is saved in variable ‘VarLargeJob’
processes will be waiting to get the chance then next process is picked from front and
and will not execute for a long time and may its estimated time is added to the variable
starve. ‘VarShorterJobs’ and compared it with the
variable ‘VarLargeJob’ when
But Enhanced SJF algorithm ensures that ‘VarShortJobs’ is equal to or greater than
bigger Processes will not starve and they the ‘VarLargeJob’ then a process is picked
will get the CPU after some specific time. from the rear and ‘VarShorterJobs’ is set to
zero and set the ‘VarLargeJob’ to the next now the value of ‘VarShorterJobs’ becomes
bigger process in the queue. In this way after 11. Again after execution of process P4.
equal time of execution of shorter processes Process P5 is selected from the front of the
a bigger process will also get the CPU and queue and value of variable
again return the control to the shorter ‘VarShorterJobs’ becomes 20. after the
processes to be picked next for execution. execution of P5. When the values of
variables are compared to pick the next
In this way bigger processes will not starve process then value of ‘VarShorterJob’
as may be in SJF and average waiting time becomes equal to value of the
for Enhanced SJF will also be approaching ‘VarLargeJob’. Now the bigger process will
to average waiting time of the SJF in many be given the chance for execution. After the
cases. Some solutions might be added by execution of P2, process P1 is selected for
introducing the time slices for each process. execution.
In that case additional computation is
required for context switches. By giving The difference between SJF and Enhanced
CPU to bigger processes without introducing SJF is that SJF will serve the process P1
the time slice, gives strength to the then the process P2 because process P1 has
Enhanced SJF algorithm for consideration. shorter CPU Burst time than process P2, but
Enhanced SJF execute Process P2 first
The Enhanced SJF for the same set of because the sum of the estimated time for
Processes which was discussed in FCFS and execution for shorter processes is greater
in SJF. than or equal to the estimated time for bigger
process which is waiting. So, Enhanced SJF
Processes CPU Burst Waiting Time gives an equal chance of execution to bigger
P1 15 40 processes as well. Figure No.4 shows the
P2 20 20 details.
P3 3 0
P4 8 3
P5 9 11 P3 P4 P5 P2 P1

In Enhanced SJF these Processes will be 0 3 11 20 40 55


inserted to the ready queue in the ascending Figure No. 4: Execution of processes in
order. After all processes are submitted they ESJF.
look like the figure 3.
Average waiting time for the Enhanced SJF
P3 P4 P5 P1 P2 is (20+40+3+11)/5 that is 74/5=14.8ms
which is closer to minimum average waiting
Figure No. 3: Arrangement of processes time. This is approximately equal to the
in ready queue for ESJF, P3 is on front Average waiting time calculated in SJF.
and P2 is on rear. Comparison for the average waiting time for
the same problem in three Algorithms is
Now there are two pointers, one pointing to given below.
front of the queue that is to the process P3
and second will be pointing to the process FCFS SJF Enhanced SJF
P2 at the rear of the queue. Value of 25.6 13.8 14.8
‘VarShorterJobs’ will be 3 and Process p3
gets the chance of execution. The value in By this example it is clear that average
variable ‘VarLargejob’ is 20. When process waiting time in Enhanced SJF is very close
p3 has been executed then the value of to SJF with the additional benefits that
‘VarShorterJobs’ is compare with longer jobs now have lesser tendency to
‘VarLargeJob’ as VarShorterJobs value is starve for CPU.
not greater or equal to ‘VarLargeJob’ so
next process from the front is chosen and
2. Detailed Discussion of Results
Average Waiting Time for FCFS=47.2ms
Enhanced SJF Algorithm is compared with Average Waiting Time for SJF=25.81ms
FCFS and SJF for different set of processes. Average Waiting Time for ESJF=29.4ms
Suppose Processes arrive at the time zero in
the order of P1, P2, P3, P4 and P5 and the d)
waiting time for FCFS, SJF and
ESJF(Enhanced Shortest Job First) are Processes CPU FCFS SJF ESJF
shown Burst
P1 30 0 37 37
a) P2 2 30 0 0
P3 20 32 17 17
Processes CPU FCFS SJF ESJF P4 10 52 7 7
Burst P5 5 62 2 2
P1 6 0 3 3 Figure No. 8 result of fourth comparison
P2 8 6 16 9 of FCFS, SJF and ESJF
P3 7 14 9 17
P4 3 21 0 0 Average Waiting Time for FCFS=35.2ms
Figure No. 5 result of first comparison of Average Waiting Time for SJF=12.0ms
FCFS, SJF and ESJF Average Waiting Time for ESJF=12.0ms

Average Waiting Time for FCFS=10.25ms e)


Average Waiting Time for SJF=7.0ms
Average Waiting Time for ESJF=7.2ms Processes CPU FCFS SJF ESJF
Burst
b) P1 8 0 11 20
P2 5 8 0 0
Processes CPU FCFS SJF ESJF P3 9 13 19 11
Burst P4 6 22 5 5
P1 25 0 28 28 Figure No. 9 result of fifth comparison of
P2 8 25 4 4 FCFS, SJF and ESJF
P3 16 33 12 12
P4 4 49 0 0 Average Waiting Time for FCFS=10.75
Figure No. 6 result of second comparison Average Waiting Time for SJF=8.75
of FCFS, SJF and ESJF Average Waiting Time for ESJF=9.0

Average Waiting Time for FCFS=26.7ms f)


Average Waiting Time for SJF=11ms
Average Waiting Time for ESJF=11ms Processes CPU FCFS SJF ESJF
Burst
c) P1 40 0 20 20
P2 100 40 275 205
Processes CPU FCFS SJF ESJF P3 90 140 185 285
Burst P4 45 230 60 105
P1 40 0 61 40 P5 80 275 105 205
P2 8 40 0 0 P6 20 355 0 0
P3 20 48 20 20 Figure No. 10 result of sixth comparison
P4 12 68 8 8 of FCFS, SJF and ESJF
P5 21 80 40 80 Average Waiting Time for FCFS=173.3ms
Figure No.7 result of Third comparison of Average Waiting Time for SJF=107.5ms
FCFS, SJF and ESJF Average Waiting Time for ESJF=112.5ms
possibility that they do not get CPU for
Comparisons of Enhanced SJF with FCFS execution will increase.
and SJF in terms of average waiting time as
shown in Figure No. 11. Enhanced SJF eliminate the problem of
starvation for bigger processes. Bigger
Cases FCFS SJF ESJF processes will avail the chance to get the
Case #a 10.25 7.0 7.2 CPU on their turn even though the shorter
Case #b 26.7 11 11 processes are present in the Ready Queue.
Case #c 47.2 25.81 29.4 Main emphasis of this work is to ensure that
Case #d 35.2 12.0 12.0 both smaller processes and bigger processes
Case #e 10.75 8.75 9.0 get the CPU. Neither one process should
Case #f 17 3.3 107.5 112.5 monopolize the CPU as in FCFS nor should
Figure No. 11: Comparison of FCFS, SJF bigger processes starve. It has been ensured
and ESJF in terms of average waiting that the average waiting time of Enhanced
time for different set of processes. SJF remains closer to the average waiting
time of shortest job first and also that no job
180 starve for CPU for longer time as does in
160 shortest job first.
140
120 Many bigger processes are urgent to execute
100 FCFS but submitted at the end of the queue. No
80 SJF conventional algorithm give them chance to
60 ESJF complete their execution in a certain time, In
40
SJF they will not get the CPU until all
20
shorter processes have been executed and in
FCFS as they are at the back of the queue
0
a b c d e f they will not get the CPU before their turn.
But Enhanced SJF will allocate the CPU to
that process on its turn while ensuring that it
FigureNo.12: Graph showing the is done in some definite amount of time.
comparative results for FCFS, SJF and
ESJF for different cases. As Enhanced SJF is non-preemptive, no
context switch occurs and there is no need
The Graph in Figure No.12 shows the to save the current states of the processes
comparison between FCFS, SJF and ESJF in which is seen as an overhead, in preemptive
terms of average waiting time. It is depicted scheduling policies.
from the graph that SJF and ESJF are very
close in terms of average waiting time and Enhanced SJF selects shorter processes first
the ESJF is giving CPU to the bigger jobs as then give chance to bigger processes, so
well in the presence of shorter jobs in the processes can not monopolize the CPU and
ready queue. CPU time is distributed among shorter
processes and bigger processes equally.
3. Conclusions
Enhanced SJF eliminate starvation for
This work focuses to provide an bigger processes because it gives equal
enhancement in existing algorithm. Shortest chance to bigger processes for execution.
Job First algorithm gives minimum average The dynamic policy of Enhanced SJF
waiting time but produce a serious problem provides equal chance of execution to both
i.e. starvation for the bigger processes. If smaller and bigger processes. The average
shorter processes keep on coming on regular wait time of Enhanced SJF is some time
basis to the ready queue the bigger processes more than that of shortest job first. This can
will remain in waiting state and the be overcomed by the benefit that the
Enhanced SJF will not let the bigger
processes starve for longer time and every
job submitted, does get the CPU with in
some adequate amount of time.

References

[1] Abraham Silberschatz, Peter Baer


Galvin, Greg Gagne, “Operating System
Concepts”, John Wiley & Sons Inc, 6th ed,
pp 160-pp 162

[2] Andrew S. Tanenbaum, “Modern


Operating System”, Prentice Hall, 2nd ed,
Chapter No.2

[3] Abraham Silberschatz, Peter Baer


Galvin, Greg Gagne, “Operating System
Concepts”, John Wiley & Sons Inc, 6th ed,
pp 160-200.

[4]William Stallings, “Operating System”,


Prentice Hall, 4th ed,

[5]http://williamstallings.com/Extras/OS-
Notes/h6.html

[6]http://www.sci.brooklyn.cuny.edu/~philar
is/cis25/cpusched.html

View publication stats

You might also like