POOJA GUPTA
POOJA GUPTA

Reputation: 2355

Descriptor bit setting in select()

I am currently studying select() for I/O multiplexing in network programming. select takes following arguments:

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

Description for select() call's fd_set datatype is given as:

select uses descriptor sets, typically an array of integers, with each bit in each integer corresponding to a descriptor. For example, using 32-bit integers, the first element of the array corresponds to descriptors 0 through 31, the second element of the array corresponds to descriptors 32 through 63, and so on.

if this is the case then as per my understanding, if you have descriptors 1,4,5 for read set then the value will be set as :

LSB                          MSB   

01001100000000000000000000000000    // descriptor bits set corresponding to 1,4,5

0123456........................31  //descriptor bit positions

0-31 bits will form integer value to be 50 which should be the first element of the read set i.e. [50] in array notation since fd_set datatype is :

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

But when I checked the strace -p 2172 where 2172 is a pid of another terminal in which I typed ls command, it showed the result as:

pselect6(1,[0],NULL,NULL,NULL,{[],8})

why read descriptor set in pselect6 array : [0], when fd descriptor associated with standard input is 0 so integer value becomes 1 i.e.[1]? Have I correctly interpreted the description?

Also, what happens if you set for read as well as exception sets and during the call to select, descriptors only in read set are ready for the read operation and returned. However, later on descriptors for exception set is also ready but since select has already returned then checking for FD_ISSET(int fd, fd_set*exceptSet) would return what ?

I am new to network programming.

Upvotes: 0

Views: 330

Answers (1)

Barmar
Barmar

Reputation: 780723

strace isn't showing the actual binary values in the fd_set. It's translating it to the list of FD numbers that it corresponds to. So if the binary value is 01001100000000000000000000000000 it will show [1,4,5].

The internal implementation of fd_set is implementation-dependent, and you don't need to concern yourself with it. You just use the macros in <select.h> to set, clear, and test descriptors in the set.

Upvotes: 1

Related Questions