-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
200 lines (161 loc) · 7.45 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "structures.h"
#include "operations.h"
int main(int argc, char * argv[]){
FILE * infile, * outfile;
// Comment out for file redirection in Linux host
//infile = (argc > 1) ? fopen(argv[1], "r") : fopen("input.txt", "r");
//outfile = (argc > 2) ? fopen(argv[2], "w+") : fopen("output.txt", "w+");
// Comment out for user-specified filepaths
printf("Input File Path: ");
size_t isize; ssize_t iinput_size; char * iinput = NULL;
iinput_size = getline(&iinput, &isize, stdin);
char * filepath = strtok(iinput, " \n");
infile = (filepath != NULL) ? fopen(filepath, "r") : fopen("input.txt", "r");
printf("Output File Path: ");
iinput_size = getline(&iinput, &isize, stdin);
filepath = strtok(iinput, " \n");
outfile = (filepath != NULL) ? fopen(filepath, "w+") : fopen("output.txt", "r");
//infile = fopen("test.txt", "r");
//outfile = fopen("out.txt", "w+");
if(!infile){
printf("Please include a file...\n");
} else {
// Initialize System
ReadyList * readylist = initReadylist();
ResourceList * resourcelist = initResourcelist();
PCB * active_process = initProcess(readylist);
writeoutput(getProcessName(active_process), outfile);
//printf("\t*Process %s is running\n", getProcessName(active_process));
// Initialize Shell
size_t size; ssize_t input_size; char * input = NULL;
//printf("shell >> ");
// Comment out for user input
// while((input_size = getline(&input, &size, stdin) ) != EOF ){
while((input_size = getline(&input, &size, infile)) != EOF) {
char * command = strtok(input, " \n");
if(command == NULL){
//printf("\tPlease include a command.\n");
fputs("\n", outfile);
} else if(!strcmp(command, "init")) {
freeReadylist(readylist);
freeWaitlist(resourcelist);
freeResourcelist(resourcelist);
readylist = initReadylist();
resourcelist = initResourcelist();
active_process = initProcess(readylist);
writeoutput(getProcessName(active_process), outfile);
} else if (!strcmp(command, "cr")) {
char * temp = strtok(NULL, " \n");
char * name = isWord(temp) ? strdup(temp) : NULL;
temp = strtok(NULL, " \n");
char * priority = isNumber(temp) ? strdup(temp) : NULL;
int pval = priority != NULL ? (int) strtol(priority, NULL, 10) : -1;
if(name && priority
&& strtok(NULL, " \n") == NULL
&& isValidName(name)
&& isInRange(pval)
&& findProcess(name, readylist) == NULL
&& findProcessBlocked(name, resourcelist) == NULL){
create(name, pval, readylist, &active_process);
writeoutput(getProcessName(active_process), outfile);
}
else {
//printf("\tInvalid command; please try again.\n");
writeoutput("error", outfile);
}
if(name){free(name);}
if(priority){free(priority);}
} else if (!strcmp(command, "de")) {
char * temp = (strtok(NULL, " \n"));
char * pid = isWord(temp) ? strdup(temp) : NULL;
if(!pid || strtok(NULL, " \n")){
//printf("\tInvalid command; please try again.\n");
writeoutput("error", outfile);
} else if (!strcmp(pid, "init")){
//printf("\tCannot delete %s process.\n", pid);
writeoutput("error", outfile);
} else if (!delete(pid, readylist, resourcelist, &active_process)){
//printf("\tProcess %s does not exist.\n", pid);
writeoutput("error", outfile);
} else {
writeoutput(getProcessName(active_process), outfile);
}
if(pid){free(pid);};
} else if (!strcmp(command, "req")) {
char * temp = strtok(NULL, " \n");
char * rid = strdup(temp);
temp = strtok(NULL, " \n");
char * units = isNumber(temp) ? strdup(temp) : NULL;
int value = units != NULL ? (int)strtol(units, NULL, 10) : -1;
if(rid && units && strtok(NULL, " \n") == NULL && isInRange2(value)
&& (!strcmp(rid, "R1") || !strcmp(rid, "R2") ||
!strcmp(rid, "R3") || !strcmp(rid, "R4") )){
int n = request(rid, value, readylist, resourcelist, &active_process);
if(n == 0){
writeoutput("error", outfile);
} else {
writeoutput(getProcessName(active_process), outfile);
}
} else {
writeoutput("error", outfile);
}
if(rid){free(rid);}
if(units){free(units);}
} else if (!strcmp(command, "rel")) {
char * temp = strtok(NULL, " \n");
char * rid = strdup(temp);
temp = strtok(NULL, " \n");
char * units = isNumber(temp) ? strdup(temp) : NULL;
int value = units != NULL ? (int)strtol(units, NULL, 10) : -1;
if(rid && units && strtok(NULL, " \n") == NULL && isInRange2(value)
&& (!strcmp(rid, "R1") || !strcmp(rid, "R2") ||
!strcmp(rid, "R3") || !strcmp(rid, "R4") )){
release(rid, value, resourcelist, readylist, &active_process);
writeoutput(getProcessName(active_process), outfile);
} else {
writeoutput("error", outfile);
}
if(rid){free(rid);}
if(units){free(units);}
} else if (!strcmp(command, "to")) {
timeout(readylist, &active_process);
writeoutput(getProcessName(active_process), outfile);
} else if (!strcmp(command, "exit")) {
//printf("\tExiting session...\n");
break;
} else if (!strcmp(command, "showProcesses")) {
// For Debugging Purposes
printReadyList(readylist);
} else if (!strcmp(command, "showTree")) {
// For Debugging Purposes
char * name = strdup(strtok(NULL, "\n"));
printTree(name, readylist);
if(name){free(name);}
} else if (!strcmp(command, "showWaitlist")) {
// For Debugging Purposes
char *rid = strdup(strtok(NULL, "\n"));
printWaitlist(rid, resourcelist);
if (rid) { free(rid); }
} else if(!strcmp(command, "showResources")){
char * pid = strdup(strtok(NULL, "\n"));
printResources(pid, readylist);
if(pid){free(pid);}
} else {
//printf("\tInvalid command; please try again.\n");
}
//printf("\t*Process %s is running\n", getProcessName(active_process));
//printf("shell >> ");
}
// Freeing Resources
free(input);
free(iinput);
freeReadylist(readylist);
freeWaitlist(resourcelist);
freeResourcelist(resourcelist);
fclose(infile);
fclose(outfile);
}
}