1 /* $OpenBSD: select.c,v 1.2 2001/09/20 16:43:15 todd 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 <stdlib.h> 48 #include "test.h" 49 50 #define NLOOPS 10000 51 52 int ntouts = 0; 53 54 void * 55 bg_routine(arg) 56 void *arg; 57 { 58 char dot = '.'; 59 int n; 60 61 SET_NAME("bg"); 62 63 /* Busy loop, printing dots */ 64 for (;;) { 65 pthread_yield(); 66 write(STDOUT_FILENO, &dot, sizeof dot); 67 pthread_yield(); 68 n = NLOOPS; 69 while (n-- > 0) 70 pthread_yield(); 71 } 72 } 73 74 void * 75 fg_routine(arg) 76 void *arg; 77 { 78 int flags; 79 int n; 80 fd_set r; 81 int fd = fileno((FILE *) arg); 82 int tty = isatty(fd); 83 int maxfd; 84 int nb; 85 char buf[128]; 86 87 SET_NAME("fg"); 88 89 /* Set the file descriptor to non-blocking */ 90 flags = fcntl(fd, F_GETFL); 91 CHECKr(fcntl(fd, F_SETFL, flags | O_NONBLOCK)); 92 93 for (;;) { 94 95 /* Print a prompt if it's a tty: */ 96 if (tty) { 97 printf("type something> "); 98 fflush(stdout); 99 } 100 101 /* Select on the fdesc: */ 102 FD_ZERO(&r); 103 FD_SET(fd, &r); 104 maxfd = fd; 105 errno = 0; 106 CHECKe(n = select(maxfd + 1, &r, (fd_set *) 0, (fd_set *) 0, 107 (struct timeval *) 0)); 108 109 if (n > 0) { 110 /* Something was ready for read. */ 111 printf("select returned %d\n", n); 112 while ((nb = read(fd, buf, sizeof(buf) - 1)) > 0) { 113 printf("read %d: `%.*s'\n", nb, nb, buf); 114 } 115 printf("last read was %d, errno = %d %s\n", nb, errno, 116 errno == 0 ? "success" : strerror(errno)); 117 if (nb < 0) 118 ASSERTe(errno, == EWOULDBLOCK || 119 errno == EAGAIN); 120 if (nb == 0) 121 break; 122 } else 123 ntouts++; 124 } 125 printf("read finished\n"); 126 return (NULL); 127 } 128 129 int 130 main(argc, argv) 131 int argc; 132 char *argv[]; 133 { 134 pthread_t bg_thread, fg_thread; 135 FILE * slpr; 136 137 /* Create a fdesc that will block for a while on read: */ 138 CHECKn(slpr = popen("sleep 2; echo foo", "r")); 139 140 /* Create a busy loop thread that yields a lot: */ 141 CHECKr(pthread_create(&bg_thread, NULL, bg_routine, 0)); 142 143 /* Create the thread that reads the fdesc: */ 144 CHECKr(pthread_create(&fg_thread, NULL, fg_routine, (void *) slpr)); 145 146 /* Wait for the reader thread to finish */ 147 CHECKr(pthread_join(fg_thread, NULL)); 148 149 /* Clean up*/ 150 CHECKe(pclose(slpr)); 151 152 SUCCEED; 153 } 154