Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

School of Computer Science and Engineering

DECLARATION
I/We hereby declare that the project entitled “HYBRID CPU
SCHEDULING
ALGORITHM” submitted by me/us to the School of Computer Science
and
Engineering, VIT University, Vellore-14 in partial fulfillment of the
requirements for the award of the degree of Bachelor of Technology in
Computer Science and
Engineering is a record of bonafide work carried out by me/us under
the

supervision of Prof. MANIKANDAN K. I further declare that the work


reported in this project has not been submitted and will not be submitted,
either in part or in full, for the award of any other degree or diploma of this
institute or of any other institute or university

SUBMITTED BY
K.SANJEETH - 18BCI0156
L.P SAMPATH - 18BCI0234
School of Computer Science and Engineering

CERTIFICATE

The project report entitled “HYBRID CPU SCHEDULING ALGORITHM”


is prepared and submitted by K.Sanjeeth - 18BCI0156, L.P SAMPATH -
18BCI0234. It has been found satisfactory in terms of scope, quality and
presentation as partial
fulfillment of the requirements for the award of the degree of Bachelor
of Technology in Computer Science and Engineering in VIT University,
India.

ACKNOWLEDGEMENT
We would like to express our deepest appreciation to all those who provided
us the possibility to complete this report. A special gratitude we give to our
project manager, Prof. . MANIKANDAN K, whose contribution in
stimulating suggestions and encouragement, helped us to coordinate our
project especially in writing this report.

Furthermore, we would also like to acknowledge with much appreciation


the crucial role of the staff of VIT University, who gave the permission to
use all required equipment and the necessary materials to complete the
project “HYBRID CPU SCHEDULING ALGORITHM”. We must
appreciate the guidance given by other supervisor as well as the panels
especially in our project presentation that has improved our presentation
skills thanks to their comment and advices.

CONTENTS
• ABSTRACT

• INTRODUCTION

• PROGRAM (NEW ALGORITHM)

• OUTPUT(SCREENSHOTS)

• REFERENCES

ABSTRACT
The essential and important aspect of operating system is
multiprogramming. Multiprogramming is a process or method of executing
multiple processes simultaneously in the memory. Its main aim to minimize
the average waiting time, average turnaround time and maximize the CPU
utilization. There are various CPU scheduling algorithms are used to
performed multiprogramming like First Come First Serve (FCFS), Shortest
Job First (SJF), Priority Scheduling (PS) and Round Robin (RR). The most
widely used scheduling algorithm is Round robin scheduling among all of
them. It is an optimal CPU scheduling algorithm in timeshared systems. In
round robin algorithm the time quantum is static, and the performance of
these algorithms is totally depending upon the selection of time quantum.
This static time quantum decreases the performance of CPU scheduling like
Average waiting time, average turnaround time and CPU utilization. This
algorithm inherits the nature of both SJF and round robin algorithm
therefore it is a Hybrid Algorithm. The objective of this project is to
propose a new CPU scheduling algorithm which will perform better than the
simple Round Robin in terms of minimizing average waiting time, average
turnaround time and number of context switches.
INTRODUCTION: -
In a single-processor system, processor can run only one process at a time
and other processes must wait until the CPU is free and these processes can
be rescheduled again. The objective of multiprogramming is to run multiple
processes at a time and to maximize the uses of CPU. Scheduling is a
fundamental operating- system function. Almost all computer resources are
scheduled before use. The CPU is, of course, one of the primary computer
resources. Thus, its scheduling is central to operating-system design. CPU
scheduling determines which processes run when there are multiple run-able
processes.

This project deals with the simulation of CPU scheduling algorithms with C.

The metrics such as finishing time, waiting time, total time taken for the
processes tocomplete, number of rounds, etc are calculated for our
innovative algorithm.

Algorithms: -
Round Robin

• One of the oldest, simplest, fairest and most widely used algorithm is
round robin (RR).
• In the round robin scheduling, processes are dispatched in a FIFO
manner but are given a limited amount of CPU time called a time-
slice or a quantum.
• If a process does not complete before its CPU-time expires, the CPU
is preempted and given to the next process waiting in a queue. The
preempted process is then placed at the back of the ready list.
• Round Robin Scheduling is preemptive (at the end of time-slice)
therefore it is effective in time-sharing environments in which the
system needs to guarantee reasonable response times for interactive
users.
• The only interesting issue with round robin scheme is the length of
the quantum. Setting the quantum too short causes too many context
switches and lower the CPU efficiency. On the other hand, setting the
quantum too long may cause poor response time and approximates
FCFS.
• In any event, the average waiting time under round robin scheduling
is often quite long.

OUR INNOVATIVE ALGORITHM:


