1 /* $OpenBSD: select.c,v 1.1.1.1 2001/08/15 14:37:13 fgsch Exp $ */ 2 /* 3 * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors, 4 * proven@mit.edu All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Chris Provenzano, 17 * the University of California, Berkeley, and contributors. 18 * 4. Neither the name of Chris Provenzano, the University, nor the names of 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR 26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Rudimentary test of select(). 37 */ 38 39 #include <pthread.h> 40 #include <pthread_np.h> 41 #include <stdio.h> 42 #include <sys/fcntl.h> 43 #include <sys/types.h> 44 #include <sys/time.h> 45 #include <errno.h> 46 #include <unistd.h> 47 #include "test.h" 48 49 #define NLOOPS 10000 50 51 int ntouts = 0; 52 53 void * 54 bg_routine(arg) 55 void *arg; 56 { 57 char dot = '.'; 58 int n; 59 60 SET_NAME("bg"); 61 62 /* Busy loop, printing dots */ 63 for (;;) { 64 pthread_yield(); 65 write(STDOUT_FILENO, &dot, sizeof dot); 66 pthread_yield(); 67 n = NLOOPS; 68 while (n-- > 0) 69 pthread_yield(); 70 } 71 } 72 73 void * 74 fg_routine(arg) 75 void *arg; 76 { 77 int flags; 78 int n; 79 fd_set r; 80 int fd = fileno((FILE *) arg); 81 int tty = isatty(fd); 82 int maxfd; 83 int nb; 84 char buf[128]; 85 86 SET_NAME("fg"); 87 88 /* Set the file descriptor to non-blocking */ 89 flags = fcntl(fd, F_GETFL); 90 CHECKr(fcntl(fd, F_SETFL, flags | O_NONBLOCK)); 91 92 for (;;) { 93 94 /* Print a prompt if it's a tty: */ 95 if (tty) { 96 printf("type something> "); 97 fflush(stdout); 98 } 99 100 /* Select on the fdesc: */ 101 FD_ZERO(&r); 102 FD_SET(fd, &r); 103 maxfd = fd; 104 errno = 0; 105 CHECKe(n = select(maxfd + 1, &r, (fd_set *) 0, (fd_set *) 0, 106 (struct timeval *) 0)); 107 108 if (n > 0) { 109 /* Something was ready for read. */ 110 printf("select returned %d\n", n); 111 while ((nb = read(fd, buf, sizeof(buf) - 1)) > 0) { 112 printf("read %d: `%.*s'\n", nb, nb, buf); 113 } 114 printf("last read was %d, errno = %d %s\n", nb, errno, 115 errno == 0 ? "success" : strerror(errno)); 116 if (nb < 0) 117 ASSERTe(errno, == EWOULDBLOCK || 118 errno == EAGAIN); 119 if (nb == 0) 120 break; 121 } else 122 ntouts++; 123 } 124 printf("read finished\n"); 125 return (NULL); 126 } 127 128 int 129 main(argc, argv) 130 int argc; 131 char *argv[]; 132 { 133 pthread_t bg_thread, fg_thread; 134 FILE * slpr; 135 136 /* Create a fdesc that will block for a while on read: */ 137 CHECKn(slpr = popen("sleep 2; echo foo", "r")); 138 139 /* Create a busy loop thread that yields a lot: */ 140 CHECKr(pthread_create(&bg_thread, NULL, bg_routine, 0)); 141 142 /* Create the thread that reads the fdesc: */ 143 CHECKr(pthread_create(&fg_thread, NULL, fg_routine, (void *) slpr)); 144 145 /* Wait for the reader thread to finish */ 146 CHECKr(pthread_join(fg_thread, NULL)); 147 148 /* Clean up*/ 149 CHECKe(pclose(slpr)); 150 151 SUCCEED; 152 } 153