|
| 1 | +//Priority Scheduling |
| 2 | +#include<bits/stdc++.h> |
| 3 | +//macro defination for "for loop" |
| 4 | +#define f(i,n) for(int i=0;i<n;i++) |
| 5 | +using namespace std; |
| 6 | +int main() |
| 7 | +{ |
| 8 | + cout<<"Enter the number of Processes : "; |
| 9 | + int p; |
| 10 | + cin>>p; |
| 11 | + vector<int> tat(p,0); //vector to store turn around time |
| 12 | + vector<int> wt(p,0); //vector to store waiting time |
| 13 | + vector<int> ct(p,0); //vector to store Completion Time |
| 14 | + vector<pair<int,int> > pribt; //vector of pair having priority and burst time |
| 15 | + f(i,p){ |
| 16 | + int priority; |
| 17 | + int bt; |
| 18 | + cout<<"Enter Burst Time and Priority of Process "<<i+1<<" :"<<endl; |
| 19 | + cin>>bt>>priority; |
| 20 | + pribt.push_back(make_pair(priority,bt)); |
| 21 | + } |
| 22 | + //sort function of standard template library |
| 23 | + sort(pribt.begin(),pribt.end()); |
| 24 | + //reverse function of standard template library |
| 25 | + reverse(pribt.begin(),pribt.end()); |
| 26 | + // Calculating the waiting time for all processes |
| 27 | + f(i,p){ |
| 28 | + if(i==0) |
| 29 | + continue; |
| 30 | + else |
| 31 | + wt[i] = pribt[i-1].second + wt[i-1] ; |
| 32 | + } |
| 33 | + // calculating turnaround time by adding |
| 34 | + // bt[i] + wt[i] |
| 35 | + f(i,p){ |
| 36 | + tat[i]=pribt[i].second+wt[i]; |
| 37 | + } |
| 38 | + //Calulation of Average wt and Turn Around time |
| 39 | + int wt_total=0; |
| 40 | + int tat_total=0; |
| 41 | + f(i,p){ |
| 42 | + wt_total+=wt[i]; |
| 43 | + tat_total+=tat[i]; |
| 44 | + } |
| 45 | + //format the dispaly results accordingly |
| 46 | + cout<<endl; |
| 47 | + cout<<"Priority Burst Time Wating Time Turn Around Time"<<endl; |
| 48 | + f(i,p)cout<<pribt[i].first<<" "<<pribt[i].second<<" "<<wt[i]<<" "<<tat[i]<<endl; |
| 49 | + cout<<"Average Waiting Time : "<<(float)wt_total/(float)p<<endl; |
| 50 | + cout<<"Average Turn Around Time : "<<((float)tat_total/(float)p); |
| 51 | +} |
0 commit comments