The proposed algorithm focuses on the improvement on CPU scheduling
algorithm. The algorithm reduces the waiting time and turnaround
time drastically compared to the other Scheduling algorithm and
simple Round Robin scheduling algorithm. This proposed algorithm
works in a similar way as but with some modification. It executes the
shortest job having minimum burst time first instead of FCFS simple
Round robin algorithm and it also uses Smart time quantum instead
of static time quantum. Instead of giving static time quantum in the
CPU scheduling algorithms, our algorithm calculates the Smart time
quantum itself according to the burst time of all processes. The
proposed algorithm eliminates the discrepancies of implementing
simple round robin architecture.
• In the first stage of the innovative algorithm CPU scheduling
algorithms all the processes are arranged in the increasing order of
CPU burst time. It means it automatically assign the priority to the
processes. Process having low burst time has high priority to the
process have high burst time.
• Then in the second stage the algorithm calculates the mean of the
CPU burst time of all the processes. After calculating the mean, it
will set the time quantum dynamically i.e. (average of mean and
lowest burst time)/2.
• Then in the last stage algorithm pick the first process from the ready
queue and allocate the CPU to the process for a time interval of up to
1 Smart time quantum. If the remaining burst time of the current
running process is less than 1 Smart time quantum then algorithm
again allocate the CPU to the Current process till its execution. After
execution it will remove the terminated process from the ready queue
and again go to the stage 3.

The flowchart for proposed algorithm is shown below in figure 1:


EXPERIMENTAL ANALYSIS:

If all process arrives at same time in ready queue, then

PROCESS-ID ARIVAL TIME BRUST TIME

PROCESS -0 0 6

PROCESS-1 0 5

PROCESS-2 0 2

PROCESS-3 0 3

PROCESS-4 0 9

ROUND ROBIN ALGORITHM: -

Time quantum=3ms Gantt

chart:
P0 P1 P2 P3 P4 P0 P1 P3 P4 P0 P1 P4
0 2 4 6 8 10 12 14 15 17 19 20 25

PROCESS ID WAITING TIME TURNAROUND TIME

PROCESS-0 13 19

PROCESS-1 15 20

PROCESS-2 4 6

PROCESS-3 12 15

PROCESS-4 16 23
Average waiting time=12

Average turnaround time=16.6

OUR INNOVATIVE ALGORITHM: -


Gantt chart:

Average ofburst time=5

Minimum burst time=2

SMT= (average of burst time+highest burst time)/2=3.5 ≈3


P2 P3 P1 P1 P0 P0 P4 P4 P4
0 3 6 9 11 14 17 20 23 25

PROCESS ID WAITING TIME TURNAROUND TIME

PROCESS-0 10 16

PROCESS-1 5 10

PROCESS-2 0 2

PROCESS-3 2 5

PROCESS-4 16 25
Average waiting time=6.600

Average turnaround time=11.600


Number
Algorithm Average Average turnaround Of
waiting time time Context switches

Round robin 12 `16.6 11

Hybrid 6.00 11.6 4


schedulin
g algorithm

If all process doesn’t arrive at same time in ready queue, then

PROCESS-ID ARIVAL TIME BRUST TIME

PROCESS -0 5 5

PROCESS-1 4 6
PROCESS-2 3 7

PROCESS-3 1 9

PROCESS-4 2 2

PROCESS-5 6 3

ROUND ROBIN ALGORITHM: -

Time quantum=3ms
Gantt chart:
CPU P3 P4 P2 P1 P3 P0 P5 P2 P1 P3 P0 P2

PROCESS ID WAITING TIME TURNAROUND TIME

PROCESS-0 22 27

PROCESS-1 17 23

PROCESS-2 23 30

PROCESS-3 20 29

PROCESS-4 2 4

PROCESS-5 12 15
Therefore, Average waiting time=16 Average

turnaround time=21.33

OUR INNOVATIVE ALGORITHM: -


Average ofburst time=5.33 Minimum

burst time=2

SMT= (average of burst time+highest burst time)/2=3.665 ~3


Gantt chart:
CPU P3 P4 P0 P0 P5 P1 P1 P2 P3 P3 P2 P2

IDEAL
0 1 4 6 9 11 14 17 20 23 26 29 32 33

PROCESS ID WAITING TIME TURNAROUND TIME

PROCESS-0 1 6

PROCESS-1 10 16

PROCESS-2 23 30

PROCESS-3 19 28

PROCESS-4 2 4

PROCESS-5 5 8
Therefore, Average waiting time=10

Average turnaround time=15.333

COMPARISION: -

Algorithm Average Average NumberOf


waiting time turnaround Context
time switches
Round robin
16 21.33 11
Hybrid
scheduling 10.00 15.33 7
algorithm

CODE:

#include<stdio.h>
int main(){

int n,i,j,bt[100],at[100],po,temp,min,smt,sum=0,num,b[100],time=
0,max,k,p[100],l,wt [100],tt[100],g[100],st[100],f=0,m;
printf("Enter Number of Process:");
scanf("%d",&n);
num=n;
for(i=0;i<n;i++){
printf("Enter the brust time of Process-%d:",i);
scanf("%d",&bt[i]);
printf("Enter the arival time of Process-%d:",i);
scanf("%d",&at[i]);
p[i]=i;
wt[i]=0;
}
int a=at[0];
for(i=0;i<n;i++){
if(at[i]<a)
a=at[i];
}
if(a!=0){
for(i=0;i<n;i++){
at[i]=at[i]-a;

}
for(i=0;i<n;i++){
min=bt[i];
po=i;
for(j=i;j<n;j++){
if(bt[j]<min && at[i]==at[j]){
min=bt[j];
po=j;
}
}
temp=bt[po];
bt[po]=bt[i]; bt[i]=temp;
temp=at[po];
at[po]=at[i];
at[i]=temp;
temp=p[po]; p[po]=p[i];
p[i]=temp;
}

for(i=0;i<n;i++){
min=at[i]; po=i;
for(j=i;j<n;j++){
if(at[j]<min){
min=at[j];
po=j;
}
}
temp=bt[po];
bt[po]=bt[i];
bt[i]=temp;
temp=at[po];
at[po]=at[i];
at[i]=temp;
temp=p[po];
p[po]=p[i];
p[i]=temp;
}

min=bt[0];
for(m=0;m<n;m++){

if(min>bt[m]){
min=bt[m];
}
}
for(i=0;i<n;i++){
sum=sum+bt[i];
}

sum=sum/n; smt=(sum+min)/2;

for(i=0;i<n;i++){
b[i]=bt[i];

for(i=0;i<n;i++){
if(b[i]!=0){
if(smt<b[i] ){
wt[i]=(time-at[i])-(bt[i]-b[i]);
;
b[i]=b[i]-smt;
time=time+smt;
tt[i]=time-at[i];

if(b[i]<=smt){
time=time+b[i]; b[i]=0;
tt[i]=time-at[i];
num--;

}
}
else{
wt[i]=(time-at[i])-(bt[i]-b[i]);
time=time+b[i];

b[i]=0;
num--;
tt[i]=time-at[i];

}
}
for(j=i+1;j<n && j!=i;j++){
min=b[j];
po=j;
for(k=j;k<n;k++){

if(b[k]<min && at[k]<time){


min=b[k];
po=k;
}
}
temp=b[po];
b[po]=b[j]; b[j]=temp;
temp=bt[po];
bt[po]=bt[j];
bt[j]=temp;
temp=at[po]; at[po]=at[j];
at[j]=temp; temp=p[po];
p[po]=p[j]; p[j]=temp;
temp=wt[po];
wt[po]=wt[j];
wt[j]=temp;
temp=tt[po];
tt[po]=tt[j]; tt[j]=temp;

}
if(i==n-1){

i=-1;
if(num==0){
break;
}
}

}
printf("process-id waitingtimeturnaroundtime \n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==p[j]){
printf("process-%d%d%d\n",p[j],wt[j],tt[j]);
}

}
}
int s=0,s1=0;
for(i=0;i<n;i++){
s=s+wt[i];
s1=s1+tt[i];
}

printf("Average waiting time: %f",(float)s/n);


printf("Average turnaround time: %f",(float)s1/n); return 0;
}

OUTPUT: -

If all process arrives at same time in ready queue, then:

If all process doesn’t arrive at same time in ready queue, then


CONCLUSION
In this project, we have presented a HYBRID CPU scheduling algorithm.
Results have shown that the proposed algorithm gives better results in terms
of average waiting time, average turnaround time and number of context
switches in all cases of process categories than the simple Round Robin
CPU scheduling algorithm. The general problem in Round Robin CPU
scheduling algorithm, is the choice of time quantum. In all these proposed
algorithms time quantum is static due to which in these cases the number of
context switches, average waiting time and average turnaround time will be
very high and in our proposed project find out a smart time quantum for all
processes which gives good performance as compared to RR.

REFERENCES: -

• Abdulraza, Abdulrahim,Salisu Aliyu, Ahmad M Mustapha, Saleh E


Abdullahi, “ An Additional Improvement in Round Robin (AAIRR)
CPU Scheduling Algorithm,”International Journal of Advanced
Research in Computer Science and Software EngineeringVolume 4,
Issue 2, February 2014 ISSN: 2277 128X.
• Manish kumar Mishra and Abdul kadir khan, “An Improved Round
Robin CPU scheduling algorithm,” Journal of Global Research in
Computer ScienceVolume 3, No. 6, June 2012.Neetu

• [3]Silberschatz, A., Galvin, P. B., and Gagne, G. 2005. Operating System Concepts. John
Wiley and Sons Inc.

• A. Abdulrahim, S. E. Abdullahi, and J. B. Sahalu, "A New Improved Round Robin (NIRR)
Scheduling Algorithm," International Journal of Computer Applications, Vol. 90, No. 4,
2014, 27- 33.

• R. J. Matarneh, "Self-Adjustment Time Quantum in Round Robin Algorithm Depending


on Time of Now Running Processes," American J. of Applied Sciences, Vol. 6, No. 10,
2009, 1831-1837

You might also like