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 (?) [-]
- Select-System call int SELECT-function
- Function
- Parameters
- return value
- Select-System invoke-how to use
- 1 adding files to the file descriptor set to be monitored
- 2 call Select to start monitoring
- 3 Determine if the file has changed
- Four macros to describe the descriptor operation-Fd_set-fd_clr-fd_zero-fd_isset
- Poll Method-Drive int Poll
- Poll Device method is responsible for the completion-drive
- Use poll_wait to add the wait queue to poll_table
- Returns a mask that describes whether the device is readable or writable-pollin
- 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:   ; pollin : Device readable . pollrdnorm: Data readable . & nbsp; &nbsP pollout  : 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"