A blocking/nonblocking method is available when an application accesses a device file. If you are using blocking mode, call Open (), read (), write () directly, but the driver layer will determine whether it is readable/writable, and if unreadable/not writable, the current process will hibernate until it wakes up. If you are using non-blocking, you need to use the poll/select mechanism, and the access to the tag file is o_nonblock when you open the file.
1 int Select (intstruct
FD_CLR (INR fd,fd_set* set), used to clear the bits of the associated FD in the description phrase set
Fd_isset (int fd,fd_set *set), used to test whether the bits of the associated FD in the description phrase set are true
Fd_set (int fd,fd_set*set), used to set the bit of the associated FD in the description phrase set
Fd_zero (Fd_set *set), used to clear all bits of the description phrase set
If the parameter timeout is set to: NULL: Then the Select () does not have a timeout,select that will remain 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.
1 int poll (structint timeout); The two functions are essentially similar.
FDS can pass multiple structures, which means that the events generated by multiple drives can be monitored, so long as one generates a request event, it can immediately return
struct POLLFD {
int FD; /* File Descriptor */
Short events; /* Event type requested, monitor event mask for driver file */
Short revents; /* Events actually returned by the drive file */
} ;
Nfds Monitor the number of drive files
Timeout time-out, in MS
Event type events can be of the following values:
Pollin has data to read
Pollrdnorm has common data readable, equivalent to Pollin
POLLPRI have urgent data to read
Pollout write data does not cause blocking
Poller An error occurred with the specified file descriptor
Pollhup The specified file descriptor suspend event
Pollnval invalid request, cannot open the specified file descriptor
return value
There is an event that returns the number of file descriptors for which the Revents field is not 0 (that is, an event occurs, or an error report)
Timeout returned 0;
Failed to return 1, and set errno to the error type
Understanding the Select Model:
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.
Understanding the poll mechanism from the kernel state
We call the poll function directly from the application and the system goes through the following process
1 App:poll2 Kernel:sys_poll3 Do_sys_poll4Poll_initwait (&table)5Do_poll (Nfds, head, &table, timeout)6 for (;;) {7 for(; PFD! = Pfd_end; pfd++) {/*can monitor events generated by multiple drive devices*/8 if(DO_POLLFD (PFD, PT)) {9count++;TenPT =NULL; One } A if(Count | |!*timeout | |signal_pending (current)) - Break; -__timeout =schedule_timeout (__timeout); the } - } - - + DO_POLLFD (PFD, PT) { - ... + if(File->f_op && file->f_op->poll) AMask = file->f_op->Poll (file, pwait); at returnMask; - ... -} based on kernel source version linux-2.6.22.6
Using Poll_initwait (&table), the __pollwait is set as a callback function, followed by the call to the driver poll function, poll function call pollwait is equal to call __pollwait, Joins the current process to the wait queue. And then always in the loop, DO_POLLFD is to call the driver's poll function, the driver's poll function, the poll function starts to call pollwait is tantamount to calling the __pollwait callback function, adding the current process to the wait queue, in order to wake up the current process after hibernation. It then returns the status of the current drive device (mask).
If DO_POLLFD returns a mask that is not 0, that is, count is not 0, it returns immediately, and the application can use Fd_isset to understand the state of the device at this time. Of course, if the timeout is exceeded or if the process has other signals to process the timeout, the process has other signals to process and will return immediately, but the application uses Fd_isset to know if the device state or not is available at this time and continues polling. If DO_POLLFD returns a mask of 0 and does not time out and no other signal occurs, the process is dispatched to hibernate the process. This process has been added to the driver's waiting queue before, and if the device is available, it wakes up the process in the waiting queue, wakes up the process, and goes to Poll_initwait (&table).
Reference: https://www.cnblogs.com/amanlikethis/p/6915485.html
Http://www.cnblogs.com/shihaochangeworld/p/5747490.html
Analysis of poll mechanism of Linux