Problem No: 03
Name of the problem: PRIORITY CPU Scheduling Algorithm.
Theory: Priority scheduling is a
non-preemptive algorithm and one of the most common scheduling algorithms in
batch systems. Each process is assigned a priority. Process with the highest
priority is to be executed first and so on.
Code:
#include<bits/stdc++.h>
using
namespace std;
main()
{
int
p[20],bt[20],pri[20],tat[20],wt[20],i,k,n;
float tatavg, wtavg;
cout<<"enter the number of
process: " ;
cin>>n;
for(i=0; i<n; i++)
{
p[i]=i;
cout<<"enter the burst time
and priority of process ";
cout<<i<<" :";
cin>>bt[i]>>pri[i];
cout<<endl;
}
for(i=0; i<n; i++)
{
for(k=i+1; k<n; k++)
{
if(pri[i]>pri[k])
{
swap(p[i],p[k]);
swap(bt[i],bt[k]);
swap(pri[i],pri[k]);
}
}
}
wtavg=wt[0]=0;
tatavg=tat[0]=bt[0];
for(i=1; i<n; i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=tat[i-1]+bt[i];
wtavg=wtavg+wt[i];
tatavg=tatavg+tat[i];
}
printf("\nprocess\t\tpriority\tburst
time\twaiting time\tturnaround time");
for(i=0; i<n; i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",p[i],pri[i],bt[i],wt[i],tat[i]);
}
printf("\naverage waiting time %f",wtavg/n);
printf("\naverage turnaround time
%f",tatavg/n);
}
No comments:
Post a Comment