xref: /haiku/src/tests/system/kernel/select_close_test.cpp (revision b247f935d133a42c427cad8a759a1bf2f65bc290)
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/select.h>
6 #include <unistd.h>
7 
8 #include <OS.h>
9 
10 
11 static status_t
12 close_fd(void* data)
13 {
14 	int fd = *((int*)data);
15 	snooze(1000000);
16 	close(fd);
17 	fprintf(stderr, "fd %d closed\n", fd);
18 	return B_OK;
19 }
20 
21 
22 int
23 main()
24 {
25 	int fd = dup(0);
26 
27 	thread_id thread = spawn_thread(close_fd, "close fd", B_NORMAL_PRIORITY,
28 		&fd);
29 	resume_thread(thread);
30 
31 	fd_set readSet;
32 	FD_ZERO(&readSet);
33 	FD_SET(0, &readSet);
34 	FD_SET(fd, &readSet);
35 
36 	fprintf(stderr, "select({0, %d}, NULL, NULL, NULL) ...\n", fd);
37 	int result = select(fd + 1, &readSet, NULL, NULL, NULL);
38 	fprintf(stderr, "select(): %d\n", result);
39 
40 	fprintf(stderr, "fd %d: %s\n", 0, FD_ISSET(0, &readSet) ? "r" : " ");
41 	fprintf(stderr, "fd %d: %s\n", fd, FD_ISSET(fd, &readSet) ? "r" : " ");
42 
43 	return 0;
44 }
45