Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
845 views
in Technique[技术] by (71.8m points)

job scheduling - How to apply the algorithm of preemptive shortest job first in C

I am trying to implement different OS scheduling algorithms in C. I have already implemented FCFS and non-preemptive shortest job first scheduling, however, I am having a hard time with regards to the loop that will occur for the preemptive SJF. Here is a sample of my text file.

Process Arrival Burst
1      0      7
2      2      4
3      4      1
4      5      4

Through solving, the average waiting time that I got is 4, however, I cant seem to get the algorithm properly.

here is a sample of my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 256

//Create 2 structures
//XYZ: X = ALGO; Y = NUM OF PROCESSES; Z = FOR RR
//ABC: A = PID; B = ARRIVAL TIME; C = EXEC TIME
struct XYZ {
    int algo, numProcesses, timeSlice;
};

struct ABC {
    int pid, arrivalTime, execTime, endTime, waitTime, startTime;
};

int main(){
    /*
    ALGO DEFINER:
    0 = FCFS ; 1 = NON PREEMPTIVE; 2 = PREEMPTIVE ; 3 = RR
    */
    char filename[255];
    struct XYZ scheduler;
    struct ABC process[255], temp;
    int i, j, k;
    float average = 0.0, totalSum = 0.0;
    FILE *filepointer;
    int num = 0; //initialize to 0, if not, num = num+1 will start with 1

    printf("Enter the name/path of the file: ");
    scanf("%s", filename);

    //If filename does not exist
    filepointer = fopen(filename, "r");
    if(filepointer == NULL){
        printf("Filename %s does not exist
", filename);
        exit(1);
    }

    //If filename exists, continue
    //Checking of file contents first
    char buffer[LEN];
    i = 0;
    printf("File contents: 
");
    //scan the file
    while(fgets(buffer, LEN - 1, filepointer) != NULL){
        sscanf(buffer, "%d %d %d", &process[i].pid, &process[i].arrivalTime, &process[i].execTime);
        printf("%d %d %d
", process[i].pid, process[i].arrivalTime, process[i].execTime);
        i = i+1;
        num = num+1; //store the number of lines, in my test is 10
    }

    //Store the first line as the XYZ structure
    scheduler.algo = process[0].pid;
    scheduler.numProcesses = process[0].arrivalTime;
    scheduler.timeSlice = process[0].execTime;

    //Preemptive SJF scheduling
    //Sort the arrival time of each process
    for(i = 1; i < num; i++){
        for(j = 1; j < num-i; j++){
            if(process[j].arrivalTime > process[j+1].arrivalTime){
                temp = process[j];
                process[j] = process[j+1];
                process[j+1] = temp;
            }
        }
    }
    printf("Arrangement
");
    for(i = 0; i < num; i++){
        printf("%d %d %d
", process[i].pid, process[i].arrivalTime, process[i].execTime);
    }

    //Run the algo on the sorted arrival time
    //Scan the whole dataset starting from the 1st line, line 0 is the indicator
    for(i = 1; i < num; i++){
        //insert code here for preemptive
    }







    return 0;

}
question from:https://stackoverflow.com/questions/65886966/how-to-apply-the-algorithm-of-preemptive-shortest-job-first-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...