sjf.c
55 linesc
DOWNLOAD
1// Aim: Program to perform SJF operations (SJF).
2#include<stdio.h>
3
4void findwt(int n, int bt[], int wt[]) {
5    wt[0] = 0;
6    for (int i = 1; i < n; i++) {
7        wt[i] = bt[i - 1] + wt[i - 1];
8    }
9}
10
11void findtat(int n, int bt[], int wt[], int tat[]) {
12    for (int i = 0; i < n; i++) {
13        tat[i] = bt[i] + wt[i];
14    }
15}
16
17void findavgtime(int n, int bt[]) {
18    int wt[100], tat[100];
19    findwt(n, bt, wt);
20    findtat(n, bt, wt, tat);
21    int total_wt = 0, total_tat = 0;
22    printf("Processes\tBurst Time\tWaiting Time\tTurnaround Time\n");
23    for (int i = 0; i < n; i++) {
24        printf("\n%d\t\t%d\t\t%d\t\t%d", i + 1, bt[i], wt[i], tat[i]);
25        total_wt += wt[i];
26        total_tat += tat[i];
27    }
28    printf("\nAverage waiting time = %f", (float)total_wt / (float)n);
29    printf("\nAverage turnaround time = %f", (float)total_tat / (float)n);
30}
31
32void sort(int n, int bt[]) {
33    for (int i = 0; i < n; i++) {
34        for (int j = 0; j < n - i - 1; j++) {
35            if (bt[j] > bt[j + 1]) {
36                int temp = bt[j];
37                bt[j] = bt[j + 1];
38                bt[j + 1] = temp;
39            }
40        }
41    }
42}
43
44void main(){
45    printf("enter the number of processes :");
46    int n;
47    scanf("%d", &n);
48    int bt[100];
49    printf("enter the burst time of each process :");
50    for (int i = 0; i < n; i++) {
51        scanf("%d", &bt[i]);
52    }
53    sort(n, bt);
54    findavgtime(n, bt);
55}