C language select function usage in Linux

Source: Internet
Author: User

Select (I/O multi-work mechanism)
Header file # I nclude
# I nclude
# I nclude
Define functions Int select (int n, fd_set * readfds, fd_set * writefds, fd_set * contains TFDs, struct timeval * timeout );
Function Description Select () is used to wait for the state change of the description word of the file. Parameter n represents the largest file descriptive word plus 1. The readfds, writefds, and limit TFDs parameters are called descriptive phrases and are used to return the reading, writing, or exception of the descriptive word. The macro below provides a way to deal with these three descriptive phrases:
Fd_clr (inr fd, fd_set * Set); used to clear the related FD bits in the description phrase set.
Fd_isset (int fd, fd_set * Set); used to test whether the bits of the related FD in the phrase set are true.
Fd_set (int fd, fd_set * Set); used to set the FD bit in the description phrase set.
Fd_zero (fd_set * Set); used to clear all bits of the phrase set.
Parameters Timeout is the timeval structure used to set the select () wait time. Its structure is defined as follows:
Struct timeval
{
Time_t TV _sec;
Time_t TV _usec;
};
Return Value If the timeout parameter is set to null, select () does not have timeout.
Error Code If execution is successful, the number of changes in the state of the description word in the file is returned. If 0 is returned, it indicates that the time before the State of the description word changes has exceeded the timeout time. If an error occurs,-1 is returned, the error cause is stored in errno. The value of readfds, writefds, limit TFDs, and timeout parameters becomes unpredictable.
The description of the ebadf file is invalid or the file is closed.
Eintr this call is interrupted by the signal
The value of N is negative.
Insufficient enomem core memory
Example Common Program snippets: fs_set readset;
Fd_zero (& readset );
Fd_set (FD, & readset );
Select (FD + 1, & readset, null );
If (fd_isset (FD, readset ){......}

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.