完善 poll 函数说明.

Signed-off-by: rick.chan <chenyang@autoai.com>
This commit is contained in:
rick.chan 2021-04-13 11:22:46 +08:00
parent e1bf5b3ca1
commit 49fc499c81
1 changed files with 69 additions and 1 deletions

View File

@ -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 <poll.h>
```
**函数原型:**
```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 会立即返回有可读数据。
**参数:**
fdsThe 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 函数