forked from aromalsanthosh/System-Softwarwe-Lab-S5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1b_SJF.c
91 lines (79 loc) · 2.05 KB
/
1b_SJF.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
int i, j, n, pId[20];
float tatAvg, wtAvg;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int b[]) {
for (i = 0; i < n - 1; ++i) {
for (j = 0; j < n - 1 - i; ++j) {
if (b[j] > b[j + 1]) {
swap(&b[j], &b[j + 1]);
swap(&pId[j], &pId[j + 1]);
}
}
}
}
void read(int b[]) {
for (i = 0; i < n; ++i) {
printf("Enter the burst time of process %d: ", i);
scanf("%d", &b[i]);
pId[i] = i + 1; /* process 1, process 2,... */
}
sort(b);
}
void findWaitingtime(int b[], int wt[]) {
wt[0] = 0;
int wtSum = 0;
for (i = 1; i < n; ++i) {
wt[i] = wt[i - 1] + b[i - 1];
wtSum += wt[i];
}
wtAvg = (float)wtSum / n;
}
void findTurnAroundtime(int tat[], int b[], int wt[]) {
int tatSum = 0;
for (i = 0; i < n; ++i) {
tat[i] = b[i] + wt[i];
tatSum += tat[i];
}
tatAvg = (float)tatSum / n;
}
void display(int b[], int wt[], int tat[]) {
printf("Process\tBurstTime WaitingTime TurnAroundTime\n");
for (i = 0; i < n; ++i) {
printf("%d\t%d\t\t%d\t%d\n", pId[i], b[i], wt[i], tat[i]);
}
printf("average waiting time: %f", wtAvg);
printf("\naverage turnaround time: %f", tatAvg);
printf("\n");
}
void calcTime(int b[]) {
int wt[20], tat[20];
findWaitingtime(b, wt);
findTurnAroundtime(tat, b, wt);
display(b, wt, tat);
}
void main() {
int b[20];
printf("Number of Processes:");
scanf("%d", &n);
read(b);
calcTime(b);
}
/* OUTPUT
Number of Processes:4
Enter the burst time of process 0: 6
Enter the burst time of process 1: 8
Enter the burst time of process 2: 7
Enter the burst time of process 3: 3
Process BurstTime WaitingTime TurnAroundTime
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24
average waiting time: 7.000000
average turnaround time: 13.000000
*/