Monday, February 27, 2012

ROUND ROBIN ALGORITHM IN C CODE

Round Robin: Round Robin is a CPU scheduling Algorithm,it can be used by CPU to executinting multiple files within time slots.




#include<stdio.h>
#include<conio.h>
 int ttime,i,j,temp;
 main()
 {
int pname2[10],pname[10];
int btime[10],btime2[10];
int n,x,z;
clrscr();
printf("Enter the no. of process:");
scanf("%d",&n);
printf("Enter the process name and burst time for the process\n");
for(i=0;i<n;i++)
{
    printf("Enter the process name:");
scanf("%d",&pname2[i]);
printf("Enter burst time for the process %d:",pname2[i]);
scanf("%d",&btime2[i]);
}
printf("PROCESS NAME \t\t BURST TIME\n");
for(i=0;i<n;i++)
   printf("%d\t\t\t %d\n",pname2[i],btime2[i]);

for(i=0;i<n;i++)
{
 pname[i]=pname2[i];
   btime[i]=btime2[i];
}


   z=1;
while(z==1)
{
ttime=0;


   printf ("PRESS 1.ROUND ROBIN 2.EXIT\n");
   scanf("%d",&x);
switch(x)
{
case 1:
rrobin(pname,btime,n);
break;
case 2:
exit(0);
break;
default:
printf("Invalid option");
      break;
       }
       printf("\n\n If you want to continue press 1:");
       scanf("%d",&z);
       }

 getch();
 }

 rrobin(int pname[],int btime[],int n)
       {
   int tslice;
   j=0;
   printf("\n\t ROUND ROBIN SCHEDULING \n\n");
   printf("Enter the time slice:\n");
   scanf("%d",&tslice);
   printf("PROCESS NAME \t REMAINING TIME\t TOTAL TIME");


 while(j<n)
   {
for(i=0;i<n;i++)
{
if(btime[i]>0)
{
   if(btime[i]>=tslice)
   {
ttime+=tslice;
btime[i]=btime[i]-tslice;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
if(btime[i]==0){
printf("  p%d is compltd and its trnarnd tme is=%d",pname[i],ttime);            }
   }
   else
   {
ttime+=btime[i];
btime[i]=0;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
      printf(" p%d is compltd and its trnarnd tme is=%d",pname[i],ttime);
}          }
 }     j++;
      }
 }


No comments:

Post a Comment