diff options
author | David Leonard <d@cvs.openbsd.org> | 1999-11-28 12:31:43 +0000 |
---|---|---|
committer | David Leonard <d@cvs.openbsd.org> | 1999-11-28 12:31:43 +0000 |
commit | 55924dfd481a6c15f6f5f98e420ea1c876245174 (patch) | |
tree | 3d282edf781bb0869a2b7f4b66bd0a99443d1abe | |
parent | c52e0a8ed2a62ba1108e6129989a169a8de5436c (diff) |
clean up
-rw-r--r-- | lib/libc_r/TEST/test_select.c | 151 |
1 files changed, 86 insertions, 65 deletions
diff --git a/lib/libc_r/TEST/test_select.c b/lib/libc_r/TEST/test_select.c index 4202a06439c..c210504610d 100644 --- a/lib/libc_r/TEST/test_select.c +++ b/lib/libc_r/TEST/test_select.c @@ -1,3 +1,8 @@ +/* $OpenBSD: test_select.c,v 1.4 1999/11/28 12:31:42 d Exp $ */ +/* + * Rudimentary test of select(). + */ + #include <pthread.h> #include <pthread_np.h> #include <stdio.h> @@ -7,7 +12,8 @@ #include <errno.h> #include <unistd.h> #include "test.h" -#define NLOOPS 1000 + +#define NLOOPS 10000 int ntouts = 0; @@ -15,84 +21,99 @@ void * bg_routine(arg) void *arg; { - char dot = '.'; - SET_NAME("bg"); - write(STDOUT_FILENO,"bg routine running\n",19); - /*pthread_dump_state();*/ - while (1) { - int n; - pthread_yield(); - write(STDOUT_FILENO, &dot, sizeof dot); - pthread_yield(); - n = NLOOPS; - while (n-- > 0) - pthread_yield(); - } + char dot = '.'; + int n; + + SET_NAME("bg"); + + /* Busy loop, printing dots */ + for (;;) { + pthread_yield(); + write(STDOUT_FILENO, &dot, sizeof dot); + pthread_yield(); + n = NLOOPS; + while (n-- > 0) + pthread_yield(); + } } void * fg_routine(arg) void *arg; { - int flags; - /* static struct timeval tout = { 0, 500000 }; */ - int n; - fd_set r; - - SET_NAME("fg"); - - flags = fcntl(STDIN_FILENO, F_GETFL); - CHECKr(fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK)); - - while (1) { - int maxfd; - - FD_ZERO(&r); - FD_SET(STDIN_FILENO,&r); - maxfd = STDIN_FILENO; - - errno = 0; - printf("select>"); - CHECKe(n = select(maxfd+1, &r, (fd_set*)0, (fd_set*)0, - (struct timeval *)0)); - - if (n > 0) { - int nb; - char buf[128]; - - printf("=> select returned: %d\n", n); - while ((nb = read(STDIN_FILENO, buf, sizeof(buf)-1)) >= 0) { - buf[nb] = '\0'; - printf("read %d: |%s|\n", nb, buf); - } - printf("=> out of read loop: len = %d / %s\n", nb, strerror(errno)); - if (nb < 0) - ASSERTe(errno, == EWOULDBLOCK || errno == EAGAIN); - } else - ntouts++; - } + int flags; + int n; + fd_set r; + int fd = fileno((FILE *) arg); + int tty = isatty(fd); + int maxfd; + int nb; + char buf[128]; + + SET_NAME("fg"); + + /* Set the file descriptor to non-blocking */ + flags = fcntl(fd, F_GETFL); + CHECKr(fcntl(fd, F_SETFL, flags | O_NONBLOCK)); + + for (;;) { + + /* Print a prompt if it's a tty: */ + if (tty) { + printf("type something> "); + fflush(stdout); + } + + /* Select on the fdesc: */ + FD_ZERO(&r); + FD_SET(fd, &r); + maxfd = fd; + errno = 0; + CHECKe(n = select(maxfd + 1, &r, (fd_set *) 0, (fd_set *) 0, + (struct timeval *) 0)); + + if (n > 0) { + /* Something was ready for read. */ + printf("select returned %d\n", n); + while ((nb = read(fd, buf, sizeof(buf) - 1)) > 0) { + printf("read %d: `%.*s'\n", nb, nb, buf); + } + printf("last read was %d, errno = %d %s\n", nb, errno, + errno == 0 ? "success" : strerror(errno)); + if (nb < 0) + ASSERTe(errno, == EWOULDBLOCK || + errno == EAGAIN); + if (nb == 0) + break; + } else + ntouts++; + } + printf("read finished\n"); + return (NULL); } int main(argc, argv) int argc; - char **argv; + char *argv[]; { - pthread_t bg_thread, fg_thread; - int junk; + pthread_t bg_thread, fg_thread; + FILE * slpr; - setbuf(stdout,NULL); - setbuf(stderr,NULL); + /* Create a fdesc that will block for a while on read: */ + CHECKn(slpr = popen("sleep 2; echo foo", "r")); + /* Create a busy loop thread that yields a lot: */ CHECKr(pthread_create(&bg_thread, NULL, bg_routine, 0)); - CHECKr(pthread_create(&fg_thread, NULL, fg_routine, 0)); - - printf("threads forked: bg=%p fg=%p\n", bg_thread, fg_thread); - /*pthread_dump_state();*/ - /* - printf("initial thread %p joining fg...\n", pthread_self()); - CHECKr(pthread_join(fg_thread, (void **)&junk)); - */ - sleep(20); + + /* Create the thread that reads the fdesc: */ + CHECKr(pthread_create(&fg_thread, NULL, fg_routine, (void *) slpr)); + + /* Wait for the reader thread to finish */ + CHECKr(pthread_join(fg_thread, NULL)); + + /* Clean up*/ + CHECKe(pclose(slpr)); + SUCCEED; } |