-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
212 lines (169 loc) · 4.77 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
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void printHelp()
{
printf("\nHELP MENU: \n");
printf("\"init\" - Restarts scheduler and recreates init process\n");
printf("\"cr\" <name> <priority> - Creates process\n");
printf("\"print\" - Prints Processes\n");
printf("\"printName\" <name> - Print Process by name\n");
printf("\"exit\" - Quit\n");
printf("\"to\" - timeout\n");
printf("\"de\" <name> - destroy process by name\n");
printf("\"req\" <resource> <num> - request resource\n");
printf("\"rel\" <resource> <num> - release resource\n\n");
}
int parseInput(char *input, char *command, char *name, int *num)
{
if (input == NULL || command == NULL || name == NULL) {
printf("error ");
verbose_print("HUH?\n");
return -1;
}
/* Get rid of newline character to make our lives easier */
input[strlen(input) - 1] = '\0';
if (strchr(input, '\r') != NULL)
input[strlen(input) - 1] = '\0';
if (input[0] == '\0') {
verbose_print("No command entered\n");
return -1;
}
const char s[2] = " ";
char *token;
/* get first word */
token = strtok(input, s);
strcpy(command, token);
/* error checking! */
if (strcmp(token, "help") == 0)
goto nextEmpty;
if (strcmp(token, "printName") == 0)
goto word2;
if (strcmp(token, "print") == 0)
goto nextEmpty;
if (strcmp(token, "init") == 0)
goto nextEmpty;
if (strcmp(token, "exit") == 0)
goto nextEmpty;
if (strcmp(token, "to") == 0)
goto nextEmpty;
if (strcmp(token, "cr") == 0)
goto word2;
if (strcmp(token, "de") == 0)
goto word2;
if (strcmp(token, "req") == 0)
goto word2;
if (strcmp(token, "rel") == 0)
goto word2;
printf("error ");
verbose_print("\'%s\': not a valid command!\n", command);
return -1;
word2:
/* Getting the next word */
token = strtok(NULL, s);
if (token == NULL) {
printf("error ");
verbose_print("\'%s\': not enough arguments\n", command);
return -1;
}
strcpy(name, token);
if (strcmp(command, "de") == 0)
goto nextEmpty;
if (strcmp(command, "printName") == 0)
goto nextEmpty;
if (strcmp(command, "cr") == 0)
goto word3;
if (strcmp(command, "req") == 0)
goto word3;
if (strcmp(command, "rel") == 0)
goto word3;
word3:
token = strtok(NULL, s);
if (token == NULL) {
printf("error ");
verbose_print("\'%s\' not enough arguments\n", command);
return -1;
}
if ((token[1] == '\0') && isdigit(token[0])) {
*num = atoi(token);
goto nextEmpty;
}
printf("error ");
verbose_print("\'%s\' not a valid argument (must be single digit integer)\n", token);
return -1;
nextEmpty:
/* Next token should be null */
token = strtok(NULL, s);
if (token == NULL) return 0;
printf("error ");
verbose_print("\'%s\' too many arguments\n", command);
return -1;
}
int main()
{
/* Create state structure */
State *current;
verbose_print("Begin scheduler simulation\n");
/* Populate state structure */
current = setup();
verbose_print("State structure created\n");
/* Create init process */
if(createInitProcess(current)) {
return -1;
}
verbose_print("Init process created\n");
verbose_print("Simulation ready\n");
/* Start simulation */
char input[100];
char command[30];
int num;
char name[30];
while (1) {
//verbose_print("Commands: exit, help, print, printName <name>,\n");
//verbose_print("cr <name> <priority>, de <name>, to, init,\n");
//verbose_print("req <resource> <num>, rel <resource> <num>\n");
verbose_print("\nEnter Command: ");
/* Recieve command */
if(fgets(input, 100, stdin) == NULL) break;
/* Parse, if error skip iteration... */
if (parseInput(input, command, name, &num)) {
verbose_print("\n");
continue;
}
verbose_print("\nCommand: %s\n", command);
verbose_print("Name: %s\n", name);
verbose_print("Num: %d\n", num);
if (strcmp(command, "help") == 0)
printHelp();
else if (strcmp(command, "rel") == 0)
releaseResource(current, name, num);
else if (strcmp(command, "req") == 0)
requestResource(current, name, num);
else if (strcmp(command, "init") == 0) {
printf("\n");
createInitProcess(current);
}
else if (strcmp(command, "print") == 0)
printAll(current);
else if (strcmp(command, "printName") == 0)
printPCB(current, name);
else if (strcmp(command, "cr") == 0)
createProcess(current, name, num);
else if (strcmp(command, "de") == 0)
destroy(current, name);
else if (strcmp(command, "to") == 0)
timeout(current);
else if (strcmp(command, "exit") == 0)
break;
memset((void*) input, '\0', 100);
memset((void*) name, '\0', 30);
memset((void*) command, '\0',30);
num = 0;
fflush(stdout);
}
verbose_print("Exiting....\n");
verbose_print("Cleaning Up\n");
free(current);
return 0;
}