I was looking at the IO multiplexing calls for writing an IO library, select() has some macros for setting, clearing the file descriptors in file descriptor sets. While looking a for a method to optimize the walk through fd sets, i looked at the implementation of those macros.
Let’s go through the implementation of FD_SET macro in Linux, other macros are similar.
See both /usr/include/i386-linux-gnu/sys/select.h and /usr/include/i386-linux-gnu/bits/select.h, FD_SET is defined as below.
Other definitions are below.
In a 32 bit Linux system, __FD_SETSIZE is defined as 1024, i.e. usually maximum allowed file descriptors for a process, see “ulimit -a”
See both /usr/include/i386-linux-gnu/sys/typesizes.h and /usr/include/linux/posix_types.h
On 32bit Linux __NFDBITS is 32 since 8sizeof(long int) is 84 i.e. 32.
So we can rewrite the FD_SET macro as below.
fd_set has an array of 32 32bit long ints, so ((set)->fds_bits)[((d)/ 32)] finds array element where new fd falls, then ((__fd_mask) 1 « ((d) % 32))) finds the bit corresponding to new fd in that long int array element.
So FD_SET sets the bit corresponding to new descriptor in a bit mask of size 1024.
Subscribe via RSS