Introduction to the select function (C language)

Source: Internet
Author: User
From: http://blog.csdn.net/piaojun_pj/article/details/5991968 select in socket programming is more important, but for beginners socket are not very fond of using select to write programs, they are just used to writing blocking programs such as connect, accept, Recv, or recvfrom (the so-called blocking method block, as the name suggests, 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 ). However, using select can achieve non-blocking (the so-called non-block method means that the process or thread does not have to wait for the event to execute this function. Once executed, it will definitely return, 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, so it is more efficient). It can monitor changes in file descriptors that we need to monitor-read/write or exceptions.

 

Select Function Format:

Int select (INT maxfdp, fd_set * readfds, fd_set * writefds, fd_set * errorfds, struct timeval * timeout );

Two struct types are described first:
First, struct fd_set
It can be understood as a collection where the file descriptor (filedescriptor) is stored, that is, the file handle. This can be what we call a normal object, of course, any device, pipeline, and FIFO in UNIX are all in the file format, so there is no doubt that a socket is a file, and a socket handle is a file descriptor. The fd_set set can be operated manually by some macros, such as clearing the set fd_zero (fd_set *) and adding a given file descriptor to the set fd_set (INT, fd_set *), deletes a given file descriptor from the set fd_clr (INT, fd_set *) and checks whether the specified file descriptor in the set can be read and written by fd_isset (int
, Fd_set *).

Second, struct timevalIt is a common structure used to represent the time value. It has two members: one is the number of seconds and the other is the number of milliseconds.

Specific interpretation of select parameters: 
Int maxfdp is an integer that indicates the range of all file descriptors in the collection. That is, the maximum value of all file descriptors is 1. It cannot be wrong! In Windows, the value of this parameter does not matter. You can set it incorrectly.

Fd_set * readfds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the read changes of these file descriptors, that is, we are concerned about whether data can be read from these files. If a file in this collection is readable, 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 to time out. If the time exceeds the timeout time, 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 read changes.

Fd_set * writefds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the write changes of these file descriptors, that is, we are concerned about whether data can be written to these files. If there is a file in this collection that can be written, select will return a value greater than 0, indicating that a file is writable, if no writable file exists, the timeout parameter is used to determine whether to time out. If the time exceeds the timeout time, 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.

Fd_set * errorfds indicates the intention of the two parameters above, which is used to monitor file error exceptions.

Struct timeval * timeout is the select timeout time. This parameter is crucial. It can make the SELECT statement in three States. First, if null is input as an input parameter, that is, the time structure is not input, that is, the SELECT statement is placed in the blocking state. It must wait until a file descriptor in the collection of monitored file descriptors changes. Second, if the time value is set to 0 s and 0 ms, it becomes a pure non-blocking function. no matter whether the file descriptor has changed or not, it immediately returns to continue execution. If the file does not change, 0 is returned, and a positive value is returned. Third, the value of timeout is greater than 0, this is the waiting timeout time, that is, the SELECT statement is blocked within the timeout time, and an event will be returned within the time-out time. Otherwise, no matter how long it will be returned, the return value is the same as the above.

Returned value: the total number of descriptors whose return status changes. 
Negative value: Select Error

Positive Value: some files can be read or written, or errors occur.

0: wait for timeout. No file can be read/written or error

 

Select Function Format:

Int select (INT maxfdp, fd_set * readfds, fd_set * writefds, fd_set * errorfds, struct timeval * timeout );

Two struct types are described first:
First, struct fd_set
It can be understood as a collection where the file descriptor (filedescriptor) is stored, that is, the file handle. This can be what we call a normal object, of course, any device, pipeline, and FIFO in UNIX are all in the file format, so there is no doubt that a socket is a file, and a socket handle is a file descriptor. The fd_set set can be operated manually by some macros, such as clearing the set fd_zero (fd_set *) and adding a given file descriptor to the set fd_set (INT, fd_set *), deletes a given file descriptor from the set fd_clr (INT, fd_set *) and checks whether the specified file descriptor in the set can be read and written by fd_isset (int
, Fd_set *).

Second, struct timevalIt is a common structure used to represent the time value. It has two members: one is the number of seconds and the other is the number of milliseconds.

Specific interpretation of select parameters: 
Int maxfdp is an integer that indicates the range of all file descriptors in the collection. That is, the maximum value of all file descriptors is 1. It cannot be wrong! In Windows, the value of this parameter does not matter. You can set it incorrectly.

Fd_set * readfds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the read changes of these file descriptors, that is, we are concerned about whether data can be read from these files. If a file in this collection is readable, 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 to time out. If the time exceeds the timeout time, 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 read changes.

Fd_set * writefds is a pointer to the fd_set structure. This collection should contain file descriptors. We want to monitor the write changes of these file descriptors, that is, we are concerned about whether data can be written to these files. If there is a file in this collection that can be written, select will return a value greater than 0, indicating that a file is writable, if no writable file exists, the timeout parameter is used to determine whether to time out. If the time exceeds the timeout time, 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.

Fd_set * errorfds indicates the intention of the two parameters above, which is used to monitor file error exceptions.

Struct timeval * timeout is the select timeout time. This parameter is crucial. It can make the SELECT statement in three States. First, if null is input as an input parameter, that is, the time structure is not input, that is, the SELECT statement is placed in the blocking state. It must wait until a file descriptor in the collection of monitored file descriptors changes. Second, if the time value is set to 0 s and 0 ms, it becomes a pure non-blocking function. no matter whether the file descriptor has changed or not, it immediately returns to continue execution. If the file does not change, 0 is returned, and a positive value is returned. Third, the value of timeout is greater than 0, this is the waiting timeout time, that is, the SELECT statement is blocked within the timeout time, and an event will be returned within the time-out time. Otherwise, no matter how long it will be returned, the return value is the same as the above.

Returned value: the total number of descriptors whose return status changes. 
Negative value: Select Error

Positive Value: some files can be read or written, or errors occur.

0: wait for timeout. No file can be read/written or error

Related Article

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.