From 49fc499c81b531f5721b10e90dfff93b0641d16d Mon Sep 17 00:00:00 2001 From: "rick.chan" Date: Tue, 13 Apr 2021 11:22:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20poll=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=AF=B4=E6=98=8E.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rick.chan --- .../Linux/User/API/Linux_常用_C_API.md | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/Software/Development/OperatingSystem/Linux/User/API/Linux_常用_C_API.md b/Software/Development/OperatingSystem/Linux/User/API/Linux_常用_C_API.md index 5a1ad61..7b97785 100644 --- a/Software/Development/OperatingSystem/Linux/User/API/Linux_常用_C_API.md +++ b/Software/Development/OperatingSystem/Linux/User/API/Linux_常用_C_API.md @@ -44,7 +44,7 @@ int usleep(useconds_t usec); **说明:** -The usleep() function suspends execution of the calling thread for (at least) usec microseconds. The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers. +The usleep() function suspends execution of the calling thread for (at least) usec microseconds. The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers. **参数:** @@ -56,8 +56,76 @@ The usleep() function returns 0 on success. On error, -1 is returned, with errno ### 1.3.poll 函数 +**头文件:** + +```cpp +#include +``` + +**函数原型:** + +```cpp +int poll(struct pollfd fds[], nfds_t nfds, int timeout); +``` + +**说明:** + +poll 提供的功能与 select 类似,不过在处理流设备时,它能够提供额外的信息。 + +The poll() function provides applications with a mechanism for multiplexing input/output over a set of file descriptors. For each member of the array pointed to by fds, poll() shall examine the given file descriptor for the The poll() function provides applications with a mechanism for multiplexing input/output over a set of file descriptors. For each member of the array pointed to by fds, poll() shall examine the given file descriptor for the certain events have occurred. + 注意,如果 poll 返回有可读数据,需要读取数据才能清除状态,如果不读取,再次 poll 会立即返回有可读数据。 +**参数:** + +fds:The fds argument specifies the file descriptors to be examined and the events of interest for each file descriptor. It is a pointer to an array with one member for each open file descriptor of interest.The array's members are pollfd structures within which fd specifies an open file descriptor and events and revents are bitmasks constructed by OR'ing a combination of the following event flags: + +| Const | Description | +|------------|--------------------| +| POLLIN | 普通或优先级带数据可读 | +| POLLRDNORM | 普通数据可读 | +| POLLRDBAND | 优先级带数据可读 | +| POLLPRI | 高优先级数据可读 | +| POLLOUT | 普通数据可写 | +| POLLWRNORM | 普通数据可写 | +| POLLWRBAND | 优先级带数据可写 | +| POLLERR | 发生错误 | +| POLLHUP | 发生挂起 | +| POLLNVAL | 描述字不是一个打开的文件 | + +The poll() function shall not be affected by the O_NONBLOCK flag. + +nfds:要监视的描述符的数目 + +timeout:用毫秒表示的超时时间。最大的超时周期大约是 30 分钟。 + +| Timeout | Description | +|---------|-----------------| +| INFTIM | 永远等待 | +| 0 | 立即返回,不阻塞进程 | +| >0 | 等待指定数目的毫秒数 | + +**返回值:** + +Upon successful completion, poll() shall return a non-negative value. A positive value indicates the total number of pollfd structures that have selected events (that is, those for which the revents member is non-zero). A value of 0 indicates that the call timed out and no file descriptors have been selected. Upon failure, poll() shall return -1 and set errno to indicate the error. + +**示例:** + +```cpp +int main(void) +{ + int fd = open("example", O_RDONLY|O_NONBLOCK); + struct pollfd pfd = { + .fd = fd, + .events = POLLPRI, + .revents = 0 + }; + int ret = poll(&pfd, 1, 1000); + close(fd); + return ret; +} +``` + ## 3.Posix ### 3.1.pthread_create 函数