-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisp.c
325 lines (276 loc) · 7.36 KB
/
isp.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define STR_LEN 1000
unsigned int N = 1000;
int number, position;
char* readLine() {
char* inputLine = malloc(sizeof(char[STR_LEN]));
fgets(inputLine, STR_LEN, stdin);
if (inputLine == NULL) {
perror("Unable to read command\n");
exit(1);
}
return inputLine;
}
char** parseCommand(char *line) {
number = 0;
int index = 0;
char **argv = malloc(number * sizeof(char *));
char *token;
char *buffer = strdup(line);
if (argv == NULL) {
fprintf(stderr, "memory allocation error\n");
exit(1);
}
token = strtok(buffer, " \n");
number++;
while (token != NULL) {
argv = realloc(argv, sizeof(char *) * number);
if (argv == NULL) {
fprintf(stderr, "memory allocation error\n");
exit(1);
}
argv[index++] = token;
token = strtok(NULL, " \n");
number++;
}
argv = realloc(argv, sizeof(char *) * number);
argv[index] = NULL;
return argv;
}
int runCommand(char **args) {
pid_t n;
n = fork();
if (n < 0) {
// fork failed
fprintf(stderr, "Fork Failed\n");
exit(1);
} else if (n == 0) {
// child process
execvp(args[0], args);
fprintf(stderr, "Cannot run command\n");
} else {
// parent process
wait(NULL);
}
return 1;
}
int checkCommand(char* line) {
int found = 0;
position = 0;
char **tokens = parseCommand(line);
while (tokens[position] != NULL) {
if (strchr(tokens[position], '|')) {
found = 1;
break;
}
position++;
}
return found;
}
int runNormalMode(char *line) {
char **tokens = parseCommand(line);
int check, first, second;
second = 0;
first = position + 1;
check = number - position;
char **str1 = malloc((first) * sizeof(char *));
char **str2 = malloc((check) * sizeof(char *));
for (int x = 0; x < position; x++) {
str1[x] = tokens[x];
}
for (int y = (first); y < number; y++) {
str2[second] = tokens[y];
second++;
}
str1[position] = NULL;
str2[check] = NULL;
// measure time of experiment
srand(time(0));
struct timeval start, end;
gettimeofday(&start, NULL);
int pipe1[2];
if (pipe(pipe1) == -1) {
fprintf(stderr, "\nError");
exit(1);
}
pid_t n1;
n1 = fork();
if (n1 < 0) {
// fork failed
fprintf(stderr, "\nFork Failed");
exit(1);
}
if (n1 == 0) {
// child process 1
dup2(pipe1[1], STDOUT_FILENO);
close(pipe1[0]);
close(pipe1[1]);
if (execvp(str1[0], str1) == -1) {
fprintf(stderr, "Cannot run command\n");
exit(2);
}
}
pid_t n2;
n2 = fork();
if (n2 < 0) {
// fork failed
fprintf(stderr, "\nFork Failed");
exit(3);
}
if (n2 == 0) {
// child process 2
dup2(pipe1[0], STDIN_FILENO);
close(pipe1[0]);
close(pipe1[1]);
if (execvp(str2[0], str2) == -1) {
fprintf(stderr, "Cannot run command\n");
}
}
close(pipe1[0]);
close(pipe1[1]);
waitpid(n1, NULL, 0);
waitpid(n2, NULL, 0);
// get time of experiment
gettimeofday(&end, NULL);
int seconds = (end.tv_sec - start.tv_sec);
int micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
printf("Time taken: %d s and %d ms\n", seconds, micros);
return 0;
}
void runTappedMode(char *line) {
char **tokens = parseCommand(line);
int check, first, second;
second = 0;
first = position + 1;
check = number - position;
char **str1 = malloc((first) * sizeof(char *));
char **str2 = malloc((check) * sizeof(char *));
for (int x = 0; x < position; x++) {
str1[x] = tokens[x];
}
for (int y = (first); y < number; y++) {
str2[second] = tokens[y];
second++;
}
str1[position] = NULL;
str2[check] = NULL;
// measure time of experiment
srand(time(0));
struct timeval start, end;
gettimeofday(&start, NULL);
int pipe1[2];
int pipe2[2];
if (pipe(pipe1) == -1) {
fprintf(stderr, "Error\n");
exit(1);
}
if (pipe(pipe2) == -1) {
fprintf(stderr, "Error\n");
exit(1);
}
// child process 1
pid_t n1;
n1 = fork();
if (n1 < 0) {
// fork failed
fprintf(stderr, "Fork Failed\n");
exit(1);
} else if (n1 == 0) {
close(pipe2[0]); // close unused ends
close(pipe2[1]);
close(pipe1[0]);
dup2(pipe1[1], STDOUT_FILENO);
if (execvp(str1[0], str1) == -1) {
fprintf(stderr, "Cannot run first command\n");
exit(1);
}
} else {
// child process 2
pid_t n2;
n2 = fork();
if (n2 < 0) {
// fork failed
fprintf(stderr, "Fork Failed\n");
exit(3);
} else if (n2 == 0) {
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[1]);
dup2(pipe2[0], STDIN_FILENO);
if (execvp(str2[0], str2) == -1) {
fprintf(stderr, "Cannot run second command\n");
exit(1);
}
} else {
// parent process
int characterCount = 0;
int bytesRead;
int count = 0;
close(pipe1[1]);
close(pipe2[0]);
char *buffer;
buffer = malloc(sizeof(char) * N);
while ((bytesRead = read(pipe1[0], &buffer, N)) > 0) {
write(pipe2[1], &buffer, bytesRead);
characterCount = characterCount + bytesRead;
count++;
}
close(pipe1[0]);
close(pipe2[1]);
waitpid(n1, NULL, 0);
waitpid(n2, NULL, 0);
// get time of experiment
gettimeofday(&end, NULL);
int seconds = (end.tv_sec - start.tv_sec);
int micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
printf("Time taken: %d s and %d ms\n", seconds, micros);
printf("\ncharacter-count: %d\nread-call-count: %d\nwrite-call-count: %d\n",
characterCount, count, count);
}
}
}
void userLoop(int mode) {
while (1) {
printf("isp:~$ ");
char *line = readLine();
char **args;
if (checkCommand(line) == 1) {
if (mode == 1) {
runNormalMode(line);
}
if (mode == 2) {
runTappedMode(line);
}
continue;
} else {
args = parseCommand(line);
}
// check if command is empty
if (args[0] == NULL) {
continue;
}
// check for exit
if (strcmp(args[0], "exit") == 0) {
break;
}
runCommand(args);
}
}
int main(int argc, char *argv[]) {
N = atoi(argv[1]);
int mode = atoi(argv[2]);
if (!(mode == 1 | mode == 2)) {
fprintf(stderr, "Mode can be 1 (normal mode) or 2 (tapped mode)\n");
exit(1);
}
if (N < 1 || N > 4096) {
printf("N should be between 1 and 4096\n");
exit(1);
}
userLoop(mode);
}