0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
POOJA GUPTA
  • 2,295
  • 7
  • 32
  • 60
  • I think `strace` is displaying the descriptor set as an array of FDs, not the actual binary data. – Barmar Feb 03 '18 at 06:56
  • @Barmar : but the array of FDs should be [1] only ? – POOJA GUPTA Feb 03 '18 at 06:58
  • Standard input is FD 0, so `[0]` means that the FD set contains `0`. – Barmar Feb 03 '18 at 06:59
  • @Barmar : can you see the above question once more please if standard FD is 0 then the every value in array is an integer formed after setting the bit for that descriptor. The integer value formed should be 1 and hence array is [1] – POOJA GUPTA Feb 03 '18 at 07:03
  • @POOJAGUPTA Right. But `strace` isn't showing you the literal integer values passed to `select`; it's showing you the list of file descriptors. If you called `select()` with standard input, output, and error (FDs 0, 1, and 2) selected, for instance, it'd show up as `[0,1,2]`. –  Feb 03 '18 at 07:06
  • @Barmar : I agree with you totally but then my question is what is the standard? does this mean descriptors represented internally and showed up differently? then this is very confusing. – POOJA GUPTA Feb 03 '18 at 07:11
  • There is no standard, the internal representation is implementation-dependent. – Barmar Feb 03 '18 at 07:11
  • @Barmar : Thanks a lot for your patience and answer :) Also what do have to say about second query? – POOJA GUPTA Feb 03 '18 at 07:13
  • I don't understand the second question. You have to check each descriptor set separately. The read and exception sets can have different descriptors set, and you need to check both of them and do whatever processing is necessary. – Barmar Feb 03 '18 at 07:16
  • @barmar : we will check each descriptor set separately but what if the select returned with only descriptors ready in read set since those were ready for read at that time. But later on descriptor in exception set receives exception but not at the time when first select was returned. So, will we miss out this signal for exception set if we are not using select iteratively and is used only once? – POOJA GUPTA Feb 03 '18 at 09:32
  • If you want to get more events later on, you have to keep calling it. It can't predict the future. – Barmar Feb 03 '18 at 09:34
  • If you only use it once, you only get the first event. That's just simple logic. – Barmar Feb 03 '18 at 09:35

1 Answers1

1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612