等待时间返回到达时间大于安排算法中的突发时间时的减去值

发布于 2025-01-17 11:39:18 字数 1540 浏览 3 评论 0 原文

我正在尝试在C中实现FCFS调度算法,并创建时间间隔,直到用户输入另一个过程为止。这是以下几行的代码将显示我要问的实际问题。

#include <stdio.h>
#include <time.h>

#define MAX 50

struct ProcessControlBlock{

    int processID;
    int burstTime;
    int arrivalTime; 
    int turnAround;

};

void main(){

    int terminateController=1;
    int inputCheck;
    int processIndex = 0;
    struct ProcessControlBlock processControlBlocks[MAX];
    time_t beginTime = time(NULL);
    while(terminateController!=0){


        printf("If you want to enter a process, please press 1: ");
        scanf("%d",&inputCheck);

        time_t endTime = time(NULL);
        if(inputCheck==1){
            printf("The arrival time of the process is %d\n",(endTime-beginTime));
            processControlBlocks[processIndex].processID = processIndex;
            processControlBlocks[processIndex].arrivalTime = endTime-beginTime;
            printf("Process ID is: %d\n", processControlBlocks[processIndex].processID);
            printf("Enter the burst time of the process: ");
            scanf("%d",&processControlBlocks[processIndex].burstTime);
            printf("Waiting time of the process is: %d\n",(processControlBlocks[processIndex].burstTime-processControlBlocks[processIndex].arrivalTime));
        }

        printf("If you want to continue to add process, please press 1: ");
        scanf("%d",&terminateController);
        processIndex++;
    }

}

因此,例如,当用户等待第二个进程输入时,时间将是12秒。而且,如果过程的突发时间为8秒,等待时间将为-4秒,这没有任何意义。那么,我犯的错误是什么,或者我想念的东西是什么?

I am trying to implement FCFS scheduling algorithm in C and I created time intervals till the user enters another process. Here is the code following lines will show the actual question I want to ask.

#include <stdio.h>
#include <time.h>

#define MAX 50

struct ProcessControlBlock{

    int processID;
    int burstTime;
    int arrivalTime; 
    int turnAround;

};

void main(){

    int terminateController=1;
    int inputCheck;
    int processIndex = 0;
    struct ProcessControlBlock processControlBlocks[MAX];
    time_t beginTime = time(NULL);
    while(terminateController!=0){


        printf("If you want to enter a process, please press 1: ");
        scanf("%d",&inputCheck);

        time_t endTime = time(NULL);
        if(inputCheck==1){
            printf("The arrival time of the process is %d\n",(endTime-beginTime));
            processControlBlocks[processIndex].processID = processIndex;
            processControlBlocks[processIndex].arrivalTime = endTime-beginTime;
            printf("Process ID is: %d\n", processControlBlocks[processIndex].processID);
            printf("Enter the burst time of the process: ");
            scanf("%d",&processControlBlocks[processIndex].burstTime);
            printf("Waiting time of the process is: %d\n",(processControlBlocks[processIndex].burstTime-processControlBlocks[processIndex].arrivalTime));
        }

        printf("If you want to continue to add process, please press 1: ");
        scanf("%d",&terminateController);
        processIndex++;
    }

}

So, for example, when the user waits for the second process entering, the time arrival will be let's say 12 seconds. And if the burst time of the process is 8 seconds, waiting time will be -4 seconds which does not make any sense. So, what is the mistake that I make or is there something that I miss?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2025-01-24 11:39:18

我认为您只对等待时间计划时间&amp; turn-around time 对于每个进程用户输入。然后也许平均等待时间&amp; 平均转移时间
用户可能需要几秒钟才能输入 Process-details ,就像在您的情况下决定继续两次是否继续,然后输入爆发时间

您可以实现实时仿真,但是跟踪&amp;验证这些统计数据变得乏味。相反,您为一组流程准备测试数据&amp;带有预期结果的计数。因此,我们需要事先进行详细信息:

  • Process-ID
  • 到达时间(process-#1从0开始)
  • 入门订单(到达时间相同时)
  • 爆发时间(时间需要CPU才能运行例程),
  • 因为这是FCF,没有预先抢先&amp;流程的优先级。

然后,您按(到达时间&amp;入门订单)对列表进行排序,并处理每个过程计算的列表:

  • 等待时间
  • 时间表的时间表
  • 上周转时间的
  • 平均等待时间列表的
  • 平均转换为单位- 列表的时间,

您将其与其他调度算法进行比较,该算法对流程的公平程度。

以下是c实施夫妇:

I think you're only interested in wait-time, schedule-time & turn-around-time for each process user inputs. Then perhaps average-wait-time & average-turn-around-time.
User may take several seconds to input process-details, like in your case deciding to continue twice whether to continue and then enter a burst-time.

You can implement real-time simulation, but tracing & validating those statistics becomes tedious. Instead you prepare test-data for set of processes & tally with expected results. So details we need beforehand:

  • process-id
  • arrival-time (process-#1 begins at 0 for convenience)
  • entry-order (when arrival-time is same)
  • burst-time (time it needs CPU to run its routine)
  • since this is FCFS, no pre-emption & priorities for processes.

Then you sort the list by (arrival-time & entry-order), and process the list calculating for each process:

  • wait-time
  • schedule-time
  • turn-around-time
  • average-wait-time for the list
  • average-turn-around-time for the list

You compare this with other scheduling algorithms, how fair it is for the processes.

Here are couple C implementation :

世界和平 2025-01-24 11:39:18

FCFS调度按照先进先出原则工作,就像队列一样。进程根据到达时间添加到队列中,并根据在队列中的位置、等待时间和执行时间删除。等待时间取决于队列中前一个进程的退出时间(如果有),要计算它,您必须跟踪队列中所有进程的到达时间、执行时间和等待时间。

例子:

Timer started at 00:00:00
_____
1/ process 1 arrived at 00:00:03 | exec time 10s | waiting time  0s | exit 00:00:13
2/ process 2 arrived at 00:00:05 | exec time  8s | waiting time  8s | exit 00:00:21
3/ process 3 arrived at 00:00:08 | exec time  5s | waiting time 13s | exit 00:00:26

FCFS scheduling works on FIFO principle, just like a queue. Processes are added to the queue based on arrival time and removed based on position in the queue, waiting time and execution time. Waiting time depends on the exit time of the previous process in the queue (if any), to calculate it you have to keep track of arrival time, execution time and waiting time of all the processes in the queue.

Example:

Timer started at 00:00:00
_____
1/ process 1 arrived at 00:00:03 | exec time 10s | waiting time  0s | exit 00:00:13
2/ process 2 arrived at 00:00:05 | exec time  8s | waiting time  8s | exit 00:00:21
3/ process 3 arrived at 00:00:08 | exec time  5s | waiting time 13s | exit 00:00:26
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文