Листинг А.1. Функция main, измеряющая полосу пропускания канала
//bench/bw_pipe.c
1 #include "unpipc.h"
2 void reader(int, int, int);
3 void writer(int, int);
4 void *buf;
5 int totalnbytes, xfersize;
6 int
7 main(int argc, char **argv)
8 {
9 int i, nLoop, contpipe[2], datapipe[2];
10 pid_t childpid;
11 if (argc != 4)
12 err_quit("usage: bw_pipe <#loops> <#mbytes> <#bytes/write>");
13 nloop = atoi(argv[1]);
14 totalnbytes = atoi(argv[2]) * 1024 * 1024;
15 xfersize = atoi(argv[3]);
16 buf = Valloc(xfersize);
17 Touch(buf, xfersize);
18 Pipe(contpipe);
19 Pipe(datapipe);
20 if ((childpid = Fork()) == 0) {
21 writer(contpipe[0], datapipe[1]); /* child */
22 exit(0);
23 }
24 /* 4parent */
25 Start_time();
26 for (i = 0; i < nloop; i++)
27 reader(contpipe[1], datapipe[0], totalnbytes);
28 printf("bandwidth: %.3f MB/sec\n",
29 totalnbytes / Stop_time() * nloop);
30 kill(childpid, SIGTERM);
31 exit(0);
32 }