c sjf

Shortest Job First (SJF) Scheduling Algorithm in C

The Shortest Job First (SJF) scheduling algorithm is a type of process scheduling algorithm that selects the waiting process with the smallest execution time to execute next. Below is an example of how the SJF scheduling algorithm can be implemented in C.

#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[]) {
    wt[0] = 0;
    for (int i = 1; i < n; i++) {
        wt[i] = bt[i - 1] + wt[i - 1];
    }
}

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
    for (int i = 0; i < n; i++) {
        tat[i] = bt[i] + wt[i];
    }
}

void findAverageTime(int processes[], int n, int bt[]) {
    int wt[n], tat[n], total_wt = 0, total_tat = 0;

    findWaitingTime(processes, n, bt, wt);
    findTurnAroundTime(processes, n, bt, wt, tat);

    printf("Processes  Burst time  Waiting time  Turn around time\n");

    for (int i = 0; i < n; i++) {
        total_wt = total_wt + wt[i];
        total_tat = total_tat + tat[i];
        printf(" %d ", (i + 1));
        printf("       %d ", bt[i]);
        printf("       %d", wt[i]);
        printf("       %d\n", tat[i]);
    }

    printf("Average waiting time = %f", (float)total_wt / (float)n);
    printf("\nAverage turn around time = %f", (float)total_tat / (float)n);
}

int main() {
    int processes[] = {1, 2, 3};
    int n = sizeof(processes) / sizeof(processes[0]);
    int burst_time[] = {6, 8, 7};

    findAverageTime(processes, n, burst_time);

    return 0;
}

Explanation:

The given C program demonstrates the implementation of the SJF scheduling algorithm. It calculates the waiting time and turnaround time for each process and then computes the average waiting time and average turnaround time.

The findWaitingTime function calculates the waiting time for each process, and the findTurnAroundTime function calculates the turnaround time for each process. These values are then used to compute the average waiting time and average turnaround time in the findAverageTime function.

The main function initializes the process IDs, burst times, and the number of processes. It then calls the findAverageTime function to calculate and display the average waiting time and average turnaround time for the given processes and burst times.

[[SOURCE 4]]