select.c revision 1.1 1 1.1 yamt /* $NetBSD: select.c,v 1.1 2008/03/21 12:27:12 yamt Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.1 yamt * Copyright (c)2008 YAMAMOTO Takashi,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt */
28 1.1 yamt
29 1.1 yamt #define FD_SETSIZE 65536
30 1.1 yamt #include <sys/select.h>
31 1.1 yamt
32 1.1 yamt #include <errno.h>
33 1.1 yamt #include <fcntl.h>
34 1.1 yamt #include <pthread.h>
35 1.1 yamt #include <stdio.h>
36 1.1 yamt #include <stdlib.h>
37 1.1 yamt #include <string.h>
38 1.1 yamt #include <unistd.h>
39 1.1 yamt
40 1.1 yamt #define NPIPE 128
41 1.1 yamt #define NTHREAD 64
42 1.1 yamt #define VERBOSE 0
43 1.1 yamt
44 1.1 yamt #if !defined(RANDOM_MAX)
45 1.1 yamt #define RANDOM_MAX ((1UL << 31) - 1)
46 1.1 yamt #endif
47 1.1 yamt
48 1.1 yamt int fds[NPIPE][2];
49 1.1 yamt
50 1.1 yamt pthread_mutex_t count_lock;
51 1.1 yamt int count;
52 1.1 yamt
53 1.1 yamt static void
54 1.1 yamt dowrite(void)
55 1.1 yamt {
56 1.1 yamt char buf[1];
57 1.1 yamt int fd;
58 1.1 yamt int i;
59 1.1 yamt
60 1.1 yamt i = random() % NPIPE;
61 1.1 yamt fd = fds[i][1];
62 1.1 yamt #if VERBOSE
63 1.1 yamt printf("[%p] write %d\n", (void *)pthread_self(), fd);
64 1.1 yamt #endif
65 1.1 yamt if (write(fd, buf, sizeof(buf)) == -1) {
66 1.1 yamt perror("write");
67 1.1 yamt abort();
68 1.1 yamt }
69 1.1 yamt }
70 1.1 yamt
71 1.1 yamt static void *
72 1.1 yamt f(void *dummy)
73 1.1 yamt {
74 1.1 yamt
75 1.1 yamt for (;;) {
76 1.1 yamt struct timeval to;
77 1.1 yamt fd_set oset;
78 1.1 yamt fd_set set;
79 1.1 yamt int maxfd = -1;
80 1.1 yamt int nfd = 0;
81 1.1 yamt int ret;
82 1.1 yamt int fd;
83 1.1 yamt int i;
84 1.1 yamt
85 1.1 yamt FD_ZERO(&set);
86 1.1 yamt do {
87 1.1 yamt for (i = 0; i < NPIPE; i++) {
88 1.1 yamt fd = fds[i][0];
89 1.1 yamt if (fd > FD_SETSIZE) {
90 1.1 yamt fprintf(stderr,
91 1.1 yamt "fd(%d) > FD_SETSIZE(%d)\n",
92 1.1 yamt fd, FD_SETSIZE);
93 1.1 yamt abort();
94 1.1 yamt }
95 1.1 yamt if (random() & 1) {
96 1.1 yamt FD_SET(fd, &set);
97 1.1 yamt if (fd > maxfd) {
98 1.1 yamt maxfd = fd;
99 1.1 yamt nfd++;
100 1.1 yamt }
101 1.1 yamt }
102 1.1 yamt }
103 1.1 yamt } while (nfd == 0);
104 1.1 yamt memcpy(&oset, &set, sizeof(oset));
105 1.1 yamt memset(&to, 0, sizeof(to));
106 1.1 yamt to.tv_sec = random() % 10;
107 1.1 yamt to.tv_usec = random() % 1000000;
108 1.1 yamt #if VERBOSE
109 1.1 yamt printf("[%p] select start to=%lu\n", (void *)pthread_self(),
110 1.1 yamt (unsigned long)to.tv_sec);
111 1.1 yamt #endif
112 1.1 yamt ret = select(maxfd + 1, &set, NULL, NULL, &to);
113 1.1 yamt #if VERBOSE
114 1.1 yamt printf("[%p] select done ret=%d\n",
115 1.1 yamt (void *)pthread_self(), ret);
116 1.1 yamt #endif
117 1.1 yamt if (ret == -1) {
118 1.1 yamt perror("select");
119 1.1 yamt abort();
120 1.1 yamt }
121 1.1 yamt if (ret > nfd) {
122 1.1 yamt fprintf(stderr, "[%p] unexpected return value %d\n",
123 1.1 yamt (void *)pthread_self(), ret);
124 1.1 yamt abort();
125 1.1 yamt }
126 1.1 yamt nfd = 0;
127 1.1 yamt for (fd = 0; fd <= maxfd; fd++) {
128 1.1 yamt if (FD_ISSET(fd, &set)) {
129 1.1 yamt char buf[1];
130 1.1 yamt
131 1.1 yamt #if VERBOSE
132 1.1 yamt printf("[%p] read %d\n",
133 1.1 yamt (void *)pthread_self(), fd);
134 1.1 yamt #endif
135 1.1 yamt if (!FD_ISSET(fd, &oset)) {
136 1.1 yamt fprintf(stderr, "[%p] unexpected\n",
137 1.1 yamt (void *)pthread_self());
138 1.1 yamt abort();
139 1.1 yamt }
140 1.1 yamt if (read(fd, buf, sizeof(buf)) == -1) {
141 1.1 yamt if (errno != EAGAIN) {
142 1.1 yamt perror("read");
143 1.1 yamt abort();
144 1.1 yamt }
145 1.1 yamt } else {
146 1.1 yamt dowrite();
147 1.1 yamt pthread_mutex_lock(&count_lock);
148 1.1 yamt count++;
149 1.1 yamt pthread_mutex_unlock(&count_lock);
150 1.1 yamt }
151 1.1 yamt nfd++;
152 1.1 yamt }
153 1.1 yamt }
154 1.1 yamt if (ret != nfd) {
155 1.1 yamt fprintf(stderr, "[%p] ret(%d) != nfd(%d)\n",
156 1.1 yamt (void *)pthread_self(), ret, nfd);
157 1.1 yamt abort();
158 1.1 yamt }
159 1.1 yamt }
160 1.1 yamt }
161 1.1 yamt
162 1.1 yamt int
163 1.1 yamt main(int argc, char *argv[])
164 1.1 yamt {
165 1.1 yamt pthread_t pt[NTHREAD];
166 1.1 yamt int i;
167 1.1 yamt unsigned int secs;
168 1.1 yamt
169 1.1 yamt secs = atoi(argv[1]);
170 1.1 yamt
171 1.1 yamt for (i = 0; i < NPIPE; i++) {
172 1.1 yamt if (pipe(fds[i])) {
173 1.1 yamt perror("pipe");
174 1.1 yamt abort();
175 1.1 yamt }
176 1.1 yamt if (fcntl(fds[i][0], F_SETFL, O_NONBLOCK) == -1) {
177 1.1 yamt perror("fcntl");
178 1.1 yamt abort();
179 1.1 yamt }
180 1.1 yamt }
181 1.1 yamt pthread_mutex_init(&count_lock, NULL);
182 1.1 yamt for (i = 0; i < NTHREAD; i++) {
183 1.1 yamt int error = pthread_create(&pt[i], NULL, f, NULL);
184 1.1 yamt if (error) {
185 1.1 yamt errno = error;
186 1.1 yamt perror("pthread_create");
187 1.1 yamt abort();
188 1.1 yamt }
189 1.1 yamt }
190 1.1 yamt dowrite();
191 1.1 yamt dowrite();
192 1.1 yamt dowrite();
193 1.1 yamt dowrite();
194 1.1 yamt
195 1.1 yamt sleep(secs);
196 1.1 yamt printf("%u / %u = %lf\n", count, secs, (double)count / secs);
197 1.1 yamt exit(EXIT_SUCCESS);
198 1.1 yamt }
199