# What is the objective of the first argument to select system call?

From man select

int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);


nfds is the highest possible - phoned number documents descriptor in any one of the 3 collections, plus 1.

What is the objective of nfds, when we currently have readfds, writefds and also exceptfds, where the file descriptors can be established?

0
2019-05-18 22:49:22
Source Share

I do not recognize without a doubt, given that I'm not one of the developers of select (), yet I would certainly claim it is an efficiency optimization. The calling function recognizes the amount of file descriptors it placed in the read, write and also other than FDs, so why should the kernel number it out once more?

Bear in mind that in the very early 80s, when select () obtained presented, they really did not have multi - gigaghertz, multi - cpus to collaborate with. A 25 MHz VAX was rather doggone quickly. And also, you desired select () to function quickly if it could: if some I/O was awaiting the process, why make the process wait?

0
2019-05-21 07:47:58
Source

In "Advanced Programming in the UNIX Environment", W. Richard Stevens claims it is a performance optimization:

By defining the highest possible descriptor we want, the bit can stay clear of experiencing thousand of extra little bits in the 3 descriptor collections, seeking little bits that are activated.

(1st version, web page 399)

If you are doing any kind of sort of UNIX systems shows, the APUE publication is very advised.

An fd_set is generally able to track approximately 1024 file descriptors.

One of the most reliable means to track which fds are set to 0 and also which are readied to 1 would certainly be a bitset, so each fd_set would certainly contain 1024 little bits.

On a 32 - little bit system, a lengthy int (or "word") is 32 little bits, to make sure that suggests each fd_set is
1024/ 32 = 32 words.

If nfds is something tiny, such as 8 or 16, which it would certainly remain in several applications, it just requires to look inside the 1st word, which need to plainly be faster than looking inside all 32.

(See FD_SETSIZE and also __NFDBITS from /usr/include/sys/select.h for the values on your system.)

UPDATE 2

As to why the function trademark isn't

int select(fd_set *readfds, int nreadfds,
fd_set *writefds, int nwritefds,
fd_set *exceptfds, int nexceptfds,
struct timeval *timeout);


My hunch is it is due to the fact that the code attempts to maintain all the debates in registers, so the CPU can work with them much faster, and also if it needed to track an added 2 variables, the CPU could not have adequate signs up.

So to put it simply, select is revealing an execution information to make sure that it can be much faster.

0
2019-05-21 06:55:16
Source