-
Notifications
You must be signed in to change notification settings - Fork 0
/
textmenu.c
346 lines (302 loc) · 8.05 KB
/
textmenu.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include "textmenu.h"
#include "process.h"
#include "message.h"
#include "semaphore.h"
/*
TextMenu
Manages the keyboard input and screen ouput functions
*/
static int currentProcessId = 0;
char inputChar() {
char input = getchar();
while (input == '\n') {
input = getchar();
}
input = toupper(input);
return input;
}
int inputPriorityInt() {
int input;
printf("Input Priority Int (0 - high, 1 - norm , 2 - low): ");
scanf("%d", &input);
while (input < 0 || input > 2) {
printf("Invalid Int\n");
scanf("%d", &input);
}
return input;
}
int inputPID() {
int input;
printf("Input Process ID: ");
scanf("%d", &input);
return input;
}
Message * inputMessage() {
Message * message = malloc(sizeof(Message));
if (message == NULL) {
return NULL;
}
message->msg = malloc(sizeof(char) * 40);
if (message->msg == NULL) {
free(message);
return NULL;
}
printf("Input Message (max 40 char): ");
scanf("%s", message->msg);
return message;
}
int inputSID() {
int input = -1;
printf("Input Semaphore ID (0-4): ");
scanf("%d", &input);
while (input < 0 || input > 4) {
printf("Invalid Semaphore ID\n");
printf("Input Semaphore ID (0-4): ");
scanf("%d", &input);
}
return input;
}
int inputSValue() {
int input = -1;
printf("Input Semaphore Value (0<=): ");
scanf("%d", &input);
while (input < 0) {
printf("Invalid Semaphore Value\n");
printf("Input Semaphore Value (0<=): ");
scanf("%d", &input);
}
return input;
}
void printIntroduction() {
printf("** PCB's and Processing Scheduling Simulation **\n");
printf("* Process Commands *\n");
printf("C - Create, F - Fork, K - Kill, E - Exit, Q - Quantum\n");
printf("* Message Commands *\n");
printf("S - Send, R - Receive, Y - Reply\n");
printf("* Semaphore Commands *\n");
printf("N - New Semaphore, P - Semaphore P, V - Semaphore V\n");
printf("* Info Commands *\n");
printf("I - Procinfo, T - Totalinfo\n");
}
void printInvalidSetup() {
printf("ERROR: Lists can't not be intialized");
}
void printInvalidCommand() {
printf("Invalid Command\n");
}
void printProcessChange() {
PCB * process = Process_getCurrentProcess();
if (process == NULL) {
return;
}
int pid = process->PID;
if (currentProcessId != pid ) {
printf("Process %d is running\n", pid);
currentProcessId = pid;
}
if (process->isMessageReceived) {
Message * message = Message_getMessage(process);
printf("(%d -> %d): %s\n", message->sender, message->receiver, message->msg);
Message_free(message);
process->isMessageReceived = false;
}
}
void printCreateReport(int pid) {
if (pid == -1) {
printf("FAILED: Process could not be created\n");
} else {
printf("SUCESS: Process %d created\n", pid);
}
}
void printForkReport(int pid) {
if (pid == -1) {
printf("FAILED: Process could not be forked\n");
} else {
printf("SUCESS: Process %d forked\n", pid);
}
}
void printKillReport(int pid) {
switch (pid) {
case -2:
printf("FAILED: Process init could not be killed\n");
break;
case -1:
printf("FAILED: Process could not be found\n");
break;
case 0:
printf("SUCESS: Process init is killed\n");
break;
default:
printf("SUCESS: Process %d is killed\n", pid);
}
}
void printExitReport(int pid) {
switch (pid) {
case -2:
printf("FAILED: Process init could not be exited\n");
break;
case -1:
printf("SUCESS: Process init is exited\n");
break;
case 0:
printf("SUCESS: Process init is running\n");
break;
default:
printf("SUCESS: Process %d exited\n", pid);
}
}
void printQuantumReport(int pid) {
currentProcessId = pid;
if (pid == 0) {
printf("Process init is running\n");
} else {
printf("Process %d is running\n", pid);
}
}
void printSendReport(int pid) {
if (pid < 0) {
printf("FAILED: Send failed\n");
} else {
printf("SUCESS: Process %d's message sent\n", pid);
}
}
void printReceiveReport(int pid) {
if (pid < 0) {
printf("FAILED: Receive failed\n");
return;
}
PCB * process = Process_getCurrentProcess();
if (process->isMessageReceived) {
Message * message = Message_getMessage(process);
printf("SUCESS: (%d -> %d): %s\n", message->sender, message->receiver, message->msg);
Message_free(message);
process->isMessageReceived = false;
} else {
printf("SUCESS: Process %d is waiting for Message\n", pid);
}
}
void printReplyReport(int pid) {
if (pid < 0) {
printf("FAILED: Reply failed\n");
return;
}
printf("SUCESS: Process %d has been replied\n", pid);
}
void printSemaphoreNewReport(int sid, int svalue) {
if (sid < 0) {
printf("FAILED: Semaphore already created\n");
} else {
printf("SUCESS: Semaphore %d created with value %d\n", sid, svalue);
}
}
void printSemaphorePReport(int sid) {
if (sid < 0) {
printf("FAILED: Semaphore not created yet\n");
} else {
printf("SUCESS: Semaphore %d P\n", sid);
}
}
void printSemaphoreVReport(int sid) {
if (sid < 0) {
printf("FAILED: Semaphore not created yet\n");
} else {
printf("SUCESS: Semaphore %d V\n", sid);
}
}
void printNumToPriority(int priority) {
switch (priority) {
case 0:
printf("High");
break;
case 1:
printf("Norm");
break;
case 2:
printf("Low");
break;
default:
break;
}
}
void printNumToState(int state) {
switch (state) {
case PROCESS_RUNNING:
printf("Running");
break;
case PROCESS_READY:
printf("Ready");
break;
case PROCESS_BLOCKED:
printf("Blocked");
break;
default:
break;
}
}
void procinfo(int pid) {
PCB * process = Process_getProcess(pid);
if (process == NULL) {
printf("Proc info: Process %d not found\n", pid);
return;
}
printf("Procinfo: PID - %d", pid);
if (pid == 0) {
printf(" (init)");
}
printf(", Priority - ");
printNumToPriority(process->priority);
printf(", State - ");
printNumToState(process->state);
printf(", Messages Waiting - %d", process->messages->count);
printf("\n");
}
void printArray(int * array) {
int count = array[0];
if (count == 0) {
printf("Empty \n");
} else {
printf("{");
for (int i = 0; i < count-1; i++) {
printf("%d, ", array[i+1]);
}
printf("%d}\n", array[count]);
}
free(array);
}
void totalinfo() {
int * array;
printf("* Totalinfo *\n");
PCB * process = Process_getCurrentProcess();
printf("Running: ");
if (process->PID == 0) {
printf("0 - init\n");
} else {
printf("%d\n", process->PID);
}
printf("High Ready Queue: ");
array = Process_getQueueArray(PRIORITY_HIGH);
printArray(array);
printf("Norm Ready Queue: ");
array = Process_getQueueArray(PRIORITY_NORM);
printArray(array);
printf("Low Ready Queue: ");
array = Process_getQueueArray(PRIORITY_LOW);
printArray(array);
printf("Send Waiting List: ");
array = Message_getQueueArray(QUEUE_SEND);
printArray(array);
printf("Receive Waiting List: ");
array = Message_getQueueArray(QUEUE_RECEIVE);
printArray(array);
for (int i = 0; i < SEMAPHORE_COUNT; i++) {
if (Semaphore_isEnabled(i)) {
printf("Semaphore %d Queue: ", i);
array = Semaphore_getQueueArray(i);
printArray(array);
}
}
}