Guest User

Untitled

a guest
Feb 28th, 2012
1,732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include "roundrobin.h"
  2. #include<iomanip>
  3. #include<iostream>
  4. using namespace std;
  5. roundrobin::roundrobin(void)
  6. {
  7.     rq=w=t=NULL;
  8. }
  9.  
  10. roundrobin::~roundrobin(void)
  11. {
  12.     if(rq!=NULL)
  13.     {
  14.         delete[] rq;
  15.         delete[] w;
  16.         delete[] t;
  17.         delete[] a;
  18.     }
  19. }
  20. int roundrobin::read()//read input from the user
  21. {
  22.     int i;
  23.     cout<<"Enter number of processes:";
  24.     cin>>n;
  25.     if(rq!=NULL)
  26.     {
  27.         delete[] rq;
  28.         delete[] w;
  29.         delete[] t;
  30.     }
  31.     try
  32.     {
  33.         rq=new int[n];
  34.         w=new int[n];
  35.         t=new int[n];
  36.         a=new int[n];
  37.     }
  38.     catch(bad_alloc &ba)
  39.     {
  40.         cerr<<ba.what()<<endl;
  41.         exit(1);
  42.     }
  43.     cout<<"Enter arrival times:\n";
  44.     for(i=0;i<n;i++)
  45.     {
  46.         cin>>a[i];
  47.     }
  48.     cout<<"Enter request times:\n";
  49.     for(i=0;i<n;i++)
  50.     {
  51.         cin>>rq[i];
  52.         w[i]=t[i]=0;
  53.     }
  54.     cout<<"Enter time quantum:";
  55.     cin>>q;
  56.     return 1;
  57. }
  58. void roundrobin::calc()//to calculate turn-around and wait times of all processes and the ordering
  59. {
  60.     int j=0;
  61.     int time;
  62.     int k;
  63.     int i;
  64.     int *r;//remaining times
  65.     try
  66.     {
  67.         r=new int[n];
  68.     }
  69.     catch(bad_alloc &ba)
  70.     {
  71.         cerr<<ba.what()<<endl;
  72.         exit(1);
  73.     }
  74.     for(i=0;i<n;i++)    r[i]=rq[i];
  75.     bool f=false;//flag to indicate whether any process was scheduled as i changed from 0 to n-1 in the next for loop
  76.     int sp=0;//time spent
  77.     for(i=0;j<n;i=(i+1)%n)//while there are uncompleted processes
  78.     {
  79.         if(r[i]>0&&sp>=a[i])//find the next uncompleted process which has already or just arrived
  80.         {
  81.             f=true;
  82.             if(r[i]<=q)//if the process requests for time less than the quantum
  83.                 time=r[i];//time to be alloted in this turn is the complete requested time
  84.             else    time=q;//else, it is the quantum time
  85.             //schedule the process
  86.             t[i]+=time,r[i]-=time,order.push_back(i+1);
  87.             if(r[i]==0) j++;//if the process has got completed, increment j
  88.             for(k=0;k<n;k++)
  89.                 if(r[k]!=0&&k!=i&&a[k]<sp+time)//for all other arrived processes incompleted after scheduling this process
  90.                     if(!(a[k]<=sp))//if they arrived while scheduling this process
  91.                         w[k]+=sp+time-a[k],t[i]+=sp+time-a[k];//account for the time they spent waiting while the process was being scheduled
  92.                     else
  93.                         w[k]+=time,t[k]+=time;//add time to their wait times and turn-around times
  94.             sp+=time;
  95.             continue;
  96.         }
  97.         if(i==n-1)
  98.         {
  99.             if(!f)
  100.             //now there are no more arrived processes to be scheduled
  101.             //so change sp to the arrival time of next arriving process
  102.             {
  103.                 int it;
  104.                 int diff=0;//diff between present time spent and arrivaltime of next arriving process
  105.                 for(it=0;it<n;it++)
  106.                     if(sp<a[it])//if process has'nt yet arrived
  107.                     {
  108.                         if(diff==0) diff=a[it]-sp;
  109.                         else if(diff>a[it]-sp)  diff=a[it]-sp;
  110.                     }
  111.                 sp+=diff;
  112.             }
  113.             f=false;
  114.         }
  115.     }
  116.     delete[] r;
  117. }
  118. void roundrobin::display()
  119. {
  120.     int i;
  121.     float tav=0;//average turn-around time
  122.     float wav=0;//average wait time        
  123.     for(i=0;i<n;i++)
  124.         tav+=t[i],wav+=w[i];
  125.     tav/=n,wav/=n;
  126.     cout<<"Scheduling order:\n";
  127.     list<int>::iterator oi;    
  128.     for(oi=order.begin();oi!=order.end();oi++)
  129.         cout<<*oi<<"\t";
  130.     cout<<"\nAverage turn-around time = "<<tav<<endl<<"Average wait time = "<<wav<<endl;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment