select - should I use FD_SETSIZE or max fd

This is a discussion on select - should I use FD_SETSIZE or max fd within the Linux Programming forums, part of the Platform Specific Boards category; Hello group, The man pages say that the first parameter to select (nfds) should be "an integer one more than ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    select - should I use FD_SETSIZE or max fd

    Hello group,

    The man pages say that the first parameter to select (nfds) should be "an integer one more than the maximum of any file descriptor in any of the sets", but I often see the code that uses FD_SETSIZE like this:

    Code:
      ret = select(FD_SETSIZE, ...);
    instead of calculated max value like this:

    Code:
      int nfds = 0;
      ...
      nfds = max(nfds, fd1);
      nfds = max(nfds, fd2);
      ..
      ret = select(nfds, ...);
    Does this code (with FD_SETSIZE) work, or does it just accidentally work?

    example with max(): http://www.linuxmanpages.com/man2/select_tut.2.php
    example with FD_SETSIZE: http://www.delorie.com/gnu/docs/glibc/libc_248.html

    Which one is correct?

    Thanks,
    Tvrtko

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    FD_SETSIZE seems like a lazy programmer cop-out which would cause extra work for the machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    On my machine FD_SETSIZE equals 1024. What if file descriptor is greater than 1024 and I use FD_SETSIZE? It doesn't seem as lazy anymore, but as incorrect.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > What if file descriptor is greater than 1024 and I use FD_SETSIZE?
    It can't be larger than FD_SETSIZE. The fd_set must be capable of representing every valid descriptor.

    So if you're only checking a few descriptors, you're checking over 1000 other descriptors which can never satisfy the select call, but you still make the OS loop through them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Thanks. I checked this by opening more than 1024 files and it failed after it returned file descriptor 1023. Now it makes sense because I assumed existence of descriptors greater than 1024 and generally random numbers but as it turns out it is not the case: "The file descriptor returned by a successful call [to open] will be the lowest-numbered file descriptor not currently open for the process."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  3. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  5. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21