Wednesday 28 November 2012

Round Robin scheduling algorithm


//Round Robin scheduling algorithm implementation using C++
#include"iostream.h"
#include"conio.h"
#include"string.h"

int total,i,j,n,m;
float avg;

class fcfs
{
 int bt[12],wt[12],et[12],rt,timer,count,found;
 char p[12][6];
 public:
 fcfs()
 {
  wt[0]=0;
  total=0;
  avg=0;
 }
 void input(int n);
 void apply();
 void display();
};
//program written by Mars. 20/09/2011. OS lab.

 void fcfs::input(int n)
 {
  for(i=0;i<n;i++)
  {
   cout<<"Enter process "<<i+1<<" name :";
   cin>>p[i];
   cout<<"Enter process time :";
   cin>>bt[i];
   cout<<"enter burst time";
   cin>>bt[i];
  }
 }

 void fcfs:: apply()
 {
  timer=4;
  m=n;
  wt[0]=0;
i=0;
do
{
if(bt[i]>timer)
{
rt=bt[i]-timer;
strcpy(p[n],p[i]);
bt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=bt[i];
}
i++;
wt[i]=wt[i-1]+et[i-1];
}
while(i<n);
count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{
wt[i]=wt[found]-(count*timer);
count=0;
found=0;
}
}
for(i=0;i<m;i++)
{
total+=wt[i];
}
avg=(float)total/m;
 }

 void fcfs::display()
 {
  cout<<"P_name\tP_time\tW_time\n";
  for(i=0;i<n;i++)
  cout<<p[i]<<"\t"<<bt[i]<<"\t"<<wt[i]<<"\n";
 }

void main()
{
 fcfs fcfs;
 clrscr();
 cout<<"Enter no. of processes :";
 cin>>n;
 fcfs.input(n);
 fcfs.apply();
 fcfs.display();
 cout<<"total waiting time :"<<total<<"\n";
 cout<<"Average waiting Time :"<<avg;
 getch();
}

No comments:

Post a Comment

You are welcome, your comment helps me to make this blog more better.