-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.c
183 lines (157 loc) · 3.97 KB
/
part2.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
/*
Author: Marc Lee (Changhwan)
duckid: clee3
Title: CIS415 Project 1 Part2
Part1-This is my own work except getting ideas of tokenize and
fork() concpets in google, stackoverflow
Part2-This is my own work
*/
#include "mcp.h"
#ifndef PART2_C
#define PART2_C
/* These are global Variables */
Program pcb[256]; /* Max Program is 256 */
int run = 0; /* Variable for SIGUSR1 Handler */
/* Handler for SIGUSR1 */
void handler(int signal)
{
run++;
}
/* 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;
}
/* 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");
return;
}
while ((n = read(file, buffer, MAX_SIZE)) > 0)
{
int i, j;
int argnum = 0;
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!\n");
exit(EXIT_FAILURE);
}
else
{
if (pcb[i].pid < 0)
{
fprintf(stderr, "FORK ERROR\n");
exit(EXIT_FAILURE);
}
else
{
freeProg(&pcb[i]);
}
}
}
for (i = 0; i < argnum; i++)
{
kill(pcb[i].pid, SIGUSR1);
}
for (i = 0; i < argnum; i++)
{
kill(pcb[i].pid, SIGSTOP);
}
for (i = 0; i < argnum; i++)
{
kill(pcb[i].pid, SIGCONT);
}
for (i = 0; i < argnum; i++)
{
wait(&pcb[i].pid);
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;
}