forked from jzplp/OSTEP-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.3.c
49 lines (48 loc) · 1.1 KB
/
6.3.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
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pi1[2];
int pi2[2];
char buf1[30], buf2[30];
int p1 = pipe(pi1);
int p2 = pipe(pi2);
long temptime;
struct timeval start, end;
int i;
if (p1 < 0 || p2 < 0)
{
fprintf(stderr, "pipe failed");
exit(1);
}
int rc = fork();
for (i = 0; i < 10; ++i)
{
if (rc < 0)
{
fprintf(stderr, "fork failed");
exit(1);
}
else if (rc == 0)
{
read(pi1[0], buf1, 25);
gettimeofday(&end, NULL);
printf("%d\n", end.tv_usec - atol(buf1));
gettimeofday(&start, NULL);
sprintf(buf2, "%ld", start.tv_usec);
write(pi2[1], buf2, 25);
}
else
{
gettimeofday(&start, NULL);
sprintf(buf1, "%ld", start.tv_usec);
write(pi1[1], buf1, 25);
read(pi2[0], buf2, 25);
gettimeofday(&end, NULL);
printf("%d\n", end.tv_usec - atol(buf2));
}
}
return 0;
}