Select Function Analysis in Linux

Source: Internet
Author: User

File Header: # include <sys/select. h>

Int select (INT maxfdp1, fd_set * restrict readfds, fd_set * restrict writefds, fdset * restrict limit TFDs, struct timeval * restrict tvptr );

Returned value: the number of ready descriptors. If it times out, 0 is returned. If an error occurs,-1 is returned. note: If a descriptor encounters the end of the file, select considers the descriptor to be readable and calls read to return 0.

Note: Select has three parameters. If all three pointers are null, select provides a timer that is more accurate than sleep.

The select function allows us to perform multiple I/O transfers. The parameters passed to select tell the kernel:

1. descriptor we care about;

2. The State we care about for each descriptor;

3. How long are you willing to wait (Always tvptr = NULL, fixed value tvptr-> TV _sec! = 0 | tvptr-> TV _sec! = 0. Do not wait for tvptr-> TV _sec = 0 & tvptr-> TV _usec = 0).

The returned result from select is that the kernel tells us:

Which descriptor is ready for each of the read, write, and exception states ." "Preparation" refers:Read of readfds is not blocked, so this descriptor is ready. Write operations on wirtefds are not blocked, so this descriptor is ready. Prepare for pending exceptions in exceptfds.

Maxfdp1: 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 plus one in each file to be monitored.Because the descriptor number starts from 0, the number of descriptors (number) must be numbered based on + 1.

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.

Each descriptor set is stored in an fd_set data type.

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.

When declaring a descriptor set, you must use fd_zero to clear the bit used by it, and then set the bit we care about.

For example:

Fd_set rset;

Int FD;

Fd_zero (& rest );

Fd_set (FD, & rset );

Fd_set (stdin_fileno, & rset );

If (fd_isset (FD, & rset )){

......}

References:

Advanced Programming in UNIX environment (version 2)

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.