Select () in Linux socket to determine whether a file is readable or written

Source: Internet
Author: User
The Select () mechanism provides an fd_set data structure, which is actually a long array. Each array element can work with an open

The file handle (whether it is a socket handle, or another file or named pipe or device handle) establishes a contact, the work of establishing a contact is done by the programmer

When select () is called, the kernel modifies the content of fd_set according to the IO status to notify the process that has executed select ().

The socket or file is readable. The following is a detailed explanation:

Int select (NFDs, readfds, writefds, limit TFDs, timeout)

Int NFDs;

Fd_set * readfds, * writefds, * required TFDs;

Struct timeval * timeout;

NDfS: the number of file handles monitored by the SELECT statement, depending on the number of files opened in the process. It is generally set to the maximum file number in each file to be monitored.

Add one.

Readfds: a collection of readable file handles monitored by select.
 
Writefds: a set of writable file handles monitored by select.
 
Except TFDs: a collection of abnormal file handles monitored by select

Timeout: the timeout end time of this select () operation. (See/usr/include/sys/select. H, which can be accurate to one in

Seconds !)

When the image files in readfds or writefds are readable, writable, or time-out, this select () operation ends and returns. Programmers use a group of systems

The macro provided by the system can determine which file is readable or writable at the end of select. Readfds is particularly useful for socket programming.

Related macros are explained as follows:

Fd_zero (fd_set * fdset): clears the contact between fdset and all file handles.
 
Fd_set (int fd, fd_set * fdset): Establishes the connection between the file handle FD and fdset.
 
Fd_clr (int fd, fd_set * fdset): clears the contact between the file handle FD and fdset.
 
Fd_isset (int fd, fdset * fdset): Check whether the file handle FD associated with fdset is readable or not.> 0 indicates that the file handle FD can be read or written.
(For definition of fd_set and related macros, see/usr/include/sys/types. h)

In this way, your socket only needs to be read when there is something to read, roughly as follows:
...
Int sockfd;
Fd_set FDR;
Struct timeval timeout = ..;
...
For (;;){
Fd_zero (& FDR );
Fd_set (sockfd, & FDR );
Switch (select (sockfd + 1, & FDR, null, & timeout )){
Case-1:
Error handled by u;
Case 0:
Timeout hanled by u;
Default:
If (fd_isset (sockfd )){
Now u read or Recv something;
/* If sockfd is father and server socket, u can now accept ()*/
}
}
}

Therefore, an fd_isset (sockfd) is equivalent to a FD readable notification.
 
For the struct timeval function, use man select. Different timeval settings enable Select () to display timeout,

No timeout blocking or polling. Because timeval can be accurate to one second per million, Windows settimer () is not

What. You can use select () to make a super clock.

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.