From ba711deb0de9caf64e625a5da9130be0a3967266 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sun, 28 Apr 2019 09:27:07 -0600 Subject: [PATCH] Convert select example to use pipes --- tests/select.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/select.c b/tests/select.c index a367b4ce16..25326a7512 100644 --- a/tests/select.c +++ b/tests/select.c @@ -6,19 +6,37 @@ #include "test_helpers.h" int main(void) { - int fd = open("select.c", 0, 0); + int pipefd[2]; + if (pipe2(pipefd, O_NONBLOCK) < 0) { + perror("pipe"); + return 1; + } + + char c = 'c'; + if (write(pipefd[1], &c, sizeof(c)) < 0) { + perror("write"); + return 1; + } fd_set read; FD_ZERO(&read); - FD_SET(fd, &read); + FD_SET(pipefd[0], &read); - printf("Is set before? %d\n", FD_ISSET(fd, &read)); + printf("Is set before? %d\n", FD_ISSET(pipefd[0], &read)); // This should actually test TCP streams and stuff, but for now I'm simply // testing whether it ever returns or not. - printf("Amount of things ready: %d\n", select(fd + 1, &read, NULL, NULL, NULL)); + int nfds = select(pipefd[0] + 1, &read, NULL, NULL, NULL); + if (nfds < 0) { + perror("select"); + return 1; + } + printf("Amount of things ready: %d\n", nfds); - printf("Is set after? %d\n", FD_ISSET(fd, &read)); + printf("Is set after? %d\n", FD_ISSET(pipefd[0], &read)); - close(fd); + close(pipefd[0]); + close(pipefd[1]); + + return 0; }