-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart4.c
412 lines (358 loc) · 10.2 KB
/
part4.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
Author: Marc Lee (Changhwan)
duckid: clee3
Title: CIS415 Project 1 Part4
Part1-This is my own work except getting ideas of tokenize and
fork() concpets in google, stackoverflow
Part2-This is my own work
Part3-This is my own work except decide time quantum from textbook
and Getting idea of setitimer from https://stackoverflow.com/questions/2086126/need-programs-that-illustrate-use-of-settimer-and-alarm-functions-in-gnu-c
circular idea from Textbook
Part4-This is my own work except getting idea of man proc(5) to decide
which information I have to choose.
*/
#include "mcp.h"
#ifndef PART4_C
#define PART4_C
#define QUANTUM 100 /* 100 Millisecond */
/* These are global Variables */
Program pcb[256]; /* Max Program is 256 */
int run = 0; /* Variable for SIGUSR1 Handler */
int argnum = 0; /* Total number of programs */
pid_t mypid; /* store pid by calling getpid() */
int active = 0; /* Total number of programs that is active */
struct itimerval it_val; /* Time struct for setitimer() */
int num = 0; /* Global Variable for SIGALRM Handler */
/* Handler for SIGUSR1 */
void handler(int signal)
{
run++;
}
/* Handler for SIGUSR2 */
void handler2(int signal){/* Do Nothing */}
/* Helper function to free Programs
input: Program *
output: None
*/
void freeProg(Program *pcb)
{
int i;
for (i = 0; pcb->progs[i] != NULL; i++)
free(pcb->progs[i]);
free(pcb->progs);
}
/* Helper function to final tokenize */
/* input: Program*, char* */
/* output: None */
void tokenize(Program *pcb, char* line)
{
int s, j;
int commands = 1;
for (s = 0; s < strlen(line); s++)
{
if (line[s] == ' ')
commands++;
}
char* tok = strtok(line, " ");
pcb->command = tok;
pcb->progs = (char **) malloc ((commands+1) * sizeof (char *));
j = 0;
while (tok != NULL)
{
pcb->progs[j++] = strdup(tok);
tok = strtok(NULL, " ");
}
pcb->progs[j] = NULL;
}
/* Handler for SIGCHLD */
void onchild(int signal)
{
pid_t pid;
int status;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
{
if (WIFEXITED(status) || WIFSIGNALED(status))
{
if (getIndex(pid) == -1)
{
fprintf(stderr, "Pid does not exist!");
exit(EXIT_FAILURE);
}
int i = getIndex(pid);
pcb[i].HasExited = 1;
printf("PID %u: Done Executing\n", pcb[i].pid);
active--;
kill(mypid, SIGUSR2);
}
}
}
/*Handler for SIGALRM */
void onalarm(int signal)
{
int prev;
if (num == 0) {
prev = active-1;
}
else {
prev = num-1;
}
/* if previous program has been started, then stop the process */
if (!pcb[prev].HasExited)
{
if (pcb[prev].status == STARTED)
{
kill(pcb[prev].pid, SIGSTOP);
}
printProc(&pcb[num]);
}
if (!pcb[num].HasExited)
{
if (pcb[num].status == READY)
{
pcb[num].status = STARTED;
kill(pcb[num].pid, SIGUSR1);
} else if (pcb[num].status == STARTED)
{
kill(pcb[num].pid, SIGCONT);
}
}
if (num == argnum-1)
num = 0;
else
num++;
}
/* Print proc information
1. /proc/[pid]/stat: PID, State, PPID, usrtime, systime
2. /proc/[pid]/cmdline: args
3. /proc/[pid]/io: syscr, syscw
*/
void printProc(Program *pcb)
{
int file, cmdfile, iofile;
int n, i, total;
if (pcb->pid == 0) return;
char buffer[100], cmdbuf[100], iobuf[100];
char *stats[20], *io[20];
char *cmd;
char *print_list[20];
/* stat file */
snprintf(buffer, sizeof(buffer), "/proc/%u/stat", pcb->pid);
file = open(buffer, O_RDONLY);
if (file == -1) return;
n = read(file, buffer, sizeof(buffer));
if (n == 0) return;
buffer[n-1] = '\0';
char* token = strtok(buffer, " ");
i=0;
while (token != NULL)
{
stats[i++] = token;
token = strtok(NULL, " ");
total++;
}
print_list[0] = stats[0]; //PID
print_list[1] = stats[2]; //State
print_list[2] = stats[4]; //PPID
print_list[3] = stats[14]; //User
print_list[4] = stats[15]; //Sys
close(file);
n = 0;
snprintf(cmdbuf, sizeof(cmdbuf), "/proc/%u/cmdline", pcb->pid);
cmdfile = open(cmdbuf, O_RDONLY);
if (cmdfile == -1) return;
n = 0;
n = read(cmdfile, cmdbuf, sizeof(cmdbuf));
if (n == 0) return;
for (i = 0; i < n; i++)
{
if (cmdbuf[i] == '\0')
{
if (i+1 == n)
cmdbuf[i] = '\n';
else if (cmdbuf[i+1] != '\0')
cmdbuf[i] = ' ';
}
}
close(cmdfile);
cmd = strtok(cmdbuf, "\n");
print_list[5] = cmd;
snprintf(iobuf, sizeof(iobuf), "/proc/%u/io", pcb->pid);
iofile = open(iobuf, O_RDONLY);
if (iofile == -1) return;
n = read(iofile, iobuf, sizeof(iobuf));
iobuf[n-1] = '\0';
token = strtok(iobuf, "\n");
i = 0;
while (token != NULL)
{
io[i++] = token;
token = strtok(NULL, "\n");
}
print_list[6] = io[2]; //syscr
print_list[7] = io[3]; //syscw
for (i = 0; i < 8; i++)
{
if (i == 0)
printf("PID: %s", print_list[i]);
else if (i == 1)
printf(" State: %s", print_list[i]);
else if (i == 2)
printf(" PPID: %s", print_list[i]);
else if (i == 3)
printf(" Usertime: %s", print_list[i]);
else if (i == 4)
printf(" Systime: %s", print_list[i]);
else if (i == 5)
printf(" CMD: %s", print_list[i]);
else if (i == 6)
printf(" Sysread: %s", print_list[i]);
else if (i == 7)
printf(" Syswrite: %s\n", print_list[i]);
}
}
/* helper function to have index of certain pid
input: pid_t
output: int
*/
int getIndex(pid_t pid)
{
int i;
for (i = 0; i < argnum; i++)
{
if (pcb[i].pid == pid)
return i;
}
return -1;
}
/* Execute all processes */
void execute(int file)
{
char buffer[MAX_SIZE];
int n;
if (signal(SIGUSR1, handler) == SIG_ERR)
{
fprintf(stderr, "Error on make SIGUSR1\n");
return;
}
if (signal(SIGUSR2, handler2) == SIG_ERR)
{
fprintf(stderr, "Error on make SIGUSR2\n");
return;
}
mypid = getpid();
if (signal(SIGCHLD, onchild) == SIG_ERR)
{
fprintf(stderr, "Error on make SIGCHLD\n");
return;
}
if (signal(SIGALRM, onalarm) == SIG_ERR)
{
fprintf(stderr, "Error on make SIGALRM\n");
return;
}
while ((n = read(file, buffer, MAX_SIZE)) > 0)
{
int i, j;
char *token;
char *args[128];
buffer[n] = '\0';
token = strtok(buffer, "\n");
j = 0;
while (token != NULL)
{
args[j++] = strdup(token);
token = strtok(NULL, "\n");
argnum++;
}
if (argnum == 256)
{
fprintf(stderr, "Too Many processes at the same time! Max 256\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < argnum; i++)
{
tokenize(&pcb[i], args[i]);
pcb[i].pid = fork();
if (pcb[i].pid == 0)
{
while(run == 0)
{
usleep(1);
}
execvp(pcb[i].command, pcb[i].progs);
freeProg(&pcb[i]);
//freeing args
for (i = 0; i < argnum; i++)
{
free(args[i]);
}
fprintf(stderr, "Error with execvp! PID: %u\n", pcb[i].pid);
exit(EXIT_FAILURE);
}
else
{
if (pcb[i].pid < 0)
{
fprintf(stderr, "FORK ERROR: PID: %u\n", pcb[i].pid);
exit(EXIT_FAILURE);
}
else
{
pcb[i].status = READY;
pcb[i].HasExited = 0;
freeProg(&pcb[i]);
}
}
}
active = argnum;
for (i = 0; i < argnum; i++)
{
printProc(&pcb[i]);
}
/* Set Timer for millisecond. Used alarm() with time 1 sec and not concurrent*/
/* Information from stackoverflow link provided at the top */
/* Accroding to OSE book, best time quantum is 10ms ~ 100ms */
/* alarm cannot take milisecond */
it_val.it_value.tv_sec = QUANTUM/1000;
it_val.it_value.tv_usec = (QUANTUM*1000) % 1000000;
it_val.it_interval = it_val.it_value;
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1)
{
fprintf(stderr, "Error calling setitimer()\n");
exit(EXIT_FAILURE);
}
/* Done setitimer */
onalarm(SIGALRM);
/* pause until there is no active process */
while (active > 0)
{
pause();
}
for (i = 0; i < argnum; i++)
{
free(args[i]);
}
}
}
#endif
int main(int argc, char** argv)
{
int file;
if (argc > 2)
{
fprintf(stderr, "Usage: %s [wordload_file]\n", *argv);
exit(EXIT_FAILURE);
}
file = open(argv[1], O_RDONLY);
if (file == -1)
{
fprintf(stderr, "NO FILE TO READ!\n");
exit(EXIT_FAILURE);
}
execute(file);
close(file);
printf("=======================\n");
printf("OS Successfully Exited!\n");
printf("=======================\n");
return 0;
}