Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //for holding process info
  4. struct Process{
  5.     int id,a_t,b_t,initial_b_t,c_t,t_t,w_t,index;
  6. };
  7. //for sorting process according to arrival time
  8. bool compare(Process x,Process y){
  9.     if(x.a_t==y.a_t)return x.id<y.id;
  10.     return x.a_t<y.a_t;
  11. }
  12.  
  13. //for sorting process according to id
  14. bool compareid(Process x,Process y){
  15.     return x.id<y.id;
  16. }
  17. int main()
  18. {
  19.     int n, timequantum;
  20.     cin>>n;
  21.     Process p[n+1];
  22.     int a[n+1],b[n+1];
  23.     // Input stuff
  24.     for(int i=0; i<n; i++)cin>>a[i];
  25.     for(int i=0; i<n; i++)cin>>b[i];
  26.     cout<<"Enter the time quantum :"; cin>>timequantum;
  27.     for(int i=0; i<n; i++){
  28.         p[i].id=i+1;
  29.         p[i].a_t=a[i];
  30.         p[i].b_t=b[i];
  31.         p[i].initial_b_t=b[i];
  32.     }
  33.     //sort as arrival time
  34.     sort(p,p+n,compare);
  35.     for(int i=0; i<n; i++)p[i].index=i;// saving the index.
  36.  
  37.     queue <Process> pq;
  38.     int time=0,it=0;
  39.  
  40.     while(it<n){
  41.         if(pq.empty()){
  42.             // if no process available we increase the time to the next process A_T
  43.             if(time<p[it].a_t)time=p[it].a_t;
  44.         }
  45.         // Pushing the Process that are ready in the priority queue
  46.         while(p[it].a_t<=time){
  47.             pq.push(p[it]);
  48.             it++;
  49.         }
  50.         // picking front one from the queue.
  51.         Process curr=pq.front();
  52.         pq.pop();
  53.         int pos=curr.index;
  54.  
  55.         if( p[pos].b_t>timequantum){
  56.             p[pos].b_t-=timequantum;
  57.             time+=timequantum;
  58.             pq.push(p[pos]);
  59.         }
  60.         else{
  61.             if( p[pos].b_t==timequantum){
  62.                 time+=timequantum;
  63.             }
  64.             else{
  65.                 time+=p[pos].b_t;
  66.             }
  67.             p[pos].c_t=time;
  68.             p[pos].t_t=time- p[pos].a_t;
  69.             p[pos].w_t=p[pos].t_t- p[pos].initial_b_t;
  70.         }
  71.  
  72.     }
  73.     // now all the process is in the queue. and we will just calculate them.
  74.     while(!pq.empty()){
  75.        Process curr=pq.front();
  76.         pq.pop();
  77.         int pos=curr.index;
  78.  
  79.         if( p[pos].b_t>timequantum){
  80.             p[pos].b_t-=timequantum;
  81.             time+=timequantum;
  82.             pq.push(p[pos]);
  83.         }
  84.         else{
  85.             if( p[pos].b_t==timequantum){
  86.                 time+=timequantum;
  87.             }
  88.             else{
  89.                 time+=p[pos].b_t;
  90.             }
  91.             p[pos].c_t=time;
  92.             p[pos].t_t=time- p[pos].a_t;
  93.             p[pos].w_t=p[pos].t_t- p[pos].initial_b_t;
  94.         }
  95.     }
  96.  
  97.  
  98.     //Sorting as the id for printing purpose
  99.     sort(p,p+n,compareid);
  100.     double avgWaiting=0;
  101.  
  102.     //Printing in a table format
  103.     for(int j=0; j<100; j++)cout<<'-';
  104.         cout<<"\n| process ID | Arrival_Time | Burst_Time | Completion_Time | Turnaround_Time | Waiting_Time |\n";
  105.     for(int i=0; i<n; i++){
  106.         for(int j=0; j<100; j++)cout<<'-';
  107.         cout<<"\n|      P"<<p[i].id<<"    ";
  108.         cout<<"|      "<<p[i].a_t<<"      ";
  109.         cout<<"|      "<<p[i].initial_b_t<<"      ";
  110.         cout<<"|      "<<p[i].c_t<<"         ";
  111.         cout<<"|      "<<p[i].t_t<<"         ";
  112.         cout<<"|      "<<p[i].w_t<<"      |\n";
  113.         avgWaiting+=p[i].w_t;
  114.     }
  115.     for(int j=0; j<100; j++)cout<<'-';
  116.     cout<<"\n\nAvarage Waiting time: "<<avgWaiting/n<<endl;
  117.     return 0;
  118. }
  119.  
  120. /*
  121.  
  122. 5
  123. 2 5 1 0 4
  124. 6 2 8 3 4
  125.  
  126. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement