使用罗宾的闲置情况

发布于 2025-02-06 09:56:15 字数 2585 浏览 2 评论 0原文

当没有准备执行过程的过程时,我在计算正确的平均等待时间和平均周转时间时遇到了麻烦。 [空闲]


闲置情况的例子:
0 3
0 5
9 8
10 6

  • 第一列表示到达时间
  • 第二列表示

当前n过程的爆发时间

平均等待时间应为:3.5
平均周转时间应该是:9

,但我得到的结果是:
平均等待时间:5
平均周转时间:10

根据我的代码解决此问题的任何建议?我知道闲置情况在哪里概述,并且在我的代码中指出。任何帮助都将不胜感激。

在评论空闲的情况下,我正计划从wait_time中减去(保存)变量。由于到达和从(在闲置情况发生)已经完成的到达时间和突发时间。

#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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文