forked from detailyang/ipc_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
75 lines (59 loc) · 1.54 KB
/
pipe.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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
double
getdetlatimeofday(struct timeval *begin, struct timeval *end)
{
return (end->tv_sec + end->tv_usec * 1.0 / 1000000) -
(begin->tv_sec + begin->tv_usec * 1.0 / 1000000);
}
int
main(int argc, char *argv[]) {
int pipefd[2] = {0};
int i, size, count, sum, n;
char *buf;
struct timeval begin, end;
if (argc != 3) {
printf("usage: ./pipe <size> <count>\n");
return 1;
}
size = atoi(argv[1]);
count = atoi(argv[2]);
buf = malloc(size);
if (buf == NULL) {
perror("malloc");
return 1;
}
if (pipe(pipefd) == -1) {
perror("pipe");
return 1;
}
if (fork() == 0) {
sum = 0;
for (i = 0; i < count; i++) {
n = read(pipefd[0], buf, size);
if (n == -1) {
perror("read");
return 1;
}
sum += n;
}
if (sum != count * size) {
return 1;
}
} else {
gettimeofday(&begin, NULL);
for (i = 0; i < count; i++) {
if (write(pipefd[1], buf, size) != size) {
perror("wirte");
return 1;
}
}
gettimeofday(&end, NULL);
printf("%.0fMb/s %.0fmsg/s\n",
(count * size * 1.0 / getdetlatimeofday(&begin, &end)) * 8 / 1000000,
(count * 1.0 / getdetlatimeofday(&begin, &end)));
}
return 0;
}