-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex2.c
87 lines (66 loc) · 1.54 KB
/
ex2.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
#include <stdio.h>
struct process{
int AT;
int BT;
int CT;
int WT;
int TAT;
};
void swap(struct process* a, struct process* b){
struct process t = *a;
*a = *b;
*b = t;
}
void sort(struct process *list, int size){
//bubble sort
for (int i=0; i<size-1; i++){
for (int k=0; k<size-i-1;k++){
if (list[k].AT>list[k+1].AT)
swap(&list[k], &list[k+1]);
else
if (list[k].AT==list[k+1].AT){
if (list[k].BT>list[k+1].BT)
swap(&list[k], &list[k+1]);
}
}
}
}
void sjn(struct process *list, int size){
sort(list, size);
int clock = 1;
int index = 0;
while (index != size){
if (list[index].AT<=clock){
list[index].CT = clock + list[index].BT;
list[index].TAT = list[index].CT - list[index].AT;
list[index].WT = clock - list[index].AT;
clock = list[index].CT;
index++;
} else clock++;
}
}
int main(void) {
FILE *fp;
fp = fopen("new_input.csv", "r");
struct process list[10];
char buffer[100];
int a = 0, d = 0;
int i=0;
while (fscanf(fp, "%d,%d", &a, &d)!=EOF){
struct process p = {a, d};
list[i] = p;
i++;
}
sjn(list, i);
printf("P # AT BT CT TAT WT\n\n");
double att = 0;
double awt = 0;
for(int k = 0; k<i; k++) {
printf("P%2d %6d %6d %6d %6d %6d\n", k, list[k].AT, list[k].BT, list[k].CT, list[k].TAT, list[k].WT);
att+=list[k].TAT;
awt+=list[k].WT;
}
printf("\nAverage Turnaround Time: %f\n", att/i);
printf("Average WT: %f\n", awt/i);
return 0;
}