0

I am having trouble with calculating the correct Average wait time and Average Turnaround time when there are no processes ready to be executed. [ IDLE ]


Example of IDLE situation:
0 3
0 5
9 8
10 6

  • The first column represents Arrival Time
  • The second column represents Burst Time


for the current n process

The average wait time should be: 3.5
The average turnaround time should be: 9

But the results I get are:
The average wait time: 5
The average turnaround time: 10

Any suggestions of what I should do to fix this problem, based on my code? I know where the IDLE situation outlies and it's noted in my code. Any help would be greatly appreciated..

Where IDLE is commented, I was planning on subtracting the (save) variable from the wait_time. Since the arrival and burst time from ( before the IDLE situation occured) already completed.

#include <stdio.h>
int main()
{
   int i, total = 0, x, limit, counter = 0, t_quantum;
   int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];

   float average_wait_time, average_turnaround_time;

   printf("\nEnter Total Number of Processes: ");
   scanf("%d", &limit);
   x = limit;

   for (i = 0; i < limit; i++)
   {
      printf("\nProvide the details for Process[%d]\n", i + 1);
      printf("Arrival Time:\t");
      scanf("%d", &arrival_time[i]);
      printf("Burst Time:\t");
      scanf("%d", &burst_time[i]);
      temp[i] = burst_time[i];
   }

   printf("\nEnter Time Quantum:\t");
   scanf("%d", &t_quantum);
        int save = 0;
   printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
   for (total = 0, i = 0; x != 0;)
   {
      if (temp[i] <= t_quantum && temp[i] > 0)
      {
         total = total + temp[i];
         temp[i] = 0;
         counter = 1;
      }
      else if (temp[i] > 0)
      {
         temp[i] = temp[i] - t_quantum;
         total = total + t_quantum;
      }

      if (temp[i] == 0 && counter == 1)
      {
         x--;
         printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]);

        // printf("Completion TIme: %d\n", total);
         wait_time = wait_time + total - arrival_time[i] - burst_time[i];
         save = total - arrival_time[i] - burst_time[i];
         turnaround_time = turnaround_time + total - arrival_time[i];
        // printf("CT: %d\n", turnaround_time);
         counter = 0;
      }

      if (i == limit - 1)
      {
         i = 0;
      }
      else if (arrival_time[i + 1] <= total)
      {
         i++;
      }
      else
      {
         // IDLE when temp[i] == 0
        // limit +=1;
         i++;
        
      }
   }

   average_wait_time = wait_time *1.0 / limit;
   average_turnaround_time = turnaround_time *1.0 / limit;
 

   printf("\n\nAverage Waiting Time:\t%f", average_wait_time);
   printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);

   return 0;
}
markio
  • 11
  • 2
  • You have not described what the code is supposed to do. For example, you have not even told us what the input numbers represent exactly. You have just launched straight into the incorrect behaviour without giving us full context. Please provide that info. – kaylum Jun 11 '22 at 01:33
  • And what did you do when you found the code produces the wrong result? The correct next step is to actually debug it yourself first. Run your program in a debugger and trace its flow and variable values as it runs to find out where things first start to go wrong. Then update the question with what you find. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – kaylum Jun 11 '22 at 01:34

0 Answers0