/* test for using select system call, signal handling and timeouts */ #include #include #include #include #include #include #include #include "closure.h" void set_signal_handlers(void); void play_with_select(void); fd_set fd_read; fd_set fd_write; fd_set fd_error; int n_fd; int n_fd_capacity; closure *read_h_by_fd; closure *write_h_by_fd; void set_read_h(int fd) { if (fd > n_fd_capacity) { read } if (fd > n_fd) { } } int main(int argc, char *argv[]) { set_signal_handlers(); play_with_select(); return 0; } void sigint_h(int i) { printf("handled SIGINT: %d\n", i); } void set_signal_handlers(void) { struct sigaction sa; sa.sa_handler = &sigint_h; sigemptyset(&sa.sa_mask); // check for -1 rv? sa.sa_flags = 0; //SA_RESTART; sigaction(SIGINT, &sa, NULL); // check for -1 rv? } void play_with_select(void) { fd_set o_fd_read, o_fd_write, o_fd_error; int n; struct timeval select_timeout; FD_ZERO(&fd_read); FD_ZERO(&fd_write); FD_ZERO(&fd_error); fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); fcntl(STDOUT_FILENO, F_SETFL, O_NONBLOCK); FD_SET(STDIN_FILENO, &fd_read); // FD_SET(STDOUT_FILENO, &fd_write); n_fd = 2; o_fd_read = fd_read; o_fd_write = fd_write; o_fd_error = fd_error; select_timeout.tv_sec = 5; select_timeout.tv_usec = 0; // select - this is Linux specific select usage, relying on the timeout being set to the remaining time do { n = select(fd_limit, &o_fd_read, &o_fd_write, &o_fd_error, &select_timeout); } while (n == -1 && errno == EINTR); if (n == -1) { if (errno == EINTR) perror("select failed"); printf("time remaining: %d %d\n", select_timeout.tv_sec, select_timeout.tv_usec); exit(1); } }