Sunteți pe pagina 1din 3

#include<iostream> int mod(int a,int b) { if(a<b) return a; else for(;a>=b;a=a-b); return a; } using namespace std; //round robin int

main() { int n,q; cout<<"Enter the time quantum"; cin>>q; cout<<"Enter the number of processess"; cin>>n; int a[n]; cout<<"\nEnter the arrival time of processes\n"; for(int i=0;i<n;i++) { cout<<"Process "<<i+1<<": "; cin>>a[i]; } cout<<"\nEnter the burst time of processes\n"; int b[n]; for(int i=0;i<n;i++) { cout<<"Process"<<i+1<<": "; cin>>b[i]; } cout<<"\n\n"; cout<<"Process No.\tExec_time\n"; int clock=0; int done[n],dcount=0; int ready[n],rcount=0; int wait[n],tat[n]; int flag=0; int k=0; int shortest=0; for(int init=0;init<n;init++) { wait[init]=0; } for(int init=0;init<n;init++) { tat[init]=b[init]; }

//cout<<"tat and wt done"; do { // int j=0; rcount=0; // while(j<n) for(int j=0;j<n;j++) { int flag1=0; if(a[j]<=clock) { for(int ki=0;ki<dcount;ki++) if(j==done[ki]) flag1=1; if(flag1==1) { //j++; continue; } ready[rcount]=j; rcount++;//cout<<"\n"<<rcount<<"\n"; } //j++; } //cout<<"\nReady queue elements"<<rcount; flag=0; if(rcount==0) if(rcount+dcount!=n) { clock++; } for(;rcount!=0;) { shortest=ready[k]; if(b[shortest]>q) { cout<<shortest+1<<"\t\t"<<q<<"\n"; b[shortest]-=q; clock=clock+q; for(int y=0;y<rcount;y++) { if(y!=k) wait[ready[y]]+=q; } k=mod(k+1,rcount); } else { cout<<shortest+1<<"\t\t"<<b[shortest]<<"\n"; done[dcount]=shortest; dcount++;

clock=clock+b[shortest]; for(int y=0;y<rcount;y++) { if(y!=k) wait[ready[y]]+=b[shortest]; } b[shortest]=0; for(int replace=k;replace<rcount;replace++) ready[replace]=ready[replace+1]; rcount--; } int x; for(int y=0;y<n;y++) { for(x=0;x<rcount;x++) { if(y==ready[x]) break; } if(x==rcount) if(a[y]<=clock) flag=1; } if(flag==1) break; if(rcount==0) if(rcount+dcount!=n) { clock++; break; } } }while(dcount<n); int totwait=0,tottat=0; for(int i=0;i<n;i++) { totwait=wait[i]+totwait; tat[i]=tat[i]+wait[i]; tottat=tottat+tat[i]; } cout<<"\nAverage waiting time: "<<float(totwait)/n; cout<<"\nAverage turn around time: "<<float(tottat)/n; return 0; }

S-ar putea să vă placă și