CPU Scheduling


//CPU Scheduling algorithms

//FCFS, Round Robin and Shortest Job First

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class cpuschedule

{

int n,Bu[20];

float Twt,Awt,Wt[20],Tat[20];

public:

void Getdata();

void Fcfs();

void Sjf();

void RoundRobin();

};

//Getting no of processes and Burst time

void cpuschedule::Getdata()

{

int i;

cout<<“Enter the no of processes:”;

cin>>n;

for(i=1;i<=n;i++)

{

cout<<“\nEnter The BurstTime for Process p”<<i<<“=”;

cin>>Bu[i];

}

}

//First come First served Algorithm

void cpuschedule::Fcfs()

{

int i,B[10];

float sum=0.0;

Twt=0.0;

for(i=1;i<=n;i++)

{

B[i]=Bu[i];

cout<<“\nBurst time for process p”<<i<<“=”;

cout<<B[i];

}

Wt[1]=0;

for(i=2;i<=n;i++)

{

Wt[i]=B[i-1]+Wt[i-1];

}

for(i=1;i<=n;i++)

{

Twt=Twt+Wt[i];

Tat[i]=B[i]+Wt[i];

sum+=Tat[i];

}

Awt=Twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=”<<Twt;

cout<<“\nAverage Waiting Time=”<<Awt;

cout<<“\nAverage Turnaround time=”<<sum;

}

//Shortest job First Algorithm

void cpuschedule::Sjf()

{

int i,j,temp,B[10];

float sum=0.0;

Twt=0.0;

for(i=1;i<=n;i++)

{

B[i]=Bu[i];

cout<<“\nBurst time for process p”<<i<<“=”;

cout<<B[i];

}

for(i=n;i>=1;i–)

{

for(j=2;j<=n;j++)

{

if(B[j-1]>B[j])

{

temp=B[j-1];

B[j-1]=B[j];

B[j]=temp;

}

}

}

Wt[1]=0;

for(i=2;i<=n;i++)

{

Wt[i]=B[i-1]+Wt[i-1];

}

for(i=1;i<=n;i++)

{

Twt=Twt+Wt[i];

Tat[i]=B[i]+Wt[i];

sum+=Tat[i];

}

Awt=Twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=”<<Twt;

cout<<“\nAverage Waiting Time=”<<Awt;

cout<<“\nAverage turnaround time=”<<sum;

}

//Round Robin Algorithm

void cpuschedule::RoundRobin()

{

int i,j,tq,k,B[10],Rrobin[10][10],count[10];

int max=0;

int m;

float sum=0.0;

Twt=0.0;

for(i=1;i<=n;i++)

{

B[i]=Bu[i];

cout<<“\nBurst time for process p”<<i<<“=”;

cout<<B[i];

if(max<B[i])

max=B[i];

Wt[i]=0;

}

cout<<“\nEnter the Time Quantum=”;

cin>>tq;

//TO find the dimension of the Round robin array

m=max/tq+1;

//initializing Round robin array

for(i=1;i<=n;i++)

{

for(j=1;j<=m;j++)

{

Rrobin[i][j]=0;

}

}

//placing value in the Rrobin array

i=1;

while(i<=n)

{

j=1;

while(B[i]>0)

{

if(B[i]>=tq)

{

B[i]=B[i]-tq;

Rrobin[i][j]=tq;

j++;

}

else

{

Rrobin[i][j]=B[i];

B[i]=0;

j++;

}

}

count[i]=j-1;

i++;

}

cout<<“Display”;

for(i=1;i<=n;i++)

{

for(j=1;j<=m;j++)

{

cout<<“\nRr[“<<i<<“,”<<j<<“]=”<<Rrobin[i][j];

cout<<”     “;

}

cout<<“\ncount=”<<count[i];

}

for(j=1;j<=n;j++)

{

for(i=1;i<=count[j];i++)

{

if(i==count[j])

{

for(k=1;k<j;k++)

{

if(k!=j)

Wt[j]+=Rrobin[k][i];

}

}

else

for(k=1;k<=n;k++)

{

if(k!=j)

Wt[j]+=Rrobin[k][i];

}

}

}

for(i=1;i<=n;i++)

cout<<“\nWaiting Time for process P”<<i<<“=”<<Wt[i];

//calculating Average Weighting Time

for(i=1;i<=n;i++)

{

Twt=Twt+Wt[i];

Tat[i]=B[i]+Wt[i];

sum+=Tat[i];

}

Awt=Twt/n;

sum=sum/n;

cout<<“\nTotal Waiting Time=”<<Twt;

cout<<“\nAverage Waiting Time=”<<Awt;

cout<<“\nAverage turnaround time=”<<sum;

}

void main()

{

int ch=0,cho;

cpuschedule c;

clrscr();

do

{

switch(ch)

{

case 0:

cout<<“\n0.MENU”;

cout<<“\n1.Getting BurstTime”;

cout<<“\n2.FirstComeFirstServed”;

cout<<“\n3.ShortestJobFirst”;

cout<<“\n4.RoundRobin”;

cout<<“\n5.EXIT”;

break;

case 1:

c.Getdata();

break;

case 2:

cout<<“FIRST COME FIRST SERVED SCHEDULING”;

c.Fcfs();

break;

case 3:

cout<<“SHORTEST JOB FIRST SCHEDULING”;

c.Sjf();

break;

case 4:

cout<<“ROUND ROBIN SCHEDULING”;

c.RoundRobin();

break;

case 5:

break;

}

cout<<“\nEnter your choice:”;

cin>>ch;

getch();

}while(ch<5);

}

Input & Output:

0.MENU

1.Getting BurstTime

2.FirstComeFirstServed

3.ShortestJobFirst

4.RoundRobin

5.EXIT

Enter your choice:1

Enter the no of processes:2

Enter The BurstTime for Process p1=7

Enter The BurstTime for Process p2=4

Enter your choice:2

FIRST COME FIRST SERVED SCHEDULING

Burst time for process p1=7

Burst time for process p2=4

Total Waiting Time=7

Average Waiting Time=3.5

Average Turnaround time=9

Enter your choice:3

SHORTEST JOB FIRST SCHEDULING

Burst time for process p1=7

Burst time for process p2=4

Total Waiting Time=4

Average Waiting Time=2

Average turnaround time=7.5

Enter your choice:4

SHORTEST JOB FIRST SCHEDULING

Burst time for process p1=7

Burst time for process p2=4

Total Waiting Time=4

Average Waiting Time=2

Average turnaround time=7.5

Enter your choice:5

Advertisements