1 /* $NetBSD: select.c,v 1.1 2008/03/21 12:27:12 yamt Exp $ */
4 * Copyright (c)2008 YAMAMOTO Takashi,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #define FD_SETSIZE 65536
30 #include <sys/select.h>
31 #include <sys/atomic.h>
45 #if !defined(RANDOM_MAX)
46 #define RANDOM_MAX ((1UL << 31) - 1)
53 pthread_barrier_t barrier
;
65 printf("[%p] write %d\n", (void *)pthread_self(), fd
);
67 if (write(fd
, buf
, sizeof(buf
)) == -1) {
77 pthread_barrier_wait(&barrier
);
91 for (i
= 0; i
< NPIPE
; i
++) {
93 if (fd
> FD_SETSIZE
) {
95 "fd(%d) > FD_SETSIZE(%d)\n",
108 memcpy(&oset
, &set
, sizeof(oset
));
109 memset(&to
, 0, sizeof(to
));
110 to
.tv_sec
= random() % 10;
111 to
.tv_usec
= random() % 1000000;
113 printf("[%p] select start to=%lu\n", (void *)pthread_self(),
114 (unsigned long)to
.tv_sec
);
116 ret
= select(maxfd
+ 1, &set
, NULL
, NULL
, &to
);
118 printf("[%p] select done ret=%d\n",
119 (void *)pthread_self(), ret
);
126 fprintf(stderr
, "[%p] unexpected return value %d\n",
127 (void *)pthread_self(), ret
);
131 for (fd
= 0; fd
<= maxfd
; fd
++) {
132 if (FD_ISSET(fd
, &set
)) {
136 printf("[%p] read %d\n",
137 (void *)pthread_self(), fd
);
139 if (!FD_ISSET(fd
, &oset
)) {
140 fprintf(stderr
, "[%p] unexpected\n",
141 (void *)pthread_self());
144 if (read(fd
, buf
, sizeof(buf
)) == -1) {
145 if (errno
!= EAGAIN
) {
151 atomic_inc_uint(&count
);
157 fprintf(stderr
, "[%p] ret(%d) != nfd(%d)\n",
158 (void *)pthread_self(), ret
, nfd
);
165 main(int argc
, char *argv
[])
167 pthread_t pt
[NTHREAD
];
171 secs
= atoi(argv
[1]);
173 for (i
= 0; i
< NPIPE
; i
++) {
178 if (fcntl(fds
[i
][0], F_SETFL
, O_NONBLOCK
) == -1) {
183 pthread_barrier_init(&barrier
, NULL
, NTHREAD
+ 1);
184 for (i
= 0; i
< NTHREAD
; i
++) {
185 int error
= pthread_create(&pt
[i
], NULL
, f
, NULL
);
188 perror("pthread_create");
192 pthread_barrier_wait(&barrier
);
199 printf("%u / %u = %lf\n", count
, secs
, (double)count
/ secs
);