Linux-c the SELECT function for network programming

Source: Internet
Author: User

To get to the point, if we are going to operate on multiple events connected to multiple clients, we will first think of the simplest mode of setting up multiple threads or processes for each of them.
However, for each thread or process, you must be on standby regardless of whether the connection has an event, that is, each object must have one thread or process to correspond to it, until the object is destroyed .
It can be imagined that when the size of the connection is larger, the system needs to switch between many threads or processes, the time and space overhead is huge, that is, in this mode, the maximum value of the program to host the object is very small (generally hundreds of).

So, we're going to mention the select function. Man Select gets the function parameters and header file as follows

       #include <sys/select.h>       #include <sys/time.h>       #include <sys/types.h>       #include <unistd.h>       intSelectintNfds, Fd_set *readfds, Fd_set *writefds, Fd_set *exceptfds,structTimeval *timeout);voidFD_CLR (intFD, Fd_set *Set);//Delete fd       intFd_isset (intFD, Fd_set *Set);//Detect if FD exists in group       voidFd_set (intFD, Fd_set *Set);//Add FD       voidFd_zero (Fd_set *Set);//Group Clearance 0

Nfds: Integer variable, refers to the scope of all file descriptors in the collection, if the maximum file descriptor in the collection is Max, then ndfs=max+1, do not understand the total amount of the file descriptor.
Readfds: Pointer to a set of sockets waiting for readability checks.
Writefds: pointer that points to a set of sockets waiting for a writable check.
Exceptfds: pointer, pointing to a set of sockets waiting for error checking.
Timeout:select () maximum wait time, if null, is equivalent to blocking (wait until the event occurs), if 0 is non-blocking (no event is returned immediately), and the other value represents the return if there is an event or timeout.

function flow Interpretation and examples

    int ret,i,fd[MAX],MAX;    struct timeval timeout;    fd_set readfds;    while(1)    {        1;        0;        FD_ZERO(&readfds);//组清零初始化        for(i=0;i<MAX;i++)        {            FD_SET(fd[i], &readfds);        }        ret select(MAX,&readfds, NULL,NULL,&timeout);//仅演示读监听,其他同理    }

There are two points to pay special attention to when you place a select in the loop body

Initialization of the <1>timeout, because select executes at timeout is actually similar to the I – operation, so the loop body is reinitialized every time, otherwise the value of timeout will be 0 forever;
<2> the initialization of the set of file descriptors, because the mechanism of the set is similar to binary, Fd_zero () after all zeros in the set (0000000), if Fd_set (5), then (0100000), when selete in the execution It is found that each connection has an event, the socket is marked 1, no event flag is 0, after the completion of the execution, if there is an event, you can Fd_isset (FD,&READFDS) to determine whether FD has an event generated.
Therefore, each time the Select function is executed, the collection is re-initialized.

Select () can determine the state of one or more sets of interfaces, commonly used for single-process or single-threaded multiplexing.
In other words, select can respond to multiple connected event events within a thread or process.

So how does it complete this function, for instance:
If you order a takeaway at 100 different stores (different store receipts are of course different).

  1. If you use the above multi-process or threading mode to take out the takeaway, you need to find 99 more friends to help you to the various receiving points to wait. (Multi-process mode, which comes from the phone!) )
  2. You have a lamp called SELECT, you just need to wait in front of the Select, whenever one or more takeout to send to the time, will be lights lit, then the problem comes, you only know that the takeaway is sent to, but do not know exactly which home, so you need to personally put 100 takeout delivery point all over the , take off the sold-out, and then continue to wait before returning to the Beacon. What are the advantages of it, it is clear that 100 people can complete the things you have done, and a number of takeout may be at the same time, the road has gone a lot.
    As for the shortcomings, of course, every time you have to take the 100 receiving point all go once (weight loss is not so spelled)
  3. Find the epoll function, it will give you a mobile phone, delivery master takeout delivery can give you a call. With the mobile phone, all this should be how perfect, then what a perfect method, the next chapter to talk about it.

PS: Good sleepy ah, old, can not endure the night!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux-c the SELECT function for network programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.