In Preemptive Shortest Job First Scheduling, jobs are put into ready queue as they arrive, but as a process with short burst time arrives, the existing process is preempted or removed from execution, and the shorter job is executed first.
#include<stdio.h>
struct shortestJobFirstType {
int bt, at, wt, st, pno, tt, cbt;
};
int get(struct shortestJobFirstType arr[], int t, int n) {
int imin, min = 9999, i;
for (i = 0; i < n; i++) {
if (arr[i].at <= t && arr[i].st == 0)
if (min > arr[i].bt) {
min = arr[i].bt;
imin = i;
}
}
return imin;
}
void gantt_chart(struct shortestJobFirstType arr[], int p[], int n, int nop) {
int i, a[100], s = 0;
float avgtt = 0, avgwt = 0;
printf("***************************************\n");
printf("GANTT CHART\n");
printf("0");
for (i = 0; i < n - 1; i++) {
while (i < n - 1 && p[i] == p[i + 1]) {
s++;
i++;
}
s++;
printf(" -> [P%d] <- %d", arr[p[i]].pno, s);
arr[p[i]].wt = s - arr[p[i]].at - arr[p[i]].tt;
}
for (i = 0; i < nop; i++) {
arr[i].tt += arr[i].wt;
avgwt += arr[i].wt;
avgtt += arr[i].tt;
}
printf("\n***************************************\n");
printf("Pro\tArTi\tBuTi\tTaTi\tWtTi\n");
printf("***************************************\n");
for (i = 0; i < nop; i++) {
printf("[P%d]\t%d\t%d\t%d\t%d\n", arr[i].pno, arr[i].at, arr[i].cbt,
arr[i].tt, arr[i].wt);
}
printf("***************************************\n");
avgwt = avgwt / nop;
avgtt = avgtt / nop;
printf("Average Waiting Time : %.2f\n", avgwt);
printf("Average Turn around Time : %.2f\n", avgtt);
return;
}
int iscomplite(struct shortestJobFirstType arr[], int n) {
int i;
for (i = 0; i < n; i++)
if (arr[i].st == 0)
return 0;
return 1;
}
int main() {
int n, i, a, t = 0;
int p[100];
float avgwt = 0, avgtt = 0;
struct shortestJobFirstType arr[100];
printf("SJF (Shortest Job First) - Preemptive\n");
printf(
"Note -\n1. Arrival Time of at least on process should be 0\n2. CPU should never be idle\n");
printf("Enter Number of Processes\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter Arrival Time & Burst Time for Process [P%d]\n", i);
scanf("%d%d", &arr[i].at, &arr[i].bt);
arr[i].pno = i;
arr[i].cbt = arr[i].bt;
arr[i].st = 0;
arr[i].tt = arr[i].bt;
arr[i].wt = 0;
}
i = 0;
while (1) {
if (iscomplite(arr, n))
break;
a = get(arr, t, n);
p[i] = a;
arr[a].bt -= 1;
if (arr[a].bt == 0)
arr[a].st = 1;
t = t + 1;
i++;
}
gantt_chart(arr, p, i, n);
return 0;
}
SJF (Shortest Job First) - Preemptive
Note -
1. Arrival Time of at least on process should be 0
2. CPU should never be idle
Enter Number of Processes
4
Enter Arrival Time & Burst Time for Process [P0]
0 8
Enter Arrival Time & Burst Time for Process [P1]
1 4
Enter Arrival Time & Burst Time for Process [P2]
2 9
Enter Arrival Time & Burst Time for Process [P3]
3 5
***************************************
GANTT CHART
0 -> [P0] <- 1 -> [P1] <- 5 -> [P3] <- 10 -> [P0] <- 17 -> [P2] <- 26
***************************************
Pro ArTi BuTi TaTi WtTi
***************************************
[P0] 0 8 17 9
[P1] 1 4 4 0
[P2] 2 9 24 15
[P3] 3 5 7 2
***************************************
Average Waiting Time : 6.50
Average Turnaround Time : 13.00