Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the nfds from select() used for

Tags:

c

select

I was wondering what the nfds does, after reading different manuals, I end up with the only answer being it is the highest numbered file descriptor plus one. What is it exactly used for?

like image 598
Abdellah IDRISSI Avatar asked Jan 01 '12 21:01

Abdellah IDRISSI


People also ask

What does select () do in C?

The select() function indicates which of the specified file descriptors is ready for reading, ready for writing, or has an error condition pending.

How does select () work?

select() works by blocking until something happens on a file descriptor (aka a socket). What's 'something'? Data coming in or being able to write to a file descriptor -- you tell select() what you want to be woken up by.

What is the use of select call?

The SELECT call tests each selected socket for activity and returns only those sockets that have completed. On return, if a socket's bit is raised, the socket is ready for reading data or for writing data, or an exceptional condition has occurred.

What does select do in Linux?

select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing.


Video Answer


1 Answers

When you're using select(), you are trying to check the status of a set of file descriptors. The possible range of file descriptors you're interested in ranges from a low of 0 (standard input) to some maximum value (the highest file descriptor you have open that you're interested in checking the status of). You have to tell select() how big the list of file descriptors is because the total number can be 'vast' (32767, for example). In that case, it takes time for the kernel to process the descriptors, plus you may not have initialized the fd_set up to that number of entries. FD_SETSIZE also figures in the equation, but sometimes you can change that value.

So, if you want to monitor file descriptors 24-31, you'd set nfds to 32, and ensure that you use FD_ZERO() to zero the whole fd_set and FD_SET() to set entries 24-31. Note, too, that select() modifies the input parameters, so you have to use FD_ISSET() to test after the select() returns, and in general you have to redo the initialization (or copy a saved value) of fd_set before calling select() again.

like image 92
Jonathan Leffler Avatar answered Sep 22 '22 21:09

Jonathan Leffler