-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ1.c
294 lines (264 loc) · 7.81 KB
/
Q1.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
//#define DEBUG
#define ARG_ROW 5
#define ARG_COL 100
void readInputLine(char*);
int parseInput(char*, char*);
void printArgs(int, char*);
void processCommand(int, char*);
void startNewTask(char*, int);
void simulateK(int);
void displaySystemTime();
void displayTotalIdleTime();
void displayMostRecentlyCompletedTask();
void dumpCompletedToFile(char*);
void deleteCompletedQueue();
void displayFirstTask();
Queue waitingQueue, completedQueue;
long systemTime = 0, idleTime = 0;
int main(){
printf("Creating queues...\n");
waitingQueue.size = 0;
completedQueue.size = 0;
printf("Done.\n");
printf("Allocating heap memory for command strings...\n");
char *input = (char*) malloc(ARG_COL*ARG_ROW*sizeof(char));
char *arguments = (char*) malloc(ARG_ROW*ARG_COL*sizeof(char));
if(!input || !arguments){
printf("Couldn't allocate heap memory. Exiting...\n");
free(input);
free(arguments);
exit(1);
}
printf("Done.\n");
printf("Welcome!\n");
while(1){
printf("\nroot@the-pearl:~$ ");
readInputLine(input);
#ifdef DEBUG
printf("Input: %s\n", input);
#endif
int argCount = parseInput(arguments, input);
#ifdef DEBUG
printArgs(argCount, arguments);
#endif
if(!strcmp(arguments, "QUIT") && argCount == 1){
printf("Quitting...\n");
free(input);
free(arguments);
deleteQueue(&waitingQueue);
deleteQueue(&completedQueue);
exit(0);
}
processCommand(argCount, arguments);
}
}
void readInputLine(char *ptr){
fgets(ptr, ARG_COL*ARG_ROW, stdin);
*(ptr + strlen(ptr) - 1) = '\0';
}
int parseInput(char *arguments, char *input){
int argCount = 0;
int readingWord = 0;
int currentLetter = 0;
for(int i = 0; i < strlen(input); i++){
char c = *(input+i);
if(readingWord){
if(c == ' '){
readingWord = 0;
*(arguments+(ARG_COL*(argCount-1))+currentLetter) = '\0';
}
else{
*(arguments+(ARG_COL*(argCount-1))+currentLetter) = c;
currentLetter++;
if(i == (strlen(input)-1)){
*(arguments+(ARG_COL*(argCount-1))+currentLetter) = '\0';
}
}
}
else{
if(c != ' '){
readingWord = 1;
argCount++;
*(arguments+(ARG_COL*(argCount-1))) = c;
currentLetter = 1;
if(i == (strlen(input)-1)){
*(arguments+(ARG_COL*(argCount-1))+currentLetter) = '\0';
}
}
}
}
return argCount;
}
// used for debug purposes
void printArgs(int argCount, char *arguments){
for(int i = 0; i < argCount; i++){
for(int j = 0; j < ARG_COL; j++){
if(*(arguments+(ARG_COL*i)+j) == '\0'){
break;
}
printf("%c", *(arguments+(ARG_COL*i)+j));
}
printf("\n");
}
}
void processCommand(int argCount, char *arguments){
char *command = arguments;
if(!strcmp(command, "SNT")){
if(argCount == 3){
char *fileName = arguments+(ARG_COL*1);
char *runtime = arguments+(ARG_COL*2);
startNewTask(fileName, strToInt(runtime));
}
else{
printf("Invalid arguments for command SNT. Usage: SNT <task_name> <runtime>\n");
}
}
else if(!strcmp(command, "S")){
if(argCount == 2){
char *k = arguments+(ARG_COL*1);
simulateK(strToInt(k));
}
else{
printf("Invalid arguments for command S. Usage: S <k>\n");
}
}
else if(!strcmp(command, "DST")){
if(argCount == 1){
displaySystemTime();
}
else{
printf("Invalid arguments for command DST. Usage: DST\n");
}
}
else if(!strcmp(command, "DTIT")){
if(argCount == 1){
displayTotalIdleTime();
}
else{
printf("Invalid arguments for command DTIT. Usage: DTIT\n");
}
}
else if(!strcmp(command, "DIMRCT")){
if(argCount == 1){
displayMostRecentlyCompletedTask();
}
else{
printf("Invalid arguments for command DIMRCT. Usage: DIMRCT\n");
}
}
else if(!strcmp(command, "DCTQ")){
if(argCount == 2){
char *fileName = arguments+(ARG_COL*1);
dumpCompletedToFile(fileName);
}
else{
printf("Invalid arguments for command DCTQ. Usage: DCTQ <file_name>\n");
}
}
else if(!strcmp(command, "ECTS")){
if(argCount == 1){
deleteCompletedQueue();
}
else{
printf("Invalid arguments for command ECTS. Usage: ECTS\n");
}
}
else{
printf("Invalid command.\n");
}
}
void startNewTask(char *taskName, int runtime){
printf("Creating new task...\n");
Task *newTask = (Task*) malloc(sizeof(Task));
if(!newTask){
printf("Heap allocation failed.\n");
return;
}
newTask->identifier = (char*) malloc((strlen(taskName)+1)*sizeof(char));
if(!newTask->identifier){
printf("Heap allocation failed.\n");
free(newTask);
return;
}
for(int i = 0; i < strlen(taskName)+1; i++){
*(newTask->identifier+i) = *(taskName+i);
}
newTask->runtime = runtime;
newTask->spentTime = 0;
newTask->initTime = systemTime;
newTask->completionTime = -1;
insert(&waitingQueue, newTask);
printf("Task created:\n");
printf("Task Name: %s, Runtime: %d\n", taskName, runtime);
}
void simulateK(int k){
for(int i = 0; i < k; i++){
Task* task = pop(&waitingQueue);
if(task == NULL){
printf("Waiting task list is empty.\n");
idleTime++;
return;
}
systemTime++;
task->spentTime++;
if(task->spentTime >= task->runtime){
task->completionTime = systemTime;
insert(&completedQueue, task);
}
else{
insert(&waitingQueue, task);
}
}
}
void displaySystemTime(){
printf("System Time: %ld\n", systemTime);
}
void displayTotalIdleTime(){
printf("Total Idle Time: %ld\n", idleTime);
}
void displayMostRecentlyCompletedTask(){
Task* task = getLastTask(&completedQueue);
if(task != NULL){
printf("Most Recently Completed Task:\n");
printf("Task Name: %s\n", task->identifier);
printf("Runtime: %d\n", task->runtime);
printf("Elapsed Time: %ld\n", task->completionTime-task->initTime);
printf("Completion Time: %ld\n", task->completionTime);
}
else{
printf("There are no completed tasks.\n");
}
}
void dumpCompletedToFile(char *fileName){
FILE *out = fopen(fileName, "w");
if(!out){
printf("Failed to create file %s\n", fileName);
return;
}
fprintf(out, "identifier, runtime, elapsedTime, completionTime:\n");
Node *curr = getFirstNode(&completedQueue);
while(curr){
fprintf(out, "%s, %d, %ld, %ld\n", curr->data->identifier, curr->data->runtime, curr->data->completionTime-curr->data->initTime, curr->data->completionTime);
curr = curr->next;
}
fclose(out);
printf("Completed tasks queue dumped into %s\n", fileName);
}
void deleteCompletedQueue(){
deleteQueue(&completedQueue);
printf("Completed tasks queue has been deleted.\n");
}
// used for debug purposes
void displayFirstTask(){
Task* task = getFirstTask(&waitingQueue);
if(task){
printf("%s, %d/%d", task->identifier, task->spentTime, task->runtime);
}
else{
printf("Queue empty.\n");
}
}