cschedulinground-robin

Round Robin Scheduling Program


I have been working on a Round Robin Scheduling Program. My inputs are:

Process     Arrival Time    Burst Time
   1            0               4
   2            2               2
   3            4               3
   4            6               5
   5            7               1

Time Slice is 3 units! My output must be:

Process     AT      BT      WT      TT      FT
   1        0       4       9       13      13
   2        2       2       1       3       5
   3        4       3       1       4       8
   4        6       5       4       9       15
   5        7       1       4       5       12

But I am not getting the correct results(WT & FT) for Process 1, 4 & 5. Here's my code, can anyone please help me fix it and get the above results?

#include<stdio.h>
#include<conio.h>
struct proc
{
    int id;
    int arrival;
    int burst;
    int rem;
    int wait;
    int finish;
    int ti;
    int turnaround;
    float ratio;
}process[10];

int no,k;
int chkprocess(int);

void main()
{
 int i,j,t,time = 0,n;
 struct proc temp;
 int nextprocess(int);
 clrscr();
 printf("\n \n Enter the number of processes: ");
 scanf("%d", &n);
 printf("\n \n Enter the time slice of the CPU: ");
 scanf("%d", &t);

 for(i = 1; i <= n; i++)
 {
  process[i].id = i;
  printf("\n\nEnter the arrival time for process %d: ", i);
  scanf("%d", &(process[i].arrival));
  printf("\nEnter the burst time for process %d: ", i);
  scanf("%d", &(process[i].burst));
  process[i].rem = process[i].burst;
  process[i].ti=0;
  process[i].wait=0;
  process[i].finish=0;
 }

 for(i = 1; i <= n; i++)
 {
  for(j = i + 1; j <= n; j++)
  {
   if(process[i].arrival > process[j].arrival)
   {
    temp = process[i];
    process[i] = process[j];
    process[j] = temp;
   }
  }
 }

 no = 0;
 j = 1;

 while(chkprocess(n) == 1)
 {
  if(process[no + 1].arrival == time)
   no++;
  if((process[j].ti<=t)&&(process[j].rem !=0))
  {
   process[j].rem--;
   process[j].ti++;
   for(i = 1; i <= no; i++)
   {
    if((i!=j) && (process[i].rem != 0))
     process[i].wait++;
   }
  }
  if(process[j].rem==0)
   process[j].finish=time;
  if((process[j].ti >= t)||(process[j].rem==0))
  {
   process[j].ti = 0;
   j=nextprocess(j);
  }
  time++;
 }
 process[n].finish = time;
 printf("\n\n Process  Arrival  Burst   Waiting  Finishing turnaround  Tr/Tb \n");
 printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time", "time", "time");
 for(i = 1; i <= n; i++)
 {
  process[i].turnaround = process[i].wait + process[i].burst;
  process[i].ratio = (float)process[i].turnaround / (float)process[i].burst;
  printf("%5d %8d %7d  %8d %10d %9d %10.1f ", process[i].id, process[i].arrival,
                          process[i].burst,
                          process[i].wait, process[i].finish,
                          process[i].turnaround, process[i].ratio);

  printf("\n\n");
 }
 getch();
}

int chkprocess(int s)
{
 int i;
 for(i = 1; i <= s; i++)
 {
  if(process[i].rem != 0)
   return 1;
 }
 return 0;
}

int nextprocess(int k)
{
 int i;
 i=k+1;
 while(chkprocess(i) && i!=k)
 {
  if(process[i].rem != 0)
   return i;
  else
   i=(i+1)%no;
 }
}

Thank You


Solution

  • I'm sure there are many bugs (starting with not caring if the user wants to enter 11 or more processes even though your array of processes is limited to 10).

    However; I spent 10 minutes trying to decipher your code and still don't really know what it thinks it's doing - there's no comments at all and the variable names and function names don't help (e.g. no is not a boolean "yes/no" variable, checkprocess() doesn't check one process but checks all processes to see if all processes have finished, etc). Mostly, if I were being paid to fix this code I'd simple throw it out and rewrite it from scratch to save time. I thought about rewriting it from scratch and just posting the resulting code; but that's not going to help you with your homework.

    My advice is, rewrite it from scratch instead of fixing it.

    It should have a global currently_running_process variable, a global current_time variable, one function to increase the current time, and one function for the scheduler itself.

    The function to increase the current time would:

    The scheduler function should:

    Note: I'd start with current_time = -1; and call the function to increase the current time once before calling the scheduler function; so that any processes with arrival_time == 0 would be added to the scheduler's linked list before the scheduler starts working (and so that the scheduler function doesn't see an empty list as soon as it's started).