Operation of file descriptors in Linux (Fd_zero, Fd_set, FD_CLR, Fd_isset

Source: Internet
Author: User

In Linux, the kernel accesses a file using the file descriptor, the file descriptor, as its handle. The file descriptor is a non-negative integer. When you open an existing file or create a new file, the kernel returns a file descriptor. Read-write files also need to use file descriptors to specify which files to read and write. Macro Fd_zero, Fd_set, FD_CLR, Fd_isset "FD" is the abbreviation for file descriptor, described below.

First, we introduce an important struct: Fd_set, which is used many times as a parameter to some of the following functions, and fd_set can be understood as a collection, which holds the file descriptor (the file descriptor), which is the handle to the document. The Fd_set collection can be manipulated manually using the following macro.

1 "Fd_zero

Usage:fd_zero (fd_set*);

Used to empty the Fd_set collection, which means that the Fd_set collection no longer contains any file handles.

2 "Fd_set

Usage:fd_set (int, fd_set *);

Used to add a given file descriptor to the collection

3 "fd_clr

Usage:fd_clr (int, fd_set*);

Used to remove a given file descriptor from the collection

4 "Fd_isset

Usage:fd_isset (int, fd_set*);

Detects if the state of the FD in the Fdset set changes, returns True if the FD state is detected, otherwise, returns False (it can also be assumed that the specified file descriptor in the collection can read and write).

5 "Function Select

Usage:int Select (int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout);

function: Used to monitor the state of the file descriptor (the file descriptor in a read or write file set) that we need to monitor. And we can tell by the value returned.

Parameter explanation:

int MAXFDP: The range of all file descriptors in the collection, plus 1 for the maximum value of all file descriptors.

fd_set *readfds: The set of read files to be monitored.

fd_set *writefds: The set of write files to be monitored.

fd_set *errorfds: used to monitor exception data.

struct timeval* Timeout: The time-out period for select, which enables select to be in three states:

First, if NULL is passed in as a parameter, that is, the time structure is not passed in, the select is put in a blocking state, it must wait until one of the file descriptors in the monitor file descriptor collection has changed;

Second, if the time value is set to 0 seconds and 0 milliseconds, it becomes a purely non-blocking function, regardless of whether the file descriptor changes, immediately return to continue execution, the file has no change return 0, there is a change to return a positive value;

Third, the value of timeout is greater than 0, which is the waiting time-out period, that is, select in timeout time block, the timeout period within the time of the arrival of the return, otherwise after the timeout, anyway must return.

struct Timeval timeout; timeout.tv_sec = 0; Seconds timeout.tv_usec = dwtimeout * 1000; Microseconds 1 milliseconds = 1000 microseconds

Return Value Description:

>0: The monitored file descriptor has changed

-1: error

0: timeout


Operation of file descriptors in Linux (Fd_zero, Fd_set, FD_CLR, Fd_isset

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.