Advertisement
FaisalAhemdBijoy

priority cpu

Oct 28th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. /*
  2. 5
  3. 3 10
  4. 1 1
  5. 4 2
  6. 5 1
  7. 2 5
  8. */
  9. #include<bits/stdc++.h>
  10. using namespace std;
  11. priority_queue <pair<int,pair<int,int > > >pq;
  12. int main()
  13. {
  14.     int num,bt,prior,waiting=0,turnaround=0;
  15.     cout<<"Enter The process Number "<<endl;
  16.     cin>>num;
  17.     cout<<"Enter The priority Number and burst time "<<endl;
  18.     int ans[num+10],totaltime=0,sumtime=0;
  19.     for (int i=0; i<num; i++)
  20.     {
  21.         cin>>prior>>bt;
  22.         pq.push(make_pair(-1*prior,make_pair(bt,i))) ;
  23.     }
  24.  
  25.  
  26.     while(!pq.empty())
  27.     {
  28.         pair<int,pair<int,int> >t=pq.top();
  29.         int pos=t.second.second;
  30.         totaltime=t.second.first;
  31.         cout<<"Top value pq: "<<-1*t.first<<"-> Position : "<<pos+1<<"-> Start time : "<<sumtime<<" ";
  32.         waiting=waiting+sumtime;
  33.         sumtime=sumtime+totaltime;
  34.         cout<<"-> Finish time : "<<sumtime<<endl;
  35.         turnaround=turnaround+sumtime;
  36.  
  37.         pq.pop();
  38.  
  39.     }
  40.     cout<<"Avg Waiting time : "<<1.0* waiting/num<<endl;
  41.     cout<<"Avg Turn around time : "<<1.0*turnaround/num<<endl;
  42.     cout<<"Throughput : "<<1.0*num/turnaround<<endl;
  43.  
  44. }
  45. /*
  46. Enter The process Number
  47. 5
  48. Enter The priority Number and burst time
  49. 3 10
  50. 1 1
  51. 4 2
  52. 5 1
  53. 2 5
  54. Top value pq: 1-> Position : 2-> Start time : 0 -> Finish time : 1
  55. Top value pq: 2-> Position : 5-> Start time : 1 -> Finish time : 6
  56. Top value pq: 3-> Position : 1-> Start time : 6 -> Finish time : 16
  57. Top value pq: 4-> Position : 3-> Start time : 16 -> Finish time : 18
  58. Top value pq: 5-> Position : 4-> Start time : 18 -> Finish time : 19
  59. Avg Waiting time : 8.2
  60. Avg Turn around time : 12
  61. Throughput : 0.0833333
  62. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement