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