Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. Round Robin Algo
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6.  
  7. long long ID[100007],busTime[100007],se[100007],wa[10000007],tr[10000007],temp[1000007],tempwait[100000];
  8. long long n,T;
  9.  
  10.  
  11.  
  12. void rr()
  13. {
  14.     cout<<"Enter time quantum: ";
  15.  
  16.     long long i,j,k,s,d,r,m;
  17.  
  18.     cin>>m; //quantum
  19.  
  20.     for(i=1; i<=T; )
  21.     {
  22.         d=0;  //flag for process if present
  23.         for(j=1; j<=n; j++)
  24.         {
  25.  
  26.             if(busTime[j]>0)
  27.             {
  28.                 d++;
  29.                 for(k=1; k<=m; k++)
  30.                 {
  31.                     if(busTime[j]==0 || i>T)
  32.                         break;
  33.  
  34.                     se[i]=j;
  35.                     busTime[j]--;
  36.  
  37.                     if(busTime[j]==0)
  38.                         tr[j]=i;
  39.  
  40.                     i++;
  41.  
  42.                 }
  43.             }
  44.         }
  45.  
  46.         if(!d)
  47.         {
  48.             T++;
  49.             i++;
  50.  
  51.         }
  52.     }
  53. }
  54.  
  55. int main()
  56. {
  57.     long long i,j,k,s,d,r;
  58.  
  59.     cout<<"Enter number of processes : ";
  60.     cin>>n;
  61.     cout<<"Enter process ID and burst time of "<<n<<" processes as follows - \n";
  62.     for(i=1; i<=n; i++)
  63.     {
  64.         cin>>ID[i];
  65.  
  66.         cin>>busTime[i];
  67.         T+=busTime[i];
  68.     }
  69.  
  70.     for(i=1; i<=n; i++)  temp[i]= busTime[i];
  71.  
  72.  
  73.  
  74.     cout<<"Round Robin Scheduling\n";
  75.  
  76.     rr();
  77.  
  78.     cout<<"\nGannt Chart"<<endl;
  79.     for(i=1; i<=T; i++)
  80.         cout<<i<<"\t"<<se[i]<<endl;
  81.  
  82.     cout<<endl;
  83.  
  84.     int TurnTotal=0;
  85.  
  86.     for(i=1; i<=n; i++)  TurnTotal+=tr[i];
  87.  
  88.  
  89.     for(i=1; i<=T; i++)
  90.     {
  91.         if(busTime[se[i]] == 0) tempwait[se[i]]= i-temp[se[i]];
  92.     }
  93.  
  94.     for(i=1; i<=n; i++)
  95.     {
  96.         cout<<"Task- "<<i<<" waits "<<tempwait[i]<<" seconds"<<endl;
  97.     }
  98.  
  99.     s=0;
  100.     for(i=1; i<=n; i++) s+=tempwait[i];
  101.  
  102.     double a,b,c;
  103.     a=s;
  104.     b=n;
  105.     c=a/b;
  106.  
  107.     cout<<"\nAverage waiting time "<<(double)s/(double)n<<endl;
  108.     cout<<"Average Turnaround Time: "<<(double)TurnTotal/(double)n<<endl;
  109. }
  110.  
  111.  
  112. /*
  113. 3
  114. 1 24
  115. 2 3
  116. 3 3
  117. 4
  118. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement