Write a program to implement CPU Scheduling algorithms like FCFS & SJF
CPU Scheduling algorithms
like FCFS & SJF
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 :

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
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.75The 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.
Log in or create a free account to answer this question.