SELECT_TUT(2) System Calls Manual SELECT_TUT(2) select, pselect - - LIBRARY Standard C library (libc, -lc) select(2) The select() and pselect() system calls are used to efficiently monitor multiple file descriptors, to see if any of them is, or becomes, "ready"; that is, to see whether I/O becomes possible, or an "exceptional condition" has occurred on any of the file descriptors. This page provides background and tutorial information on the use of these system calls. For details of the arguments and semantics of select() and pselect(), see select(2). pselect() is useful if you are waiting for a signal as well as for file descriptor(s) to become ready for I/O. Programs that receive signals normally use the signal handler only to raise a global flag. The global flag will indicate that the event must be processed in the main loop of the program. A signal will cause the select() (or pselect()) call to return with errno set to EINTR. This behavior is essential so that signals can be processed in the main loop of the program, otherwise select() would block indefinitely. Now, somewhere in the main loop will be a conditional to check the global flag. So we must ask: what if a signal arrives after the conditional, but before the select() call? The answer is that select() would block indefinitely, even though an event is actually pending. This race condition is solved by the pselect() call. This call can be used to set the signal mask to a set of signals that are to be received only within the pselect() call. For instance, let us say that the event in question was the exit of a child process. Before the start of the main loop, we would block SIGCHLD using sigprocmask(2). Our pselect() call would enable SIGCHLD by using an empty signal mask. Our program would look like: static volatile sig_atomic_t got_SIGCHLD = 0; static void child_sig_handler(int sig) { got_SIGCHLD = 1; } int main(int argc, char *argv[]) { sigset_t sigmask, empty_mask; struct sigaction sa; fd_set readfds, writefds, exceptfds; int r; sigemptyset(&sigmask); sigaddset(&sigmask, SIGCHLD); if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) { perror("sigprocmask"); exit(EXIT_FAILURE); } sa.sa_flags = 0; sa.sa_handler = child_sig_handler; sigemptyset(&sa.sa_mask); if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("sigaction"); exit(EXIT_FAILURE); } sigemptyset(&empty_mask); for (;;) { /* main loop */ /* Initialize readfds, writefds, and exceptfds before the pselect() call. (Code omitted.) */ r = pselect(nfds, &readfds, &writefds, &exceptfds, NULL, &empty_mask); if (r == -1 && errno != EINTR) { /* Handle error */ } if (got_SIGCHLD) { got_SIGCHLD = 0; /* Handle signalled event here; e.g., wait() for all terminated children. (Code omitted.) */ } /* main body of program */ } } , select()? ? select() , , . UNIX , - . read(2) write(2), , / , , . select() . , select(), , , . , , , . , select(), , select(). 1. select() . , . , , . 2. nfds , . 3. , select() . . 4. select() . 5. read(2), recv(2), write(2) send(2) / . , , , . , / . 6. / , , . / , . 1024 , . 7. Calls to read(2), recv(2), write(2), send(2), and select() can fail with the error EINTR, and calls to read(2), recv(2), write(2), and send(2) can fail with errno set to EAGAIN (EWOULDBLOCK). These results must be properly managed (not done properly above). If your program is not going to receive any signals, then it is unlikely you will get EINTR. If your program does not set nonblocking I/O, you will not get EAGAIN. 8. read(2), recv(2), write(2) send(2) . 9. read(2), recv(2), write(2) send(2) , 7. 0, , select() . -1 . 10. select(), . pselect() . 11. select() , . select(2). , , , select(). select() , , , IPC, , . poll(2) , select() . , select(). Linux epoll(7) select(2) poll(2). , select(). TCP . #include #include #include #include #include #include #include #include #include #include static int forward_port; #undef max #define max(x, y) ((x) > (y) ? (x) : (y)) static int listen_socket(int listen_port) { int lfd; int yes; struct sockaddr_in addr; lfd = socket(AF_INET, SOCK_STREAM, 0); if (lfd == -1) { perror("socket"); return -1; } yes = 1; if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) { perror("setsockopt"); close(lfd); return -1; } memset(&addr, 0, sizeof(addr)); addr.sin_port = htons(listen_port); addr.sin_family = AF_INET; if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { perror("bind"); close(lfd); return -1; } printf("accepting connections on port %d\n", listen_port); listen(lfd, 10); return lfd; } static int connect_socket(int connect_port, char *address) { int cfd; struct sockaddr_in addr; cfd = socket(AF_INET, SOCK_STREAM, 0); if (cfd == -1) { perror("socket"); return -1; } memset(&addr, 0, sizeof(addr)); addr.sin_port = htons(connect_port); addr.sin_family = AF_INET; if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) { fprintf(stderr, "inet_aton(): bad IP address format\n"); close(cfd); return -1; } if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { perror("connect()"); shutdown(cfd, SHUT_RDWR); close(cfd); return -1; } return cfd; } #define SHUT_FD1 do { \ if (fd1 >= 0) { \ shutdown(fd1, SHUT_RDWR); \ close(fd1); \ fd1 = -1; \ } \ } while (0) #define SHUT_FD2 do { \ if (fd2 >= 0) { \ shutdown(fd2, SHUT_RDWR); \ close(fd2); \ fd2 = -1; \ } \ } while (0) #define BUF_SIZE 1024 int main(int argc, char *argv[]) { int h; int ready, nfds; int fd1 = -1, fd2 = -1; int buf1_avail = 0, buf1_written = 0; int buf2_avail = 0, buf2_written = 0; char buf1[BUF_SIZE], buf2[BUF_SIZE]; fd_set readfds, writefds, exceptfds; ssize_t nbytes; if (argc != 4) { fprintf(stderr, "Usage\n\tfwd " " \n"); exit(EXIT_FAILURE); } signal(SIGPIPE, SIG_IGN); forward_port = atoi(argv[2]); h = listen_socket(atoi(argv[1])); if (h == -1) exit(EXIT_FAILURE); for (;;) { nfds = 0; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); FD_SET(h, &readfds); nfds = max(nfds, h); if (fd1 > 0 && buf1_avail < BUF_SIZE) FD_SET(fd1, &readfds); /* Note: nfds is updated below, when fd1 is added to exceptfds. */ if (fd2 > 0 && buf2_avail < BUF_SIZE) FD_SET(fd2, &readfds); if (fd1 > 0 && buf2_avail - buf2_written > 0) FD_SET(fd1, &writefds); if (fd2 > 0 && buf1_avail - buf1_written > 0) FD_SET(fd2, &writefds); if (fd1 > 0) { FD_SET(fd1, &exceptfds); nfds = max(nfds, fd1); } if (fd2 > 0) { FD_SET(fd2, &exceptfds); nfds = max(nfds, fd2); } ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL); if (ready == -1 && errno == EINTR) continue; if (ready == -1) { perror("select()"); exit(EXIT_FAILURE); } if (FD_ISSET(h, &readfds)) { socklen_t addrlen; struct sockaddr_in client_addr; int fd; addrlen = sizeof(client_addr); memset(&client_addr, 0, addrlen); fd = accept(h, (struct sockaddr *) &client_addr, &addrlen); if (fd == -1) { perror("accept()"); } else { SHUT_FD1; SHUT_FD2; buf1_avail = buf1_written = 0; buf2_avail = buf2_written = 0; fd1 = fd; fd2 = connect_socket(forward_port, argv[3]); if (fd2 == -1) SHUT_FD1; else printf("connect from %s\n", inet_ntoa(client_addr.sin_addr)); /* Skip any events on the old, closed file descriptors. */ continue; } } /* NB: read OOB data before normal reads. */ if (fd1 > 0 && FD_ISSET(fd1, &exceptfds)) { char c; nbytes = recv(fd1, &c, 1, MSG_OOB); if (nbytes < 1) SHUT_FD1; else send(fd2, &c, 1, MSG_OOB); } if (fd2 > 0 && FD_ISSET(fd2, &exceptfds)) { char c; nbytes = recv(fd2, &c, 1, MSG_OOB); if (nbytes < 1) SHUT_FD2; else send(fd1, &c, 1, MSG_OOB); } if (fd1 > 0 && FD_ISSET(fd1, &readfds)) { nbytes = read(fd1, buf1 + buf1_avail, BUF_SIZE - buf1_avail); if (nbytes < 1) SHUT_FD1; else buf1_avail += nbytes; } if (fd2 > 0 && FD_ISSET(fd2, &readfds)) { nbytes = read(fd2, buf2 + buf2_avail, BUF_SIZE - buf2_avail); if (nbytes < 1) SHUT_FD2; else buf2_avail += nbytes; } if (fd1 > 0 && FD_ISSET(fd1, &writefds) && buf2_avail > 0) { nbytes = write(fd1, buf2 + buf2_written, buf2_avail - buf2_written); if (nbytes < 1) SHUT_FD1; else buf2_written += nbytes; } if (fd2 > 0 && FD_ISSET(fd2, &writefds) && buf1_avail > 0) { nbytes = write(fd2, buf1 + buf1_written, buf1_avail - buf1_written); if (nbytes < 1) SHUT_FD2; else buf1_written += nbytes; } /* Check if write data has caught read data. */ if (buf1_written == buf1_avail) buf1_written = buf1_avail = 0; if (buf2_written == buf2_avail) buf2_written = buf2_avail = 0; /* One side has closed the connection, keep writing to the other side until empty. */ if (fd1 < 0 && buf1_avail - buf1_written == 0) SHUT_FD2; if (fd2 < 0 && buf2_avail - buf2_written == 0) SHUT_FD1; } exit(EXIT_SUCCESS); } , TCP, (OOB) , telnet. . , , fork(2) . , . - fcntl(2). - , . The program does not handle more than one simultaneous connection at a time, although it could easily be extended to do this with a linked list of buffers--one for each connection. At the moment, new connections cause the current connection to be dropped. . accept(2), connect(2), poll(2), read(2), recv(2), select(2), send(2), sigprocmask(2), write(2), epoll(7) Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . SELECT_TUT(2)