简体   繁体   中英

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. 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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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