MarketSelect (Unix)
Company Profile

Select (Unix)

select is a system call and application programming interface (API) in Unix-like and POSIX-compliant operating systems for examining the status of file descriptors of open input/output channels. An enhanced facility with support for signal masking is pselect. The select system call is similar to the newer poll facility introduced in UNIX System V and later operating systems. However, with the C10k problem, both select and poll have been superseded by the likes of kqueue, /dev/poll, epoll and I/O completion ports.

Example
• include • include • include • include • include • include • include • include • include • include • include • include • define PORT "9421" void die(const char* msg) { perror(msg); exit(EXIT_FAILURE); } typedef struct addrinfo AddressInfo; int main(int argc, char* argv[]) { int sockfd; AddressInfo* res0; char buffer[BUFSIZ]; fd_set master; fd_set readfds; int error; ssize_t nbytes; memset(&hints, '\0', sizeof(struct addrinfo)); AddressInfo* hints = { .ai_family = AF_INET, .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP, .ai_flags = AI_PASSIVE }; if (int error = getaddrinfo(NULL, PORT, &hints, &res0)) { errx(EXIT_FAILURE, "%s", gai_strerror(error)); } for (AddressInfo* res = res0; res; res = res->ai_next)) { if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { perror("socket()"); continue; } int on = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(int)) == -1) { perror("setsockopt()"); continue; } if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1) { perror("bind()"); continue; } break; } if (sockfd == -1) { return EXIT_FAILURE; } freeaddrinfo(res0); if (listen(sockfd, 32) == -1) { die("listen()"); } if (fcntl(sockfd, F_SETFD, O_NONBLOCK) == -1) { die("fcntl()"); } FD_ZERO(&master); FD_ZERO(&readfds); FD_SET(sockfd, &master); int maxfd = sockfd; while (true) { memcpy(&readfds, &master, sizeof(master)); printf("running select()\n"); int nready; if (nready = select(maxfd + 1, &readfds, NULL, NULL, NULL) == -1) { die("select()"); } printf("Number of ready descriptor: %d\n", nready); for (int i = 0; i 0; i++) { if (FD_ISSET(i, &readfds)) { nready--; if (i == sockfd) { printf("Trying to accept() new connection(s)\n"); int newval; if ((newval = accept(sockfd, NULL, NULL)) == -1) { if (EWOULDBLOCK != errno) { die("accept()"); } break; } else { if (fcntl(newval, F_SETFD, O_NONBLOCK) == -1) { die("fcntl()"); } FD_SET(newval, &master); if (maxfd ==See also==
tickerdossier.comtickerdossier.substack.com