Select function in socket programming

2020-01-29 07:31发布

问题:

Can anyone tell me the use and application of select function in socket programming in c?

回答1:

The select() function allows you to implement an event driven design pattern, when you have to deal with multiple event sources.

Let's say you want to write a program that responds to events coming from several event sources e.g. network (via sockets), user input (via stdin), other programs (via pipes), or any other event source that can be represented by an fd. You could start separate threads to handle each event source, but you would have to manage the threads and deal with concurrency issues. The other option would be to use a mechanism where you can aggregate all the fd into a single entity fdset, and then just call a function to wait on the fdset. This function would return whenever an event occurs on any of the fd. You could check which fd the event occurred on, read that fd, process the event, and respond to it. After you have done that, you would go back and sit in that wait function - till another event on some fd arrives.

select facility is such a mechanism, and the select() function is the wait function. You can find the details on how to use it in any number of books and online resources.



回答2:

The select function allows you to check on several different sockets or pipes (or any file descriptors at all if you are not on Windows), and do something based on whichever one is ready first. More specifically, the arguments for the select function are split up into three groups:

  • Reading: When any of the file descriptors in this category are ready for reading, select will return them to you.

  • Writing: When any of the file descriptors in this category are ready for writing, select will return them to you.

  • Exceptional: When any of the file descriptors in this category have an exceptional case -- that is, they close uncleanly, a connection breaks or they have some other error -- select will return them to you.

The power of select is that individual file/socket/pipe functions are often blocking. Select allows you to monitor the activity of several different file descriptors without having to have a dedicated thread of your program to each function call.

In order for you to get a more specific answer, you will probably have to mention what language you are programming in. I have tried to give as general an answer as possible on the conceptual level.



回答3:

select() is the low-tech way of polling sockets for new data to read or for an open TCP window to write. Unless there's some compelling reason not to, you're probably better off using poll(), or epoll_wait() if your platform has it, for better performance.



回答4:

More detail would be good, but I think you're referring to Java NIO's Selector.select() method.

The simple answer to your question is that select() (in this context) will wait for a channel (that is, one of the network connections that's being managed by this Selector object) has data available to read.

When you have many connections open at once, many/most will be dormant at any given moment. This method/class allows you to manage many connections without having a separate for each connection blocking on that connection. You can block with one thread for multiple connections and simply receive back whichever connection(s) are "ready" at the moment.

Here's a great little tutorial that should make things clear:

http://rox-xmlrpc.sourceforge.net/niotut/



回答5:

Per the documentation for Linux manpages and MSDN for Windows,

select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking.

For simple explanation: often it is required for an application to do multiple things at once. For example you may access multiple sites in a web browser, a web server may want to serve multiple clients simultaneously. One needs a mechanism to monitor each socket so that the application is not busy waiting for one communication to complete.

An example: imagine downloading a large Facebook page on your smart phone whilst traveling on a train. Your connection is intermittent and slow, the web server should be able to process other clients when waiting for your communication to finish.

  • select(2) - Linux man page
  • select Function - Winsock Functions


回答6:

I like description at gnu.org:

Sometimes a program needs to accept input on multiple input channels whenever input arrives. For example, some workstations may have devices such as a digitizing tablet, function button box, or dial box that are connected via normal asynchronous serial interfaces; good user interface style requires responding immediately to input on any device. [...]

You cannot normally use read for this purpose, because this blocks the program until input is available on one particular file descriptor; input on other channels won’t wake it up. You could set nonblocking mode and poll each file descriptor in turn, but this is very inefficient.

A better solution is to use the select function. This blocks the program until input or output is ready on a specified set of file descriptors, or until a timer expires, whichever comes first.