简体   繁体   English

调度算法中到达时间大于突发时间时等待时间返回负值

[英]Wait time returns minus value when arrival time is bigger than burst time in scheduling algorithm

I am trying to implement FCFS scheduling algorithm in C and I created time intervals till the user enters another process.我正在尝试在 C 中实现 FCFS 调度算法,并且我创建了时间间隔,直到用户进入另一个进程。 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.因此,例如,当用户等待第二个进程进入时,时间到达假设为 12 秒。 And if the burst time of the process is 8 seconds, waiting time will be -4 seconds which does not make any sense.如果进程的突发时间为 8 秒,则等待时间将为 -4 秒,这没有任何意义。 So, what is the mistake that I make or is there something that I miss?那么,我犯了什么错误或者我错过了什么?

I think you're only interested in wait-time , schedule-time & turn-around-time for each process user inputs.我认为您只对每个流程用户输入的wait-timeschedule-timeturn-around-time感兴趣。 Then perhaps average-wait-time & average-turn-around-time .然后也许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 .用户可能需要几秒钟来输入process-details ,就像你的情况决定继续两次是否继续然后输入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)到达时间(process-#1 为方便起见从 0 开始)
  • entry-order (when arrival-time is same)入场顺序(到达时间相同时)
  • burst-time (time it needs CPU to run its routine)突发时间(需要 CPU 运行其例程的时间)
  • since this is FCFS, no pre-emption & priorities for processes.因为这是 FCFS,所以没有进程的优先权和优先权。

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:下面是一对 C 的实现:

FCFS scheduling works on FIFO principle, just like a queue. FCFS 调度按照 FIFO 原则工作,就像队列一样。 Processes are added to the queue based on arrival time and removed based on position in the queue, waiting time and execution time.进程根据到达时间加入队列,根据队列中position、等待时间和执行时间移除。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM