Linux driver-Select function Introduction

Source: Internet
Author: User
Tags readable

First, the Select function is introducedThe Select function is used to notify you when a socket or a set of sockets has a signal, and the system provides a select function to implement a multiplexed input/output model, prototypes:         #include < sys/time.h>         #include <unistd.h>         INT Select ( int Maxfd,fd_set *rdset,fd_set *wrset,fd_set *exset,struct timeval *timeout);     The parameter maxfd is the largest file descriptor value that needs to be monitored +1;rdset,wrset,exset corresponds to a collection of readable file descriptors that need to be instrumented, a collection of writable file descriptors, and a collection of exception file descriptors. The struct TIMEVAL structure is used to describe a length of time, and the return value is 0 if the descriptor that needs to be monitored does not have an event occurred during this time.      Fd_set (it's important to introduce it first) is a set of file description words (FD), which is represented by a single FD (described below), and for the Fd_set type by the following four macros::      fd_zero (Fd_set *fdset); The specified file description is Fu Giuqing empty, and the file descriptor collection must be initialized before it is set, and if not emptied, the result is not known because it is usually not emptied after the system allocates memory space.       fd_set (Fd_set *fdset) to add a new file descriptor to the file descriptor collection.      &NBSP;FD_CLR (fd_set *fdset); Used to delete a file descriptor in the file descriptor collection.       fd_isset (int fd,fd_set *fdset), used to test whether the specified file descriptor is in the collection.           In the past, a fd_set usually can only contain <32 fd (file descriptor) because Fd_setIn fact, only a 32-bit vector to represent FD; now, UNIX systems typically define constant fd_setsize in the header file, which is the number of descriptors for the data type Fd_set, which is usually 1024, which can represent the FD of <1024. Based on the fd_set bit vector implementation, we can re-understand the operation of the Fd_set four macro:     Fd_set set; Fd_zero (&set);//Clearfd_set (0, &set);//Addfd_clr (4, &set);//Clear 4Fd_isset (5, &set);//check

Timeout setting Condition:

The null:select will always be blocked until an event occurs on a file descriptor.

0: Detects only the state of the descriptor collection and then returns immediately without waiting for an external event to occur.

Specific time value: If no event occurs during the specified time period, select will time out to return.

The key to understanding the Select model is to understand fd_set, for illustrative convenience, take fd_set length of 1 bytes, each bit in fd_set can correspond to a file descriptor FD. The 1-byte-long fd_set can correspond to 8 fd maximum.

(1) Execute fd_set set; Fd_zero (&set); The set is represented by a bit 0000,0000.

(2) If fd=5, execute Fd_set (fd,&set), then SET to 0001,0000 (5th position 1)

(3) If you add fd=2,fd=1 again, set becomes 0001,0011

(4) Execute select (6,&set,0,0,0) blocking wait

(5) If a readable event occurs on the fd=1,fd=2, select returns, at which point the set becomes 0000,0011. Note: No event occurs when the fd=5 is emptied.

Linux driver-Select function Introduction

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.