Linux C Select Usage tips

Source: Internet
Author: User

1. Use the Select function as a timer
it_value.tv_sec = 0;
It_value.tv_usec = 100000;
Select (1,null,null,null,&it_value);
The above code does not return immediately but waits for 100ms, can be used as a timer, and is more accurate than sleep.
2, the Linux platform under the Select function to modify the time-out period
Select_timeout.tv_sec = 10;
select_timeout.tv_usec = 0;
for (;;)
{
if (select (fd+1, &rset, NULL, NULL, &select_timeout) <=0)
{   **************
}
}
We had hoped to process the data when FD was readable, if there was no data sleep waiting in 10s. But
In fact, because the SELECT function modifies the value of Select_timeout, the above code enters an infinite loop and cannot sleep. 3. Select function to help determine the disconnection of the network connection
Select (confd+1, &rset, NULL, NULL, &select_timeout);
Assume that CONFD is a TCP network FD and is joined to RSet. If the connection is closed to the end of the socket,
The Select function is immediately returned from sleep, at which point the socket return value of 0 is read, and we can tell that the socket is broken. 4, select function to determine whether a network socket can write select (confd+1, NULL, &wset, NULL, &select_timeout); Suppose a confd is a TCP-connected network socket, and the peer is closed before calling the above code
The TCP connection, when the Select function returns immediately, but the return value is 0, if the end of the function is called without
Closed and the socket write buffer is empty, this time the task sleep waits for the CONFD writable, if the waiting process
The peer-to-peer shutdown connection Select does not return immediately, but sleeps until it expires.

Linux C Select Usage tips

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.