test select(2).
This commit is contained in:
parent
16ebb0dc79
commit
9ff0d7c13c
|
@ -0,0 +1,17 @@
|
|||
# $NetBSD: Makefile,v 1.1 2008/03/21 12:27:12 yamt Exp $
|
||||
|
||||
NOMAN= # defined
|
||||
|
||||
PROG= select
|
||||
WARNS?= 4
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
regress:
|
||||
@if ${.OBJDIR}/select 5; then \
|
||||
echo "PASSED"; \
|
||||
else \
|
||||
echo "FAILED"; \
|
||||
fi
|
||||
|
||||
.include <bsd.prog.mk>
|
|
@ -0,0 +1,198 @@
|
|||
/* $NetBSD: select.c,v 1.1 2008/03/21 12:27:12 yamt Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2008 YAMAMOTO Takashi,
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#define FD_SETSIZE 65536
|
||||
#include <sys/select.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define NPIPE 128
|
||||
#define NTHREAD 64
|
||||
#define VERBOSE 0
|
||||
|
||||
#if !defined(RANDOM_MAX)
|
||||
#define RANDOM_MAX ((1UL << 31) - 1)
|
||||
#endif
|
||||
|
||||
int fds[NPIPE][2];
|
||||
|
||||
pthread_mutex_t count_lock;
|
||||
int count;
|
||||
|
||||
static void
|
||||
dowrite(void)
|
||||
{
|
||||
char buf[1];
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
i = random() % NPIPE;
|
||||
fd = fds[i][1];
|
||||
#if VERBOSE
|
||||
printf("[%p] write %d\n", (void *)pthread_self(), fd);
|
||||
#endif
|
||||
if (write(fd, buf, sizeof(buf)) == -1) {
|
||||
perror("write");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
static void *
|
||||
f(void *dummy)
|
||||
{
|
||||
|
||||
for (;;) {
|
||||
struct timeval to;
|
||||
fd_set oset;
|
||||
fd_set set;
|
||||
int maxfd = -1;
|
||||
int nfd = 0;
|
||||
int ret;
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
FD_ZERO(&set);
|
||||
do {
|
||||
for (i = 0; i < NPIPE; i++) {
|
||||
fd = fds[i][0];
|
||||
if (fd > FD_SETSIZE) {
|
||||
fprintf(stderr,
|
||||
"fd(%d) > FD_SETSIZE(%d)\n",
|
||||
fd, FD_SETSIZE);
|
||||
abort();
|
||||
}
|
||||
if (random() & 1) {
|
||||
FD_SET(fd, &set);
|
||||
if (fd > maxfd) {
|
||||
maxfd = fd;
|
||||
nfd++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (nfd == 0);
|
||||
memcpy(&oset, &set, sizeof(oset));
|
||||
memset(&to, 0, sizeof(to));
|
||||
to.tv_sec = random() % 10;
|
||||
to.tv_usec = random() % 1000000;
|
||||
#if VERBOSE
|
||||
printf("[%p] select start to=%lu\n", (void *)pthread_self(),
|
||||
(unsigned long)to.tv_sec);
|
||||
#endif
|
||||
ret = select(maxfd + 1, &set, NULL, NULL, &to);
|
||||
#if VERBOSE
|
||||
printf("[%p] select done ret=%d\n",
|
||||
(void *)pthread_self(), ret);
|
||||
#endif
|
||||
if (ret == -1) {
|
||||
perror("select");
|
||||
abort();
|
||||
}
|
||||
if (ret > nfd) {
|
||||
fprintf(stderr, "[%p] unexpected return value %d\n",
|
||||
(void *)pthread_self(), ret);
|
||||
abort();
|
||||
}
|
||||
nfd = 0;
|
||||
for (fd = 0; fd <= maxfd; fd++) {
|
||||
if (FD_ISSET(fd, &set)) {
|
||||
char buf[1];
|
||||
|
||||
#if VERBOSE
|
||||
printf("[%p] read %d\n",
|
||||
(void *)pthread_self(), fd);
|
||||
#endif
|
||||
if (!FD_ISSET(fd, &oset)) {
|
||||
fprintf(stderr, "[%p] unexpected\n",
|
||||
(void *)pthread_self());
|
||||
abort();
|
||||
}
|
||||
if (read(fd, buf, sizeof(buf)) == -1) {
|
||||
if (errno != EAGAIN) {
|
||||
perror("read");
|
||||
abort();
|
||||
}
|
||||
} else {
|
||||
dowrite();
|
||||
pthread_mutex_lock(&count_lock);
|
||||
count++;
|
||||
pthread_mutex_unlock(&count_lock);
|
||||
}
|
||||
nfd++;
|
||||
}
|
||||
}
|
||||
if (ret != nfd) {
|
||||
fprintf(stderr, "[%p] ret(%d) != nfd(%d)\n",
|
||||
(void *)pthread_self(), ret, nfd);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
pthread_t pt[NTHREAD];
|
||||
int i;
|
||||
unsigned int secs;
|
||||
|
||||
secs = atoi(argv[1]);
|
||||
|
||||
for (i = 0; i < NPIPE; i++) {
|
||||
if (pipe(fds[i])) {
|
||||
perror("pipe");
|
||||
abort();
|
||||
}
|
||||
if (fcntl(fds[i][0], F_SETFL, O_NONBLOCK) == -1) {
|
||||
perror("fcntl");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
pthread_mutex_init(&count_lock, NULL);
|
||||
for (i = 0; i < NTHREAD; i++) {
|
||||
int error = pthread_create(&pt[i], NULL, f, NULL);
|
||||
if (error) {
|
||||
errno = error;
|
||||
perror("pthread_create");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
dowrite();
|
||||
dowrite();
|
||||
dowrite();
|
||||
dowrite();
|
||||
|
||||
sleep(secs);
|
||||
printf("%u / %u = %lf\n", count, secs, (double)count / secs);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
Loading…
Reference in New Issue