Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Round Robin Algo
- #include<bits/stdc++.h>
- using namespace std;
- long long ID[100007],busTime[100007],se[100007],wa[10000007],tr[10000007],temp[1000007],tempwait[100000];
- long long n,T;
- void rr()
- {
- cout<<"Enter time quantum: ";
- long long i,j,k,s,d,r,m;
- cin>>m; //quantum
- for(i=1; i<=T; )
- {
- d=0; //flag for process if present
- for(j=1; j<=n; j++)
- {
- if(busTime[j]>0)
- {
- d++;
- for(k=1; k<=m; k++)
- {
- if(busTime[j]==0 || i>T)
- break;
- se[i]=j;
- busTime[j]--;
- if(busTime[j]==0)
- tr[j]=i;
- i++;
- }
- }
- }
- if(!d)
- {
- T++;
- i++;
- }
- }
- }
- int main()
- {
- long long i,j,k,s,d,r;
- cout<<"Enter number of processes : ";
- cin>>n;
- cout<<"Enter process ID and burst time of "<<n<<" processes as follows - \n";
- for(i=1; i<=n; i++)
- {
- cin>>ID[i];
- cin>>busTime[i];
- T+=busTime[i];
- }
- for(i=1; i<=n; i++) temp[i]= busTime[i];
- cout<<"Round Robin Scheduling\n";
- rr();
- cout<<"\nGannt Chart"<<endl;
- for(i=1; i<=T; i++)
- cout<<i<<"\t"<<se[i]<<endl;
- cout<<endl;
- int TurnTotal=0;
- for(i=1; i<=n; i++) TurnTotal+=tr[i];
- for(i=1; i<=T; i++)
- {
- if(busTime[se[i]] == 0) tempwait[se[i]]= i-temp[se[i]];
- }
- for(i=1; i<=n; i++)
- {
- cout<<"Task- "<<i<<" waits "<<tempwait[i]<<" seconds"<<endl;
- }
- s=0;
- for(i=1; i<=n; i++) s+=tempwait[i];
- double a,b,c;
- a=s;
- b=n;
- c=a/b;
- cout<<"\nAverage waiting time "<<(double)s/(double)n<<endl;
- cout<<"Average Turnaround Time: "<<(double)TurnTotal/(double)n<<endl;
- }
- /*
- 3
- 1 24
- 2 3
- 3 3
- 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement