Round robin scheduling in C with arrival time

Round robin process scheduling algorithm in C with gantt chart.

#include<stdio.h>

struct p{
    int no,bt,at,rbt,ct;
};

int front=-1,rear=-1,n,pt=0,total=0;
int q[30];
struct p process[25];

void add(int c) //Insert element c into the queue
{
  if(front==-1)
    front=0;
  q[++rear]=c;
}

int rem() //Remove element and return it from the queue
{
  if(rear<front)
  {
    front=rear=-1;
    return -1;
  }
  else return q[front++];
}

void insert(int pos,int c) //Insert element c into the queue at a given position
{
  int df,dr;
  df=front;
  dr=rear;
  int i;
  for(i=dr;i>=pos;i--)
  {
    q[i+1]=q[i];
  }
  q[pos]=c;
  rear++;
}

void printq() //Print the queue
{
  int i;
  if(front!=-1 && front<=rear)
    for(i=front;i<=rear;i++)
    {
      printf("%d \t",q[i] );
    }
  else printf("Queue empty\n");
  printf("\n" );
}

void sort() //Sort the process according to their arrival time and place it in queue
{
  int i,j,temp;
  for(i=0;i<n;i++)
    add(i);

  for(i=0;i<n;i++)
    for(j=0;j<n-1;j++)
      {
        if(process[q[j]].at>process[q[j+1]].at)
        {
          temp=q[j];
          q[j]=q[j+1];
          q[j+1]=temp;
        }
      }
   pt = total = process[q[front]].at;
}

void display(int pno1) //Display each instance of Gantt chart
{
  printf("-%d |%d| %d- \n",pt,pno1,total);
}

/*
Check if new processes have arrived up until the current total.
If new processes have arrived place them in appropriate position in the queue
*/
void checkForArrivals(int cp)
{
  int ofront=front,orear=rear;
  while(ofront<=orear)
  {
    if(process[q[ofront]].at<=total)
    {
      ofront++;
    }
    else{
      insert(ofront, cp);
      return;
    }
  }
  add(cp);
}

int main()
{
  float atat,awt;
  int ttat=0,twt=0,i,tb=0,cp,tq=2,tat[20],wt[20]; //tq: Time quantum
  printf("Enter the number of processes\n"); //Get the data
  scanf("%d",&n );
  printf("Enter burst time for each process\n");
  for(i=0;i<n;i++)
  {
      scanf("%d",&process[i].bt);
      process[i].no=i;
      tb=tb+process[i].bt;
      process[i].rbt = process[i].bt;
  }
  printf("Enter arrival time for each process\n");
  for(i=0;i<n;i++)
  {
      scanf("%d",&process[i].at);
  }
  printf("\nThe given processes are\n");
  printf("Pno\tBt\tAt\n");
  for(i=0;i<n;i++)
  {
    printf("%d\t%d\t%d\n",process[i].no,process[i].bt,process[i].at);
  }
  printf("\nGantt chart\n");
  sort(); //Sort the processes according to their arrival time in increasing order
  //Logic for round robin
  while(front<=rear)
  {
    cp=rem();
    if(cp!=-1)
    {
      if(process[cp].rbt<=tq && process[cp].rbt>0)
      {
        pt=total;
        total = total + process[cp].rbt;
        process[cp].rbt=0;
        process[cp].ct=total;
        display(cp);
      }
      else if(process[cp].rbt>tq)
      {
        pt=total;
        total=total + tq;
        process[cp].rbt = process[cp].rbt - tq;
        display(cp);
        checkForArrivals(cp);
      }
    }
  }
  // To calucalate the average waiting time and turn around time
  for(i=0;i<n;i++)
  {
    tat[i] = process[i].ct - process[i].at;  //formula for turn around, time completion time - arrival time
    wt[i] = tat[i] - process[i].bt;   // formula for waiting time, turn around time - burst time
    ttat = tat[i] + ttat; //To calculate the the total turn around time
    twt = wt[i] + twt; //To calculate total waiting time
  }
  awt = twt / (float)n; //To calculate average
  atat = ttat / (float)n;
  printf("Average wait time: %f\n",awt);
  printf("Average turn around time: %f\n",atat);
}

Sample output:

Comments

Popular Posts