Select (Unix)
Get Select Unix essential facts below. View Videos or join the Select Unix discussion. Add Select Unix to your topic list for future reference or share this resource on social media.
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.[1] The select system call is similar to the 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, epoll, /dev/poll and I/O completion ports.[2]

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);
argument description
nfds This is an integer one more than the maximum of any file descriptor in any of the sets. In other words, while adding file descriptors to each of the sets, you must calculate the maximum integer value of all of them, then increment this value by one, and then pass this as nfds.
readfds fd_set type holding the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read. Can be NULL.
writefds fd_set type holding the file descriptors to be checked for being ready to write, and on output indicates which file descriptors are ready to write. Can be NULL.
errorfds fd_set type holding the file descriptors to be checked for error conditions pending, and on output indicates which file descriptors have error conditions pending. Can be NULL.
timeout structure of type struct timeval that specifies a maximum interval to wait for the selection to complete. If the timeout argument points to an object of type struct timeval whose members are 0, select does not block. If the timeout argument is NULL, select blocks until an event causes one of the masks to be returned with a valid (non-zero) value. Linux will update the timeout in place to indicate how much time was elapsed, though this behavior is not shared by most other Unix systems.

fd_set type arguments may be manipulated with four utility macros: FD_SET, FD_CLR, FD_ZERO, and FD_ISSET.

Select returns the total number of bits set in readfds, writefds and errorfds, 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 poll provides a more flexible solution.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include <sys/select.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <errno.h>

#define PORT "9421"

/* function prototypes */
void die(const char*);

int main(int argc, char **argv)
    int sockfd, new, maxfd, on = 1, nready, i;

    struct addrinfo *res0, *res, hints;

    char buffer[BUFSIZ];

    fd_set master, readfds;

    int error;

    ssize_t nbytes;

    (void)memset(&hints, '\0', sizeof(struct addrinfo));

    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    if (0 != (error = getaddrinfo(NULL, PORT, &hints, &res0)))
        errx(EXIT_FAILURE, "%s", gai_strerror(error));

    for (res = res0; res; res = res->ai_next)
        if (-1 == (sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)))

        if (-1 == (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(int))))

        if (-1 == (bind(sockfd, res->ai_addr, res->ai_addrlen)))



    if (-1 == sockfd)


    if (-1 == (listen(sockfd, 32)))

    if (-1 == (fcntl(sockfd, F_SETFD, O_NONBLOCK)))


    FD_SET(sockfd, &master);

    maxfd = sockfd;

    while (1)
        memcpy(&readfds, &master, sizeof(master));

        (void)printf("running select\n");

        if (-1 == (nready = select(maxfd+1, &readfds, NULL, NULL, NULL)))

        (void)printf("Number of ready descriptor: %d\n", nready);

        for (i=0; i<=maxfd && nready>0; i++)
            if (FD_ISSET(i, &readfds))

                if (i == sockfd)
                    (void)printf("Trying to accept new connection(s)\n");

                    if (-1 == (new = accept(sockfd, NULL, NULL)))
                        if (EWOULDBLOCK != errno)



                        if (-1 == (fcntl(new, F_SETFD, O_NONBLOCK)))

                        FD_SET(new, &master);

                        if (maxfd < new)
                            maxfd = new;

                    (void)printf("recv data from one of descriptors(s)\n");

                    nbytes = recv(i, buffer, sizeof(buffer), 0);
                    if (nbytes <= 0)
                        if (EWOULDBLOCK != errno)


                    buffer[nbytes] = '\0';
                    printf("%s", buffer);

                    (void)printf("%zi bytes received.\n", nbytes);

                    FD_CLR(i, &master);



    return 0;

void die(const char *msg)

See also


  1. ^ Computer Systems Research Group (1994). "select, pselect -- synchronous I/O multiplexing". BSD Cross Reference. NetBSD. Lay summary.
  2. ^ "Connection processing methods".

External links

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.



Music Scenes