SJF preemptive

PHOTO EMBED

Wed Jun 05 2024 14:19:18 GMT+0000 (Coordinated Universal Time)

Saved by @login

#include <stdio.h>

#define MAX_PROCESSES 100

typedef struct {
    int id;         // Process ID
    int arrival;    // Arrival time
    int burst;      // Burst time
    int remaining;  // Remaining time
    int finish;     // Finish time
    int waiting;    // Waiting time
    int turnaround; // Turnaround time
} Process;

int main() {
    int n;
    Process processes[MAX_PROCESSES];

    printf("Enter number of processes: ");
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        processes[i].id = i + 1;
        printf("Enter arrival time and burst time for process %d: ", i + 1);
        scanf("%d %d", &processes[i].arrival, &processes[i].burst);
    }

    int currentTime = 0, completed = 0, shortest = 0;
    int minRemaining = 10000;
    int finishTime;
    int found = 0;

    // Initialize remaining times
    for (int i = 0; i < n; i++) {
        processes[i].remaining = processes[i].burst;
    }

    while (completed != n) {
        // Find process with minimum remaining time at current time
        for (int i = 0; i < n; i++) {
            if (processes[i].arrival <= currentTime && processes[i].remaining < minRemaining && processes[i].remaining > 0) {
                minRemaining = processes[i].remaining;
                shortest = i;
                found = 1;
            }
        }

        if (!found) {
            currentTime++;
            continue;
        }

        // Decrement remaining time
        processes[shortest].remaining--;

        // Update minimum remaining time
        minRemaining = processes[shortest].remaining;
        if (minRemaining == 0) {
            minRemaining = 10000;
        }

        // If a process gets completely executed
        if (processes[shortest].remaining == 0) {
            completed++;
            found = 0;
            finishTime = currentTime + 1;
            processes[shortest].finish = finishTime;
            processes[shortest].waiting = finishTime - processes[shortest].burst - processes[shortest].arrival;
            if (processes[shortest].waiting < 0) {
                processes[shortest].waiting = 0;
            }
            processes[shortest].turnaround = processes[shortest].waiting + processes[shortest].burst;
        }
        currentTime++;
    }
     float totalWaiting = 0, totalTurnaround = 0;

    printf("\nPID\tArrival\tBurst\tFinish\tWaiting\tTurnaround\n");
    for (int i = 0; i < n; i++) {
        totalWaiting += processes[i].waiting;
        totalTurnaround += processes[i].turnaround;
        printf("%d\t%d\t%d\t%d\t%d\t%d\n",
               processes[i].id,
               processes[i].arrival,
               processes[i].burst,
               processes[i].finish,
               processes[i].waiting,
               processes[i].turnaround);
    }

    printf("\nAverage Waiting Time: %.2f\n", totalWaiting / n);
    printf("Average Turnaround Time: %.2f\n", totalTurnaround / n);

    return 0;
}
content_copyCOPY