All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Tuesday, 9 September 2014

Round_robin using structures

#include<iostream.h>
#include<conio.h>
struct pro
{
int t[50];          //time of each turn
int burst;          //burst time
int rem;            //rem time of process
int tc;             //time at which process is completed
int ti;             //for counting purpose
int c;              //set when process is completed
int tu;             //number of turns
float wait;         //total waiting time for that process
};
void main()
{     clrscr();
      struct  pro p[100];
      int n,count=0,time_slice,s;
      float avg_wait=0,total_wait=0;
      cout<<"enter number of processes "<<endl;
      cin>>n;
      cout<<"enter burst time of each process "<<endl;
      for(int i=0;i<n;i++)
      {
cin>>p[i].burst;
p[i].rem=p[i].burst;
p[i].c=0;
p[i].ti=0;
p[i].tu=0;
      }
      s=n;
      cout<<"enter time slice "<<endl;
      cin>>time_slice;
      while(s!=0)
      {
for(i=0;i<n;i++)
{
if(p[i].c==0)
{
if(p[i].rem<time_slice)
{
    count=count+p[i].rem;
    p[i].tc=count;
    p[i].t[p[i].ti]=p[i].rem;
   p[i].rem=0;
    p[i].tu++;
   p[i].ti++;

}
else
{    count=count+time_slice;
    p[i].tc=count;
    p[i].t[p[i].ti]=time_slice;
    p[i].rem=p[i].rem-time_slice;
    p[i].tu++;
    p[i].ti++;
}
if(p[i].rem==0)
{
p[i].c=1;
s--;
}
}
}  }

for(i=0;i<n;i++)
{       int temp=0;
for(int j=0;j<p[i].tu;j++)
{    temp=temp+p[i].t[j];

}
p[i].wait=p[i].tc-temp;
}
cout<<endl;
cout<<"process name\twaiting time\tturns "<<endl;
for(i=0;i<n;i++)
{
cout<<"p"<<i+1<<"\t\t\t"<<p[i].wait<<"\t"<<p[i].tu<<endl;
total_wait=total_wait+p[i].wait;
}
cout<<endl<<"total waiting time of all processes= "<<total_wait<<endl;
avg_wait=total_wait/n;
cout<<endl<<"average waiting time= "<<avg_wait;
getch();
      }







1 comment: