Saturday, July 30, 2011

Round Robin Simulator

This is a program I write to simply simulate Round Robin scheduling algorithm. I use C to write this. Using DOT NET Framework.
/*---------------------------------------------------------------*
 * This program is to simulate Round Robin scheduling algorithm. *
 * Developed using DOT NET framework 3.5 for supporting windows  *
 * environment.                                                  *
 *                                                               *
 * Developed by Jayanga Kaushalya @JK CREATIONS.                 *
 * jkaushalya@gmail.com                                          * 
 * 2010/07/30.                                                   *
 *---------------------------------------------------------------*/

#include 
#include 
#include 

struct process //Process structure.
{
 int prs_name;
 int brst_tme;
 int arivl_time;
 int wtn_tme;
 int pl;
};

void input();//Input details.
int pl_check( process *, int );//Priority level check.
void print( process *brst_tme, int, int );//Printing all.
int cmp(const void *a, const void *b);//Comparing structre using qsort.
void about();

void main()
{
 int chs;

 printf("\n\n\n\n\n\t\t\t******Menue******\n\n");
 printf("\t\t   1. Enter 1 for continue\n");
 printf("\t\t   2. Enter 2 View about and continue\n");
 printf("\t\t   3. Enter 0 for exit\n");
  scanf("%d", &chs);

 switch(chs)
 {
 case 1: {
  input();
  break;
   }
 case 2:{
     about();
  break;
     }
 case 0: {
  break;
   }
 default: {
  printf("Invalid option");
  break;
    }
 }

 system("Cls");
 printf("\n\n\n\n\n\n\n\t\t\tJK CREATIONS!");
 printf("\n\n\nPress any key to continue");
 getch();
}

void input()
{
 system("cls");

 int prss_cnt, q;
 struct process *prs;

 printf("Enter process count: ");
  scanf("%d", &prss_cnt);

 if( prss_cnt > 26 )
 {
  system("Cls");
  printf("Opps.....! this program only suports 26 programs.\n");
  printf("Sorry! for more wait for new version or conatct develpoer\n");
  printf("\n\n\nPress any key to continue");
  getch();
  return;
 }

    prs = (process*) calloc(prss_cnt, sizeof(process)); // prs is process array.
 
 {
  printf("Enter burst times\n");
  int p = 65;

  for ( int i = 0; i < prss_cnt; i++ )//Printing user friendly input environment.
  {
   prs[i].prs_name = p;
   printf("Process %c: ",p++ );
   scanf("%d", &prs[i].brst_tme);
  }

  printf("Arival times\n");
  p = 65;
  
  for ( int i = 0; i < prss_cnt; i++ )//Printing user friendly input environment.
  {
   printf("Process %c: ",p++ );
   scanf("%d", &prs[i].arivl_time);
  }
 }

 printf("Enter time quantum: ");
  scanf("%d", &q);
 printf("\n");
 print( prs, prss_cnt, q);
}

void print( process *prs, int prss_cnt, int q )//This is the palce that all the math happend.
{
 int ttl = 0;
 
 qsort( prs, prss_cnt, sizeof(process), cmp);// sorting arrya according to arrival time ascending.

 for(int i = 0; i < prss_cnt; i++) prs[i].pl = prss_cnt - i;//Pririty level is assing acording to arival time. Highest priority is first arival.
 for(int i = 0; i < prss_cnt; i++) ttl += prs[i].brst_tme;//Calculating total brst time.
 for(int i = 0; i < prss_cnt; i++) prs[i].wtn_tme = -(prs[i].arivl_time);//Adding initial wating times.
 
 {
  int c = 0;
  int chk_q = 0;

  while( c < ttl )//Until total burst time ends.
  {
   for(int i = 0; i < prss_cnt; i++)//Cheking if any process burst time is 0.
   {
    if( prs[i].brst_tme == 0 )
    {
     prs[i].pl = 0;//If 0 remove that process.
     prs[i].brst_tme = -1;

     for(int i = 0; i < prss_cnt; i++)
     {
      if(prs[i].pl == 0) continue;
      prs[i].pl++;
     }

     chk_q = 0;
    }
   }

   if( chk_q == q )//Cheking for time quantum expire.
   {
    for(int i = 0; i < prss_cnt; i++)
    {
     if(prs[i].pl == 0) continue;
     if(prs[i].pl == prss_cnt)
     {
      prs[i].pl = pl_check(prs, prss_cnt);
      continue;
     }
     prs[i].pl++;
    }

    chk_q = 0;
    continue;
   }

   for(int i = 0; i < prss_cnt; i++)//Print
   {
    if(prs[i].pl == prss_cnt)
    {
     printf("%c\t", prs[i].prs_name);
     prs[i].brst_tme--;
     for(int j = 0; j < prss_cnt; j++)//Wating time.
     {
      if(j == i) continue;
      if(prs[j].pl == 0) continue;
      prs[j].wtn_tme++;
     }
    }
   }
   
   chk_q++;
   c++;
  }
 }
 
 ttl = 0;
 printf("\n\nWating times:\n");
 for(int i = 0; i < prss_cnt; i++)
 {
  printf("%c: %d\n", prs[i].prs_name, prs[i].wtn_tme);
  ttl += prs[i].wtn_tme;
 }

 printf("\nAvereage wating time: %d\n", (ttl/prss_cnt));

 printf("\n\n\nPress any key to continue");
 free(prs);
 getch();
 
}

int cmp( const void *a, const void *b )//For qsort.
{
 struct process *x = (struct process*) a; 
 struct process *y = (struct process*) b;
 
 return ((int) (x->arivl_time - y->arivl_time));
}

int pl_check( process *prs, int prss_cnt )
{
 /* If when time quantm expire burst time also remaining then we must
  * set that process priority level to below last priority value. Not 0!
  */

 int x = 0;

 for( int i = 0; i < prss_cnt; i++) if(prs[i].pl == 0 ) x++;
 
 return x+1;
}

void about()
{
 system("cls");
 printf("\n\n\n\n\t\tDeveloped by Jayanga Kaushalya @JK CREATIONS.\n");
 printf("\t\tjkaushalya@gmail.com\n");
 printf("\t\t2010/07/30.\n\t\tVersion 1.0.0.1");
 printf("\n\n\nPress any key to continue");
 getch();
 input();
}

1 comments:

Ravindu said...

1st comment from the 1st follower :)

Visitors Count

Free Hit Counter