Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 5: CPU scheduling algorithms: FCFS, SJF, Priority

Theory link: 2.4 Scheduling I

Experiment 5 of 15 Module 2 2 hours

Video walkthrough

Lab 5 walkthrough — CPU scheduling algorithms: FCFS, SJF, Priority

Video coming soon

Lab 5 walkthrough — CPU scheduling algorithms: FCFS, SJF, Priority

Recorded by Dr. Krithika L.B. Slot lab05-walkthrough

Aim

Aim. To implement the non-preemptive FCFS, SJF and Priority scheduling algorithms and compute completion, turnaround and waiting times.

Explanation

All three algorithms share one loop: at the current time t, look at processes that have arrived and not finished, pick one by the algorithm's rule, run it to completion (non-preemptive), and advance t by its burst.

FCFS rule

Earliest arrival time.

SJF rule

Smallest burst; ties by arrival.

Priority rule

Smallest priority number; ties by arrival.

Formulas

TAT = CT − AT · WT = TAT − BT

If no process has arrived yet, the CPU is idle and t advances by 1. Check every answer with the simulator in Session 2.4.

Code

sched_np.c
/* sched_np.c - non-preemptive CPU scheduling: FCFS, SJF, Priority (BACSE106 Lab 5) */
#include <stdio.h>

int main(void)
{
    int n, choice, i, t = 0, done = 0;
    int at[20], bt[20], pr[20], ct[20], fin[20] = {0};
    float twt = 0, ttat = 0;

    printf("Enter number of processes: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("P%d arrival burst priority: ", i + 1);
        scanf("%d %d %d", &at[i], &bt[i], &pr[i]);
    }
    printf("1.FCFS  2.SJF  3.Priority  Choice: ");
    scanf("%d", &choice);

    printf("\nGantt chart:\n");
    while (done < n) {
        int k = -1;
        for (i = 0; i < n; i++) {
            if (fin[i] || at[i] > t) continue;          /* finished or not arrived */
            if (k == -1) { k = i; continue; }
            if (choice == 1 && at[i] < at[k]) k = i;                         /* FCFS     */
            if (choice == 2 && (bt[i] < bt[k] || (bt[i] == bt[k] && at[i] < at[k]))) k = i; /* SJF */
            if (choice == 3 && (pr[i] < pr[k] || (pr[i] == pr[k] && at[i] < at[k]))) k = i; /* Priority (low number = high) */
        }
        if (k == -1) { t++; continue; }                 /* CPU idle */
        printf("| %d P%d %d ", t, k + 1, t + bt[k]);
        t += bt[k];
        ct[k] = t;
        fin[k] = 1;
        done++;
    }
    printf("|\n\nProcess  AT  BT  PR  CT  TAT  WT\n");
    for (i = 0; i < n; i++) {
        int tat = ct[i] - at[i], wt = tat - bt[i];
        ttat += tat;
        twt += wt;
        printf("P%-7d %-3d %-3d %-3d %-3d %-4d %d\n", i + 1, at[i], bt[i], pr[i], ct[i], tat, wt);
    }
    printf("Average TAT = %.2f\nAverage WT  = %.2f\n", ttat / n, twt / n);
    return 0;
}

How to run

  1. gcc sched_np.c -o sched_np
  2. ./sched_np
  3. Enter the number of processes, then arrival, burst and priority for each (priority is ignored by FCFS/SJF), then the algorithm number.

Sample input & output

FCFS (choice 1)

Input
3 processes: 0 24 1 · 0 3 1 · 0 3 1
Output
Output
Gantt chart:
| 0 P1 24 | 24 P2 27 | 27 P3 30 |

Process  AT  BT  PR  CT  TAT  WT
P1       0   24  1   24  24   0
P2       0   3   1   27  27   24
P3       0   3   1   30  30   27
Average TAT = 27.00
Average WT  = 17.00

SJF (choice 2)

Input
4 processes: 0 6 1 · 0 8 1 · 0 7 1 · 0 3 1
Output
Output
Gantt chart:
| 0 P4 3 | 3 P1 9 | 9 P3 16 | 16 P2 24 |

Process  AT  BT  PR  CT  TAT  WT
P1       0   6   1   9   9    3
P2       0   8   1   24  24   16
P3       0   7   1   16  16   9
P4       0   3   1   3   3    0
Average TAT = 13.00
Average WT  = 7.00

Priority (choice 3)

Input
5 processes: 0 10 3 · 0 1 1 · 0 2 4 · 0 1 5 · 0 5 2
Output
Output
Gantt chart:
| 0 P2 1 | 1 P5 6 | 6 P1 16 | 16 P3 18 | 18 P4 19 |

Process  AT  BT  PR  CT  TAT  WT
P1       0   10  3   16  16   6
P2       0   1   1   1   1    0
P3       0   2   4   18  18   16
P4       0   1   5   19  19   18
P5       0   5   2   6   6    1
Average TAT = 12.00
Average WT  = 8.20

Try it yourself

  1. Add response time (RT) to the table. For non-preemptive algorithms, how does RT relate to WT?
  2. Run FCFS with P1 arriving at 2 and P2 at 5 to see idle time handled.
  3. Change the priority rule so a larger number means higher priority.
  4. Add aging: every 5 time units of waiting, decrease a process's priority number by 1.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
Which algorithm is optimal for average waiting time?

SJF (among non-preemptive algorithms, for a given set of processes).

What is the convoy effect?

Short processes waiting behind a long one under FCFS.

How is starvation possible in priority scheduling?

A low-priority process may never be chosen while higher-priority processes keep arriving; aging fixes it.

Why does the program advance t by 1 when no process is ready?

To model CPU idle time until the next arrival.