All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Tuesday 9 September 2014

C++ Code for Priority Based Scheduling Algorithm.

// Priority Based Scheduling Algorithm.
#include<iostream>
using namespace std;
struct process
{
int pno[10];
int p[10];
int bt[10];
}p;

int main()
{
int i,n,wt[10];
int twt=0;
float avg;
cout<<"Enter No of Processes: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<" Enter Process no,Priority and Brust time :";
cin>>p.pno[i]>>p.p[i]>>p.bt[i];
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(p.p[i]>p.p[j])
{
int temp=p.p[j];
p.p[j]=p.p[i];
p.p[i]=temp;
temp=p.bt[j];
p.bt[j]=p.bt[i];
p.bt[i]=temp;
temp=p.pno[j];
p.pno[j]=p.pno[i];
p.pno[i]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
  wt[i]=wt[i-1]+p.bt[i-1];
twt+=wt[i];
}
cout<<"P_NO\tPriority\tB_Time\tW_time\n";
for(i=0;i<n;i++)
{
cout<<p.pno[i]<<"\t"<<p.p[i]<<"\t"<<p.bt[i]<<"\t"<<wt[i]<<endl;
}
avg=twt/(float) n;
cout<<"Total waiting Time="<<twt<<endl;
cout<<"Average Waiting Time="<<avg;
return 0;
}

No comments:

Post a Comment