Linux Device Driver---Poll method---Select "Go"

Source: Internet
Author: User
Tags readable

Transferred from: http://blog.csdn.net/yikai2009/article/details/8653842

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Directory (?) [-]

    1. Select-System call int SELECT-function
      1. Function
      2. Parameters
      3. return value
    2. Select-System invoke-how to use
      1. 1 adding files to the file descriptor set to be monitored
      2. 2 call Select to start monitoring
      3. 3 Determine if the file has changed
    3. Four macros to describe the descriptor operation-Fd_set-fd_clr-fd_zero-fd_isset
    4. Poll Method-Drive int Poll
    5. Poll Device method is responsible for the completion-drive
      1. Use poll_wait to add the wait queue to poll_table
      2. Returns a mask that describes whether the device is readable or writable-pollin
    6. Poll Working principle

In the drive (kernel space) The Poll method corresponds to the Select system call (user space).

Poll-------------------------> Select

Select-System call int SELECT-function:

Seclect system calls are used for multi-channel monitoring , and select blocks the calling process when no file satisfies the requirement.

Function:

int select (int maxfd, fd_set *readfds, Fd_set * Writefds, Fe_set *exceptfds, const stuct Timeval * Timeout) .

Parameters:

MAXFD: The range of file descriptors is 1 larger than the maximum file descriptor to be detected.

Readfds: The set of file descriptors that are read-detected.

Writefds: The set of file descriptors that are written to monitor.

Exceptfds: The set of file descriptors that are being monitored abnormally.

Timeout: Timer.

Timeout to take a different value. The call has a different performance:

The 1,timeout value is 0, and returns immediately, regardless of whether a file satisfies the requirement.

No files meet the requirements return 0, there is a file that satisfies the requirement to return a positive value.

2,timeout is NULL. Select will block the process. Until a file satisfies the requirement.

3,timeout is a positive integer, which is the maximum time to wait, that is, Select blocks the process in Timeout time.

return value:

When the Select call returns, the return value has the following conditions:

1, normally returns the number of file descriptors that satisfy the requirement.

2, after Timeout wait, there is still no file to meet the requirements, the return value is 0.

3, if Select is interrupted by a signal, it will return-1 and set errno to Eintr.

4, if there is an error, return-1 and set the corresponding errno.

Select-System call-using method: 1, the file to be monitored is added to the file descriptor set.         2, call Select to start monitoring. 3. Determine if the file has changed.

Four macros to describe descriptor operation- fd_set- fd_clr- fd_zero- fd_isset :          system provides 4 Macro Description Descriptor Operation:                   #include  < sys/select.h >&nbs P                 void  fd_set (int  fd, fd_set * fdset)   &N Bsp   Add File descriptor FD   to file description set Fdset .                  void  fd_c LR (int  fd, fd_set * fdset)     Clear file descriptor from File Description Descriptor Fdset   FD .        &NBSP;&NBS P        void  fd_zero (Fd_set * fdset)               empty file descriptor set Fdset .                  void  fd_isset (int  fd, Fd_set * Fdset)   Use this macro to detect file Description descriptor Fdset file FD changes .eg:            poll method after you call Select-drive Int (*poll) in motion :          applications often use Select system calls, which can block processes .          This call is implemented by the driver's Poll method, the prototype For:         unsigned  int   (*poll) (struct  file * FILP, poll_table * wait  poll Device method is responsible for complete-drive  : Use  poll_wait   add wait queue to poll_table:         1, using &NB Sp;poll_wait   Adds the wait queue to poll_table. Returns a mask that describes whether the device is readable or writable- pollin:        &NBSP;2, and returns a description of the device is No readable or writable mask .                            Bitmask: &nbsp ;                                   pollin  : Device readable .                                    pollrdnorm: Data readable .                 & nbsp;    &nbsP             pollout &nbsp: Device writable .                                    pollwrnorm  : Data writable .   Device-readable usually return (Pollin | Pollrdnorm)
device writable usually returns (Pollout | Pollwrnorm)

eg

Poll Working principle:

The Poll method just makes a registration, and the real block occurs in the Do_select function in select.c.

Linux Device Driver---Poll method---Select "Go"

Related Article

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.