GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

Select (Unix)

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 . The select system call is similar to the newer 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 , /dev/poll, and I/O completion ports. One common use of select outside of its stated use of waiting on filehandles is to implement a portable sub-second sleep. This can be achieved by passing NULL for all three fd_set arguments, and the duration of... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

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 . The select system call is similar to the newer 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 , /dev/poll, and I/O completion ports. One common use of select outside of its stated use of waiting on filehandles is to implement a portable sub-second sleep. This can be achieved by passing NULL for all three fd_set arguments, and the duration of the desired sleep as the timeout argument. In the C programming language, the select system call is declared in the header file sys/select.h or unistd.h, and has the following syntax: int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds, struct timeval* timeout); fd_set type arguments may be manipulated with four utility macros: , and . Select returns the total number of bits set in and , or zero if the timeout expired, and -1 on error. The sets of file descriptor used in select are finite in size, depending on the operating system. The newer system call provides a more flexible solution. 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== Berkeley sockets Polling epoll kqueue Input/output completion port (IOCP) ==External links==

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Select (Unix)". 1764723696. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1325434559

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·