The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Thursday, August 4, 2011

Round Robin CPU Scheduling Algorithm (Pointers)


#include<stdio.h>
#include<conio.h>


int qt;

struct process
{
 int prn;
 int bt;
 int wt;
 int tt;
 struct process *nxt;
}*start,*np,*end,*temp;

void ins_node(struct process *np)
{
 if(start == NULL)
    start = end = np;
  else
  {
   end->nxt = np;
   end = np;
  }
}

void del_node(struct process *np,int choice)
{
  if(start == end)
   start = NULL;
  else
   start = start->nxt;
  if(choice == 1)
   delete np;
}

void ins_dat()
{
  char ch;
  int i=0;
  start = end = NULL;
  do
  {
  np = new process;
  np->nxt = NULL;
  np->prn = ++i;
  printf("\n Enter the Burst time of Process #%d : ",np->prn);
  scanf("%d",&np->bt);
  np->nxt = start;
  np->wt = np->tt = 0;
  ins_node(np);
  printf("\n\n Continue ?? : ");
  ch = getche();
  }while(ch == 'y');
}

void Round_Robin()
{
 float avg_wt,avg_tt;
 int i = 0,ttl_wt=0,ttl_tt=0;
 getch();
 printf("\n\n Process   Burst Time   Waiting Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 temp = start;
 do
 {
   printf(" %d \t\t %d \t\t",start->prn,start->bt);
   start->wt += (temp->tt - start->tt);
   if(qt >= start->bt)
   {
    start->tt = temp->tt + start->bt;
    start->bt = 0;
    printf(" %d \t\t %d\n\n",start->wt,start->tt);
    ttl_wt += start->wt;
    ttl_tt += start->tt;
    temp = np = start;
    del_node(np,1);
    ++i;
   }
   else
   {
    start->bt -= qt;
    start->tt = temp->tt + qt;
    printf(" %d \t\t %d\n\n",start->wt,start->tt);
    np = temp = start;
    del_node(np,2);
    ins_node(np);
   }
 }while(start != NULL);
    avg_wt = (float) ttl_wt/i;
    avg_tt = (float) ttl_tt/i;
   printf("\n\n Average Waiting Time  : %f",avg_wt);
   printf("\n Average Turnaround Time : %f",avg_tt);
}

void main()
{
 clrscr();
 printf("\n Enter the Quantum time : ");
 scanf("%d",&qt);
 ins_dat();
 Round_Robin();
 getch();
}

No comments:

Post a Comment