Increasing limit of FD_SETSIZE and select

前端 未结 5 1076
长情又很酷
长情又很酷 2020-11-27 16:38

I want to increase FD_SETSIZE macro value for my system. Is there any way to increase FD_SETSIZE so select will not fail

5条回答
  •  自闭症患者
    2020-11-27 17:28

    It would be better (and easy) to replace with poll. Generally poll() is a simple drop-in replacement for select() and isn't limited by the 1024 of FD_SETSIZE...

    fd_set fd_read;
    int id = 42;
    FD_ZERO(fd_read);
    FD_SET(id, &fd_read);
    struct timeval tv;
    tv.tv_sec = 5;
    tv.tv_usec = 0;
    if (select(id + 1, &fd_read, NULL, NULL, &tv) != 1) {
       // Error.
    }
    

    becomes:

    struct pollfd pfd_read;
    int id = 42;
    int timeout = 5000;
    pfd_read.fd = id;
    pfd_read.events = POLLIN;
    if (poll(&pfd_read, 1, timeout) != 1) {
       // Error
    }
    

    You need to include poll.h for the pollfd structure.

    If you need to write as well as read then set the events flag as POLLIN | POLLOUT.

提交回复
热议问题