xref: /dragonfly/test/sysperf/pipe1.c (revision e5a92d33)
1 /*
2  * pipe1.c
3  *
4  * $DragonFly: src/test/sysperf/pipe1.c,v 1.1 2003/08/12 02:29:44 dillon Exp $
5  */
6 
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include "blib.h"
10 
11 int
12 main(int ac, char **av)
13 {
14     long long count = 0;
15     long long max;
16     char c[1];
17     int j;
18     int loops;
19     int fds[2];
20 
21     printf("tests full duplex pipe 1write,2read,2write,1read loop\n");
22     if (pipe(fds)) {
23 	perror("pipe");
24 	exit(1);
25     }
26     if (fork() == 0) {
27 	/*
28 	 * child process
29 	 */
30 	close(fds[0]);
31 	while (read(fds[1], c, sizeof(c)) == sizeof(c)) {
32 	    write(fds[1], c, sizeof(c));
33 	}
34 	_exit(0);
35     } else {
36 	/*
37 	 * parent process.
38 	 */
39 	close(fds[1]);
40 	write(fds[0], c, sizeof(c));	/* prime the caches */
41 	read(fds[0], c, sizeof(c));
42 
43 	start_timing();
44 	for (j = 0; ; ++j) {
45 	    write(fds[0], c, sizeof(c));
46 	    if (read(fds[0], c, sizeof(c)) != sizeof(c)) {
47 		fprintf(stderr, "broken pipe during test\n");
48 		exit(1);
49 	    }
50 	   if ((j & 31) == 0 && stop_timing(0, NULL))
51 		break;
52 	}
53 	loops = j;
54 
55 	start_timing();
56 	for (j = 0; j < loops; ++j) {
57 	    write(fds[0], c, sizeof(c));
58 	    if (read(fds[0], c, sizeof(c)) != sizeof(c)) {
59 		fprintf(stderr, "broken pipe during test\n");
60 		exit(1);
61 	    }
62 	}
63 	stop_timing(j, "full duplex pipe / 1char:");
64 	close(fds[0]);
65 	while(wait(NULL) >= 0);
66     }
67     return(0);
68 }
69 
70