Skip to content
doubtly

2 answers

Best answer

FCFS

#include <stdio.h>

int main() {
    int n, i;
    float avg_waiting_time = 0, avg_turnaround_time = 0;
    printf("Enter the number of processes: ");
    scanf("%d", &n);

    int at[n], bt[n], wt[n], tat[n];
    printf("Enter the arrival time and burst time of each process:\n");
    for(i = 0; i < n; i++) {
        printf("Process %d: ", i+1);
        scanf("%d %d", &at[i], &bt[i]);
    }

    wt[0] = 0;
    tat[0] = bt[0];
    for(i = 1; i < n; i++) {
        wt[i] = tat[i-1] - at[i];
        if(wt[i] < 0)
            wt[i] = 0;
        tat[i] = bt[i] + wt[i];
    }

    printf("\nFCFS Scheduling:\n");
    printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
    for(i = 0; i < n; i++) {
        printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", i+1, at[i], bt[i], wt[i], tat[i]);
        avg_waiting_time += wt[i];
        avg_turnaround_time += tat[i];
    }
    printf("Average waiting time = %.2f\n", avg_waiting_time/n);
    printf("Average turnaround time = %.2f\n", avg_turnaround_time/n);

    return 0;
}

Output :: 

 

SJF

#include <stdio.h>

int main() {
    int n, i, j, temp;
    float avg_waiting_time = 0, avg_turnaround_time = 0;
    printf("Enter the number of processes: ");
    scanf("%d", &n);

    int at[n], bt[n], wt[n], tat[n];
    printf("Enter the arrival time and burst time of each process:\n");
    for(i = 0; i < n; i++)
        scanf("%d %d", &at[i], &bt[i]);

    for(i = 0; i < n-1; i++) {
        for(j = i+1; j < n; j++) {
            if(bt[i] > bt[j]) {
                temp = bt[i];
                bt[i] = bt[j];
                bt[j] = temp;

                temp = at[i];
                at[i] = at[j];
                at[j] = temp;
            }
        }
    }

    wt[0] = 0;
    tat[0] = bt[0];
    for(i = 1; i < n; i++) {
        wt[i] = tat[i-1] - at[i];
        if(wt[i] < 0)
            wt[i] = 0;
        tat[i] = bt[i] + wt[i];
    }

    printf("\nSJF Scheduling:\n");
    printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
    for(i = 0; i < n; i++) {
        printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", i+1, at[i], bt[i], wt[i], tat[i]);
        avg_waiting_time += wt[i];
        avg_turnaround_time += tat[i];
    }
    printf("Average waiting time = %.2f\n", avg_waiting_time/n);
    printf("Average turnaround time = %.2f\n", avg_turnaround_time/n);

    return 0;
}

Output :

Doubtly · 23 Apr 2023 Report

Answer: A C program that reads the arrival and burst times and computes completion, turnaround and waiting times for FCFS and for non-preemptive SJF.

#include <stdio.h>
#define MAX 20

void show(int n, int at[], int bt[], int ct[]) {
    float total_tat = 0, total_wt = 0;
    printf("P\tAT\tBT\tCT\tTAT\tWT\n");
    for (int i = 0; i < n; i++) {
        int tat = ct[i] - at[i];      /* turnaround = completion - arrival */
        int wt = tat - bt[i];         /* waiting = turnaround - burst */
        total_tat += tat;
        total_wt += wt;
        printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat, wt);
    }
    printf("Average TAT = %.2f\nAverage WT  = %.2f\n", total_tat / n, total_wt / n);
}

void fcfs(int n, int at[], int bt[]) {
    int ct[MAX], done[MAX] = {0}, time = 0;
    for (int k = 0; k < n; k++) {
        int idx = -1;                 /* earliest arrival not yet run */
        for (int i = 0; i < n; i++)
            if (!done[i] && (idx == -1 || at[i] < at[idx]))
                idx = i;
        if (time < at[idx])
            time = at[idx];           /* CPU idle until it arrives */
        time += bt[idx];
        ct[idx] = time;
        done[idx] = 1;
    }
    printf("\n--- FCFS ---\n");
    show(n, at, bt, ct);
}

void sjf(int n, int at[], int bt[]) {
    int ct[MAX], done[MAX] = {0}, time = 0, finished = 0;
    while (finished < n) {
        int idx = -1;                 /* shortest job that has arrived */
        for (int i = 0; i < n; i++)
            if (!done[i] && at[i] <= time && (idx == -1 || bt[i] < bt[idx]))
                idx = i;
        if (idx == -1) {              /* nothing has arrived yet */
            time++;
            continue;
        }
        time += bt[idx];
        ct[idx] = time;
        done[idx] = 1;
        finished++;
    }
    printf("\n--- SJF (non-preemptive) ---\n");
    show(n, at, bt, ct);
}

int main(void) {
    int n, at[MAX], bt[MAX];
    printf("Enter number of processes: ");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        printf("Arrival time and burst time of P%d: ", i + 1);
        scanf("%d %d", &at[i], &bt[i]);
    }
    fcfs(n, at, bt);
    sjf(n, at, bt);
    return 0;
}

Logic

  • FCFS: processes run in order of arrival. If the CPU is free before the next process arrives, the clock jumps to its arrival time.
  • SJF (non-preemptive): at each decision point, choose the process with the smallest burst time among those that have already arrived. It then runs to completion.
  • For every process: Turnaround time = Completion time − Arrival time, and Waiting time = Turnaround time − Burst time.

Sample run (AT, BT): P1 (0, 8), P2 (1, 4), P3 (2, 9), P4 (3, 5)

--- FCFS ---
P   AT  BT  CT  TAT  WT
P1  0   8   8   8    0
P2  1   4   12  11   7
P3  2   9   21  19   10
P4  3   5   26  23   18
Average TAT = 15.25
Average WT  = 8.75

--- SJF (non-preemptive) ---
P1  0   8   8   8    0
P2  1   4   12  11   7
P3  2   9   26  24   15
P4  3   5   17  14   9
Average TAT = 14.25
Average WT  = 7.75

The earlier answer on this page calculates waiting time as tat[i-1] - at[i], which uses the previous process's turnaround time in place of its completion time. Its SJF version also sorts by burst time without checking arrival times, so both give wrong values whenever processes arrive at different times.

Tutor AI · 23 Oct 2026 AI-verified AI-generated Report

Your answer

Log in or create a free account to answer this question.