-
Notifications
You must be signed in to change notification settings - Fork 54
/
socketpair.c
78 lines (64 loc) · 1.71 KB
/
socketpair.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.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 sv[2];
int i, size, count, sum, n;
char *buf;
struct timeval begin, end;
if (argc != 3) {
printf("usage: ./socketpair <size> <count>\n");
return 1;
}
size = atoi(argv[1]);
count = atoi(argv[2]);
buf = malloc(size);
if (buf == NULL) {
perror("malloc");
return 1;
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
perror("socketpair");
return 1;
}
if (fork() == 0) {
sum = 0;
for (i = 0; i < count; i++) {
n = read(sv[0], buf, size);
if (n == -1) {
perror("read");
return 1;
}
sum += n;
}
if (sum != count * size) {
fprintf(stderr, "sum error: %d != %d\n", sum, count * size);
return 1;
}
} else {
gettimeofday(&begin, NULL);
for (i = 0; i < count; i++) {
if (write(sv[1], buf, size) != size) {
perror("wirte");
return 1;
}
}
gettimeofday(&end, NULL);
double tm = getdetlatimeofday(&begin, &end);
printf("%.0fMB/s %.0fmsg/s\n",
count * size * 1.0 / (tm * 1024 * 1024),
count * 1.0 / tm);
}
return 0;
}