Created
June 10, 2020 19:38
-
-
Save paulproteus/13df690f99ffed22e2d1048ab3deb523 to your computer and use it in GitHub Desktop.
Select on a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/uio.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/select.h> | |
int main(void) { | |
int read_fd = open("example.txt", O_NONBLOCK | O_RDONLY); | |
int read_fd_nfds = read_fd + 1; | |
fd_set set1; | |
struct timeval timeout; | |
// initialize fd set1 | |
FD_ZERO(&set1); | |
FD_SET(read_fd, &set1); | |
int select_val = 0; | |
select_val = select(read_fd_nfds, &set1, NULL, NULL, NULL); | |
printf("Got %d\n", select_val); | |
printf("Our fd is set? %d\n", FD_ISSET(read_fd, &set1)); | |
// Read nothing from the FD, to satisfy things (?) | |
read(read_fd, NULL, 0); | |
// Hoping this select will block forever; unfortunately, it | |
// immediately succeeds. | |
FD_ZERO(&set1); | |
FD_SET(read_fd, &set1); | |
select_val = select(read_fd_nfds, &set1, NULL, NULL, NULL); | |
printf("Second select: got %d\n", select_val); | |
printf("Our fd is set? %d\n", FD_ISSET(read_fd, &set1)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actual output:
Expected output: