PoVa
PoVa

Reputation: 1043

Why is socket file descriptor argument incremented by one when calling select?

Whenever I see a select call in C, I see it written as:

select(sock_fd + 1, &fdset, NULL, NULL, &tv)

or something similar. What is the meaning behind incrementing the file descriptor?

Upvotes: 2

Views: 832

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

From the POSIX specification of select:

The nfds argument specifies the range of descriptors to be tested. The first nfds descriptors shall be checked in each set; that is, the descriptors from zero through nfds-1 in the descriptor sets shall be examined.

That is, you give the size of the set, where each descriptor is an index.

Descriptors sets are basically implemented as arrays, and sock_fd (in your case) is an index into that array, while sock_fd + 1 is the size of the array to check.

Upvotes: 6

Related Questions