What data structure is behind FD_SET and FD_ISSET when working with sockets in c?

13,031

Solution 1

Prototypes:

void FD_SET(int fd, fd_set* fdset);
int FD_ISSET(int fd, fd_set* fdset);

From sys/select.h

typedef struct fd_set {
  u_int  fd_count;
  SOCKET fd_array[FD_SETSIZE];
} fd_set;

Solution 2

I seem to recall it's just a bitmask. An array of chars (or some other basic type) where each bit of the char represents the state of each file descriptor.

Some implementations also have a limit variable if they allow variable sized structures but most that I've seen (and these are generally the older ones) simply allow for the largest number of file descriptors.

However, an implementation is free to use whatever data structure it wants as long as it provides the FD_* macros or functions to properly initialize and change them.

Share:
13,031
FlinkmanSV
Author by

FlinkmanSV

https://bitcoinsv.academy/ https://bsvquickstart.com/ https://www.youtube.com/watch?v=3MJSEGnpgB8 https://www.reddit.com/r/bitcoincashSV/

Updated on June 07, 2022

Comments

  • FlinkmanSV
    FlinkmanSV almost 2 years

    What data structure is behind FD_SET and FD_ISSET macro when working with sockets?

  • FlinkmanSV
    FlinkmanSV about 15 years
    ok, Nice. I see the answer. FD_SETSIZE! That is how many listening sockets I can have. (*8)
  • danyamachine
    danyamachine over 5 years
    note that the names of the members of the struct fd_set differ between implementations