-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_func.c
265 lines (213 loc) · 6.27 KB
/
helper_func.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* OS Scheduling Policy Simulator - Accessory Functions
* Author: Ana Berthel
* Date: 12/13/18
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "helper.h"
/* Returns true if the given value is in the array, false otherwise */
int isInArray(int x, int* arr, int until) {
for(int i=0; i<until; i++) {
if(x == arr[i]) {
return FALSE;
}
}
return TRUE;
}
/* Returns a pessimistic estimate of the time required to run the processes in the list.
* Estimate is the sum of all CPU times and IO times needed by all processes.
*/
int estimate_time(struct process *p[], int num_processes) {
int time_estimate = 0;
for(int i=0; i<num_processes; i++) {
time_estimate += p[i]->CPU_time;
time_estimate += p[i]->num_io * io_time;
}
return time_estimate;
}
/* Sorts array of process pointers in order of increasing enter time */
void selection_sort(struct process *p[], int num_processes) {
struct process *temp;
int min;
for(int i=0; i<num_processes; i++) {
min=i;
for(int j=i+1; j<num_processes; j++) {
if(p[j]->enter_time < p[min]->enter_time) {
min=j;
}
}
//swap index i and min
if(i != min) {
temp = p[i];
p[i] = p[min];
p[min] = temp;
}
}
}
/* writes the provided array to a .csv file */
void print_array_to_file(int* array[], int l, char* file_name, int num_processes) {
FILE *file;
file = fopen(file_name, "w");
for(int i=0; i<num_processes; i++) {
for(int j=0; j<l; j++) {
fprintf(file, "%d,", array[i][j]);
}
fprintf(file, "\n");
}
fclose(file);
}
/* Calculates average metrics for the finished simulation */
void calculate_metrics(int* results[], struct process *p[], int length, int num_processes) {
int tt[num_processes]; //turnaround time
int rt[num_processes]; //response time
int wt[num_processes]; //wait time
//for averages
int avg_t = 0;
int avg_r = 0;
int avg_w = 0;
for(int i=0; i<num_processes; i++) {
int enter = -1; //time at which the process enters the system
int first_run = -1; //time at which the process is first run
int exit = -1; //time at which the process exits the system
for(int j=0; j<length; j++) {
if(enter==-1 && results[p[i]->id][j]>0) {
enter = j;
}
if(first_run==-1 && results[p[i]->id][j] == 3) {
first_run = j;
}
if(exit==-1 && results[p[i]->id][j]==4) {
exit = j;
break;
}
}
//handles if process is last to run
if(exit == -1) {
exit = length;
}
tt[p[i]->id] = exit-enter;
rt[p[i]->id] = first_run-enter;
wt[p[i]->id] = (exit-enter)-p[i]->CPU_time -(io_time*p[i]->num_io);
avg_t += tt[p[i]->id];
avg_r += rt[p[i]->id];
avg_w += wt[p[i]->id];
}
//calculate averages
double avg_t1 = ((double) avg_t)/num_processes;
double avg_r1 = ((double) avg_r)/num_processes;
double avg_w1 = ((double) avg_w)/num_processes;
printf("Metrics: average\n");
printf("Turnaround time: %lf\n", avg_t1);
printf("Wait time: %lf\n", avg_w1);
printf("Response time: %lf\n\n", avg_r1);
}
/* Calculates average metrics for the finished simulation for two groups of processes */
void calculate_metrics_groups(int* results[], struct process *p[], int length, int num_processes, int sep) {
int tt[num_processes]; //turnaround time
int rt[num_processes]; //response time
int wt[num_processes]; //wait time
//for averages group 1
int avg_t_1 = 0;
int avg_r_1 = 0;
int avg_w_1 = 0;
//for averages group 2
int avg_t_2 = 0;
int avg_r_2 = 0;
int avg_w_2 = 0;
for(int i=0; i<num_processes; i++) {
int enter = -1; //time at which the process enters the system
int first_run = -1; //time at which the process is first run
int exit = -1; //time at which the process exits the system
for(int j=0; j<length; j++) {
if(enter==-1 && results[p[i]->id][j]>0) {
enter = j;
}
if(first_run==-1 && results[p[i]->id][j] == 3) {
first_run = j;
}
if(exit==-1 && results[p[i]->id][j]==4) {
exit = j;
break;
}
}
//handles if process is last to run
if(exit == -1) {
exit = length;
}
tt[p[i]->id] = exit-enter;
rt[p[i]->id] = first_run-enter;
wt[p[i]->id] = (exit-enter)-p[i]->CPU_time;
if(p[i]->id < sep) {
avg_t_1 += tt[p[i]->id];
avg_r_1 += rt[p[i]->id];
avg_w_1 += wt[p[i]->id];
} else {
avg_t_2 += tt[p[i]->id];
avg_r_2 += rt[p[i]->id];
avg_w_2 += wt[p[i]->id];
}
}
//calculate averages
double avg_t1 = ((double) avg_t_1)/sep;
double avg_r1 = ((double) avg_r_1)/sep;
double avg_w1 = ((double) avg_w_1)/sep;
double avg_t2 = ((double) avg_t_2)/(num_processes-sep);
double avg_r2 = ((double) avg_r_2)/(num_processes-sep);
double avg_w2 = ((double) avg_w_2)/(num_processes-sep);
printf("Metrics: average group 1\n");
printf("Turnaround time: %lf\n", avg_t1);
printf("Wait time: %lf\n", avg_w1);
printf("Response time: %lf\n\n", avg_r1);
printf("Metrics: average group 2\n");
printf("Turnaround time: %lf\n", avg_t2);
printf("Wait time: %lf\n", avg_w2);
printf("Response time: %lf\n\n", avg_r2);
}
/* checks to make sure that the first character of an input is a digit */
void check_digit(char* s) {
if(isdigit(s[0]) == 0) {
printf("Error: expected positive number \n");
exit(1);
}
}
/* checks to make sure that a string input has been successfully converted to an integer */
void check_arg(int t) {
if(t <= 0) {
printf("Error: invalid argument input\n");
exit(1);
}
}
/* Prints basic information about each process in list */
void print_process_status(struct process *p[], int np) {
for(int i=0; i<np; i++) {
printf("Process %d:\n", p[i]->id);
printf("Enters system at %d\n", p[i]->enter_time);
printf("Total CPU time: %d\n", p[i]->CPU_time);
printf("Number of tickets: %d\n", p[i]->tix);
printf("I/O request times: ");
for(int j=0; j<p[i]->num_io; j++) {
printf("%d ", p[i]->io_times[j]);
}
printf("\n\n\n");
}
}
/* Returns true if the given process makes an IO request at this time point */
int is_io_time(struct process *x) {
for(int i=0; i<x->num_io; i++) {
if(x->time_counter == x->io_times[i]) {
return TRUE;
}
}
return FALSE;
}
/* Returns true if all processes have completed, false otherwise */
int processes_completed(struct process *p[], int num_processes) {
for(int i=0; i<num_processes; i++) {
if(p[i]->time_counter < p[i]->CPU_time) {
return FALSE;
}
}
return TRUE;
}