Code :
// Header file for Cpu scheduling
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class cpuschedule
{
int n,Bu[20];
float Twt,Awt,A[10],Wt[10],w;
public:
//Getting the No of processes & burst time
void Getdata();
//First come First served Algorithm
void Fcfs();
//Shortest job First Algorithm
void Sjf();
//Shortest job First Algorithm with Preemption
void SjfP();
//Shortest job First Algorithm with NonPreemption
void SjfNp();
//Round Robin Algorithm
void RoundRobin();
//Priority Algorithm
void Priority();
};
// Implementation file for Cpu scheduling
#include "cpuh.h"
//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<<"
Enter The BurstTime for Process p"<<i<<"= ";
cin>>Bu[i];
}
}
//First come First served Algorithm
void cpuschedule::Fcfs()
{
int i,B[10];
Twt=0.0;
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
cout<<"
Burst 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];
}
//Calculating Average Weighting Time
for(i=1;i<=n;i++)
Twt=Twt+Wt[i];
Awt=Twt/n;
cout<<"
Total Weighting Time="<<Twt;
cout<<"
Average Weighting Time="<<Awt<<"
";
}
//Shortest job First Algorithm
void cpuschedule::Sjf()
{
int i,j,temp,B[10];
Twt=0.0;
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
cout<<"
Burst time for process p"<<i<<"= ";
cout<<B[i];
}
for(i=n;i>=1;i--)
{
for(j=1;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];
}
//calculating Average Weighting Time
for(i=1;i<=n;i++)
Twt=Twt+Wt[i];
Awt=Twt/n;
cout<<"
Total Weighting Time="<<Twt;
cout<<"
Average Weighting Time="<<Awt<<"
";
}
//Shortest job First Algorithm with NonPreemption
void cpuschedule::SjfNp()
{
int i,B[10],Tt=0,temp,j;
char S[10];
float A[10],temp1,t;
Twt=0.0;
w=0.0;
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
cout<<"
Burst time for process p"<<i<<"= ";
cout<<B[i];
S[i]='T';
Tt=Tt+B[i];
cout<<"
Enter the Arrival Time for"<<i<<"th process= ";
cin>>A[i];
}
for(i=n;i>=1;i--)
{
for(j=3;j<=n;j++)
{
if(B[j-1]>B[j])
{
temp=B[j-1];
temp1=A[j-1];
B[j-1]=B[j];
A[j-1]=A[j];
B[j]=temp;
A[j]=temp1;
}
}
}
for(i=1;i<=n;i++)
{
cout<<"
p"<<i<<" "<<B[i]<<" "<<A[i];
}
//For the 1st process
Wt[1]=0;
w=w+B[1];
t=w;
S[1]='F';
while(w<Tt)
{
i=2;
while(i<=n)
{
if(S[i]=='T'&&A[i]<=t)
{
Wt[i]=w;
cout<<"
WT"<<i<<"="<<Wt[i];
S[i]='F';
w=w+B[i];
t=w;
i=2;
}
else
i++;
}
}
for(i=1;i<=n;i++)
cout<<"
Wt"<<i<<"=="<<Wt[i];
//calculating average weighting Time
for(i=1;i<=n;i++)
Twt=Twt+(Wt[i]-A[i]);
Awt=Twt/n;
cout<<"Total Weighting Time="<<Twt<<"
";
cout<<"Average Weighting Time="<<Awt<<"
";
}
//Priority Algorithm
void cpuschedule::Priority()
{
int i,B[10],P[10],j;
w=0.0;
int max;
Twt=0.0;
max=1;
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
cout<<"
Burst time for process p"<<i<<"= ";
cout<<B[i];
cout<<"
Enter the priority for process P"<<i<<"= ";
cin>>P[i];
if(max<P[i])
max=P[i];
}
j=1;
while(j<=max)