Select function Summary

Source: Internet
Author: User

Blocking block means that the process or thread must wait for an event to occur when executing these functions. If the event does not occur, the process or thread will be blocked and the function cannot return immediately. With select, non-blocking non-block can be completed, that is, when the process or thread executes this function, it does not have to wait for the event to occur. Once it is executed, it is certainly returned, different return values are used to reflect the execution of the function. If the event occurs in the same way as the blocking method, if the event does not occur, a code is returned to inform the event that the event has not occurred, while the process or thread continues to execute, therefore, the efficiency is high. Select can monitor the changes of file descriptors that we need to monitor.

(1) first, describe two structs:
1: struct fd_set a file descriptor, that is, the aggregation of file handles, is actually a long array,
Each array element can be associated with an open file handle (whether it is a socket handle, or other files, named pipes, or device handle). The establishment of the contact is done by the programmer;

Fd_zero (fd_set * fdset): clears the contact between fdset and all file handles.
Fd_set (IntFD, fd_set * fdset): Establishes the connection between the file handle FD and fdset.
Fd_clr (IntFD, fd_set * fdset): clears the contact between the file handle FD and fdset.
Fd_isset (IntFD, fdset * fdset): Check whether the file handle FD associated with fdset can be read and written.> 0 indicates that the file handle can be read and written.

2: struct timeval is used to represent the time value. There are two members, one is the number of seconds TV _sec, and the other is the number of milliseconds TV _usec.

(2) The following describes the prototype of the select () function:
1: int select (INT NFDs, fd_set * rdfds, fd_set * wtfds, fd_set * exfds, struct timeval * timeout)
2: NDfS: the number of file handles monitored in the SELECT statement, which is generally set to the maximum file number plus one in the file to be monitored.
3: rdfds: a collection of readable file handles monitored by select (). When the file handle status of the rdfds image changes to readable, the system tells the select function to return the result.
If there is a readable file in this set, select returns a value greater than 0, indicating that the file is readable,
If there is no readable file, the timeout parameter is used to determine whether the file times out,
If the time exceeds timeout, select returns 0. If an error occurs, a negative value is returned,
A null value can be input, indicating that the read changes of any files are not concerned;
4: the set of writable file handles monitored by wtfds: Select (). When the file handle status of the wtfds image changes to writable, the system tells the select function to return the result.
If there is a file writable in this collection, select returns a value greater than 0, indicating that there is a file writable,
If there is no writable file, the timeout parameter is used to determine whether the file times out,
If the time exceeds timeout, select returns 0. If an error occurs, a negative value is returned,
You can pass in a null value, indicating that you do not care about any file write changes.
5: exfds: a collection of abnormal file handles monitored by select (). When a special exception occurs on the file handle of the exfds image, the system will tell the select function to return the exception.
6: Timeout: the timeout end time of select.
This parameter puts the SELECT statement in three states,
First, if null is input as a form parameter, that is, if the time structure is not input, select is placed in the blocking state,
Wait until a file descriptor changes in the collection of monitored file descriptors;
Second, if the time value is set to 0 s 0 ms, it becomes a pure non-blocking function, regardless of whether the file descriptor has changed,
Are returned immediately to continue the execution. If the file is not changed, 0 is returned. If the file is changed, a positive value is returned;
Third, the value of timeout is greater than 0, which is the wait timeout time, that is, the Select is blocked within the timeout time,
If an event arrives within the time-out period, it is returned. Otherwise, no matter how long the event is returned, the returned value is the same as the above.

7: Return Value: negative value: Select Error
0: wait for timeout. No file can be read/written or error
Positive Value: some files can be read, written, or error

(3) The following is an example with three socket handles.

IntSa, Sb, SC;
Sa = socket (...);
Connect (SA ,...);
SB = socket (...);
Connect (SB ,...);
SC = socket (...);
Connect (SC ,...);
Fd_set (SA, & rdfds);/* add three handles to the read monitoring set respectively */
Fd_set (SB, & rdfds );
Fd_set (SC, & rdfds );
IntMaxfd = 0;
If(Sa> maxfd) maxfd = sa;/* obtain the maximum value of three handles */
If(Sb> maxfd) maxfd = Sb;
If(SC> maxfd) maxfd = SC;
StructTimeval TV;
TV. TV _sec = ...;
TV. TV _usec = ...;
Ret = select (maxfd + 1, & rdfds, null, null, & TV);/* Add the maximum value to 1 */
If(Ret <0)
{
Perror ("select");/* select function Error */
}
Else If(Ret = 0)
{
Printf ("timeout \ n");/* the socket status does not change during the set TV time */
}
Else
{
Printf ("ret = % d \ n", RET );
If(Fd_isset (SA, & rdfds)/* first checks whether the monitored handle of SA is actually readable */
{
Recv (...);/* read data in the socket handle */
}
......
}

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.