-
Notifications
You must be signed in to change notification settings - Fork 0
/
newestimatescron.c
312 lines (262 loc) · 8 KB
/
newestimatescron.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <ctype.h>
//You're going to have to trim these libraries later, but this is what the old project had
#define ALLVALUES -1
#define ERRORVAL -2
#define MAX_CMDLINES 20
#define MAX_CMDNAME_LEN 41
#define MAX_FILENAME_LEN 101
//'weekday' is a bad way to decribe this, and vague. meed a better way to describe days of the week
int weekdayToInt(char *day_name) {
char days[7][4] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
for (int dayNum = 0; dayNum < 7; dayNum++) {
if (strcmp(days[dayNum], day_name)) {
return dayNum;
}
}
return ERRORVAL;
}
//Converts month string to int code
int monthToInt(char *month_name) {
char months[12][4] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
for (int monthNum = 0; monthNum < 12; monthNum++) {
if (strcmp(months[monthNum], month_name) == 0) {
return monthNum;
}
}
return ERRORVAL;
}
//Get's the first weekday of the month
int firstDayOfMonth(int month) {
struct tm tm;
memset(&tm, 0, sizeof(tm));
tm.tm_mday = 1;
tm.tm_mon = month;
tm.tm_year = (2022 - 1900);
mktime(&tm);
return tm.tm_wday;
}
#define FEBUARY 1
#define APRIL 3
#define JUNE 5
#define SEPTEMBER 8
#define NOVEMBER 10
//Returns the number of days in a month
int GetDaysInMonth(int month) {
if (month == FEBUARY) {
return 28;
} else if (month == APRIL || month == JUNE || month == SEPTEMBER || month == NOVEMBER) {
return 30;
} else {
return 31;
}
}
//Converts a passed string of data into an integer form
//The mode indicates that this string is a month/week, which pertains to how a string should be deciphered
#define MODE_NONE 0
#define MODE_WEEK 1
#define MODE_MONTH 2
int my_atoi(char *datastring, int mode) {
if (isdigit(datastring[0])) {
return atoi(datastring);
} else if (datastring[0] == '*') {
return ALLVALUES;
} else {
//At this point, convert a 3-char string to an integer based off the mode
switch (mode) {
case MODE_WEEK:
return weekdayToInt(datastring);
case MODE_MONTH:
return monthToInt(datastring);
default:
return ERRORVAL;
}
}
}
//
struct {
int minute;
int hour;
int date;
int month;
int weekday;
char name[MAX_CMDNAME_LEN];
int estimatesID;
} crontabFile[MAX_CMDLINES];
struct {
char name[MAX_CMDNAME_LEN];
int runtime;
int runcount;
} estimatesFile[MAX_CMDLINES];
//Returns whether it succeed or failed to find an available space to put the process
int invokeProcess(int endTime, int *processEndTimes) {
//Find available space for the process to run
for (int i = 0; i < MAX_CMDLINES; i++) {
if (processEndTimes[i] == -1) {
processEndTimes[i] = endTime;
return 1;
}
}
return 0;
}
#define CRONTABFILE 0
#define ESTIMATESFILE 1
int readfiles(char *filename, int filetype) {
FILE *dict = fopen(filename, "r");
if (dict == NULL) {
fprintf(stderr, "error: cannot open '%s'\n", filename);
exit(EXIT_FAILURE);
}
char line[MAX_FILENAME_LEN];
char *token;
int lineIndex = 0;
while (fgets(line, sizeof line, dict) != NULL) {
line[strcspn(line, "\r\n")] = 0;
//Ignore empty lines
if (strlen(line) == 0) {
continue;
}
token = strtok(line, " ");
//Ignore comment lines
if (token[0] == '#') {
continue;
}
switch (filetype) {
case CRONTABFILE:
crontabFile[lineIndex].minute = my_atoi(token, MODE_NONE);
token = strtok(NULL, " ");
crontabFile[lineIndex].hour = my_atoi(token, MODE_NONE);
token = strtok(NULL, " ");
crontabFile[lineIndex].date = my_atoi(token, MODE_NONE);
token = strtok(NULL, " ");
crontabFile[lineIndex].month = my_atoi(token, MODE_MONTH);
token = strtok(NULL, " ");
crontabFile[lineIndex].weekday = my_atoi(token, MODE_WEEK);
token = strtok(NULL, " ");
//Test if there is any errors when reading the file
if (crontabFile[lineIndex].minute == ERRORVAL ||
crontabFile[lineIndex].hour == ERRORVAL ||
crontabFile[lineIndex].date == ERRORVAL ||
crontabFile[lineIndex].month == ERRORVAL ||
crontabFile[lineIndex].weekday == ERRORVAL) {
printf("%d %d %d %d %d", crontabFile[lineIndex].minute, crontabFile[lineIndex].hour, crontabFile[lineIndex].date,
crontabFile[lineIndex].month, crontabFile[lineIndex].weekday);
fprintf(stderr, "Error reading non-comment line '%d': Make sure data is valid\n", lineIndex);
exit(EXIT_FAILURE);
}
for (int j = 0; j < sizeof(estimatesFile)/sizeof(estimatesFile[0]); j++) {
if (strcmp(token, estimatesFile[j].name) == 0) {
crontabFile[lineIndex].estimatesID = j;
break;
}
}
break;
case ESTIMATESFILE:
strcpy(estimatesFile[lineIndex].name, token);
token = strtok(NULL, " ");
estimatesFile[lineIndex].runtime = atoi(token);
break;
}
lineIndex++;
}
fclose(dict);
return lineIndex;
}
//Invalid end times are -1. -1 is returned if there are no processes running
int GetNextEndingTime(int *processEndTimes) {
int min = -1;
for (int i = 0; i < MAX_CMDLINES; i++) {
if (min == -1 || processEndTimes[i] < min) {
min = i;
}
}
return min;
}
int main(int argcount, char *argvalue[]) {
if (argcount != 4) {
fprintf(stderr, "Error: received %d arguments when there should have been 4", argcount);
exit(EXIT_FAILURE);
}
int estimateLines = readfiles(argvalue[3], ESTIMATESFILE);
int crontabLines = readfiles(argvalue[2], CRONTABFILE);
int month = my_atoi(argvalue[1], MODE_MONTH); //Should error check here
int minutesInMonth = 60 * 24 * GetDaysInMonth(month);
int currentRunningProcesses = 0;
int maxRunningProcesses = 0;
int firstDay = firstDayOfMonth(month);
int processEndTimes[MAX_CMDLINES];
//Initalise processEndTimes with -1, as no process has begun yet
for (int i = 0; i < MAX_CMDLINES; i++) {
processEndTimes[i] = -1;
}
//Check every minute in month to see if anything starts, if anything ends
for (int i = 0; i < minutesInMonth; i++) {
//Ending processes
for (int j = 0; j < MAX_CMDLINES; j++) {
if (processEndTimes[j] == i) {
processEndTimes[j] = -1;
currentRunningProcesses -= 1;
printf("Ended a process at point '%d' at time %d \n", j, i);
}
}
//Can only end a process
if (currentRunningProcesses == MAX_CMDLINES) {
continue;
}
//Checking if any process can be starting
for (int j = 0; j < crontabLines; j++) {
int valid = 1; //Default true
int hour = i / 60;
int day = i / (60 * 24);
if (crontabFile[j].date != ALLVALUES) {
//Test if the day in the current minute is in fileinfo[j].date
if (crontabFile[j].date != day) {
valid = 0;
}
}
if (crontabFile[j].weekday != ALLVALUES) {
//Test if the day in the current minute is a valid weekday
if ((firstDay + day) % 7 != crontabFile[j].weekday) {
valid = 0;
}
}
if (crontabFile[j].month != ALLVALUES && crontabFile[j].month != month) {
valid = 0;
}
if (crontabFile[j].hour != ALLVALUES) {
if (hour % 24 != crontabFile[j].hour) {
valid = 0;
}
}
if (crontabFile[j].minute != ALLVALUES) {
if (i % 60 != crontabFile[j].minute) {
valid = 0;
}
}
if (valid == 1) {
//Invoke process
int returnval = invokeProcess(i + estimatesFile[crontabFile[j].estimatesID].runtime, processEndTimes);
printf("Invoked process at point '%d' at time %d with endtime %d. Process id: %d \n", returnval, i, i + estimatesFile[crontabFile[j].estimatesID].runtime, crontabFile[j].estimatesID);
currentRunningProcesses += 1;
estimatesFile[crontabFile[j].estimatesID].runcount += 1;
if (currentRunningProcesses > maxRunningProcesses) {
maxRunningProcesses = currentRunningProcesses;
}
}
}
}
//We've accumulated the run counts for every
int mostRanProgram = 0;
int cumulativeProcesses = 0;
for (int i = 0; i < estimateLines; i++) {
cumulativeProcesses += estimatesFile[i].runcount;
if (estimatesFile[mostRanProgram].runcount < estimatesFile[i].runcount) {
mostRanProgram = i;
}
}
printf("%s %d %d", estimatesFile[mostRanProgram].name, cumulativeProcesses, maxRunningProcesses);
}