pollset_create、pollset_ctl、pollset_destroy、pollset_poll、pollset_query、pollset_ctl_ext、pollset_poll_ext、pollset_query_ext 和 pollset_ext 子程序
用途
检查多个文件描述符的 I/O 状态。
库
标准 C 库 (libc.a)
语法
#include <sys/poll.h>
#include <sys/pollset.h>
#include <sys/fcntl.h>
pollset_t ps = pollset_create(int maxfd)
int rc = pollset_destroy(pollset_t ps)
int rc = pollset_ctl(pollset_t ps, struct poll_ctl *pollctl_array,
int array_length)
int rc = pollset_query(pollset_t ps, struct pollfd *pollfd_query)
int nfound = pollset_poll(pollset_t ps,
struct pollfd *polldata_array,
int array_length, int timeout)
int rc = pollset_ctl_ext(pollset_t ps, struct poll_ctl_ext* pollctl_array,
int array_length)
int rc = pollset_query_ext(pollset_t ps, struct pollfd_ext *pollfd_query)
int nfound = pollset_poll_ext(pollset_t ps,
struct pollfd_ext *polldata_array,
int array_length, int timeout)
int pollset_ext(void)
描述
pollset应用编程接口(API)可有效轮询大型文件描述符集。 当文件描述符集不经常更新时,最好使用该接口。 与传统 select 和 poll API 相比, pollset 子例程可以提供显着的性能增强。 当每次轮询操作返回的事件数与轮询的文件描述符数相比较少时,改进效果最为明显。
pollset API 使用系统调用来完成轮询。 成功调用 "pollset_create后,文件描述符集(或pollset)就建立起来了。 使用 "pollset_ctl子程序添加、删除或更新文件描述符和轮询事件。 调用 "pollset_poll子程序执行轮询操作。 调用 "pollset_query子程序查询当前轮询集是否包含文件描述符。 控制、轮询和查询子程序的扩展版本,可方便希望使用扩展轮询集格式功能的应用程序使用。
-1,则属于轮询集的文件描述符的最大数量受 "OPEN_MAX的限制,如 "<sys/limits.h>(每个进程打开文件描述符的AIX®限制)所定义。 AIX在全系统范围内规定了同一时间 245025 个活动轮询集的限制。 如果调用失败,系统将返回 "-1,并适当设置 "errno。 成功后,将返回 "pollset_t类型的投票站 ID: typedef int pollset_t
应用程序不得更改 pollset ID。 pollset API 会验证 ID 是否为 "-1。 此外,应用程序的进程 ID 必须与轮询集创建时存储的进程 ID 一致。
成功调用 "pollset_destroy后,pollset 将被销毁。 调用成功后,系统将返回 "0。 失败时, pollset_destroy 子例程返回 -1 ,并将 errno 设置为相应的代码。 errno为 "EINVAL表示投票站 ID 无效。
必须使用 "pollset_ctl子程序或 "pollset_ctl_ext子程序将文件描述符添加到轮询集,然后才能对其进行监控。 由 "poll_ctl结构和/或 "poll_ctl_ext结构组成的异构数组通过 "pollctl_array传递给 "pollset_ctl或 "pollset_ctl_ext。 array_length参数表示数组中不同 "poll_ctl or poll_ctl_ext元素的个数。
每个 poll_ctl 轮询控制结构都包含一个 version, 命令, fd和 事件 字段。 当版本字段设置为 0 时,该结构与AIX以前版本的定义向后兼容。 当 version 字段设置为 1 时,扩展轮询控制结构 poll_ctl_ext将添加附加字段以存储用户指定的数据。 fd字段定义了命令的目标文件描述符。 events 字段包含感兴趣的事件。 When the is PS_ADD, the pollset_ctl or pollset_ctl_ext call adds a valid open file descriptor to the pollset. 如果文件描述符已在轮询集中,"PS_ADD会导致 "pollset_ctl或 "pollset_ctl_ext返回错误。 当命令为 "PS_MOD且文件描述符已在 "pollset中时,事件字段中的比特将被添加(ORed)到监控事件中。 如果 "pollset中还没有文件描述符,则 "PS_MOD行为等同于 PS_ADD,并将一个有效的打开文件描述符添加到 "pollset中。
Poll events and user-specified data can be refreshed for a file descriptor that is currently resident in the pollset with the PS_REPLACE command. When command is PS_DELETE and the file descriptor is already in the pollset, pollset_ctl or pollset_ctl_ext removes the file descriptor from the pollset. 如果文件描述符不在 "pollset中,则 "PS_DELETE会导致 "pollset_ctl或 "pollset_ctl_ext返回错误。
pollset_query或 "pollset_query_ext接口可用于确定文件描述符与轮询集相关的信息。 当文件描述符在轮询集中时,"pollset_query或 "pollset_query_ext将返回 "1,事件将设置为当前监控的事件。 当出现 "POLLEXT事件时,也会返回用户指定的文件描述符数据。
pollset_poll或 "pollset_poll_ext子程序确定轮询集中哪些文件描述符有事件待处理。 polldata_array参数包含一个缓冲区地址,其中的 "pollfd或 "pollfd_ext结构用于返回具有待处理事件的文件描述符。 轮询返回的事件数受array_length 限制。 超时参数指定在没有事件等待的情况下需要等待的时间。 将超时设置为 "0可保证 "pollset_poll或 "pollset_poll_ext子程序立即返回。 将超时设置为 "-1可指定无限超时。 其他非零正值指定等待时间 (以毫秒计)。
When events are returned from a pollset_poll or pollset_poll_ext operation, each pollfd or pollfd_ext structure contains an fd field with the file descriptor set, an 活动 field with the requested events, and a 修订 field with the events that have occurred. 包含用户指定数据的投票站事件将在 "pollfd_ext结构的数据字段中返回这些数据,并在事件字段中用 "POLLEXT事件表示这些数据的存在。
多线程进程中的多个线程可以访问单个轮询集。 当多个线程轮询一个轮询集,而文件描述符发生事件时,可能只有一个线程被提示接收事件。 文件描述符返回线程后,直到下一次调用 "pollset_poll或 "pollset_poll_ext时才会产生新事件。 此行为可防止在每个事件上提示所有线程。 多个线程可同时执行 "pollset_poll或 "control操作。 如果用户希望对轮询集进行序列化修改,则应将环境变量PS_CTL_BLOCKING设为 "yes,这样 "pollset_ctl或 "pollset_ctl_ext调用就会阻塞,直到 "pollset_poll或 "pollset_poll_ext中的所有运行线程都退出为止。 调用 "pollset_destroy的线程会被阻塞,直到所有线程都离开 pollset 轮询、控制和查询系统调用。
进程可以在调用 pollset_create之后调用 fork 。 The child process will already have a pollset ID per pollset, but pollset_destroy, pollset_ctl, pollset_ctl_ext, pollset_query, pollset_query_ext, pollset_poll, and pollset_poll_ext operations will fail with an errno value of EACCES.
将文件描述符添加到轮询集后,在运行 "pollset_ctl或 "pollset_ctl_ext调用以及 "PS_DELETE命令之前,文件描述符不会被移除。 即使关闭了文件描述符,该文件描述符仍会保留在轮询集中。 A pollset_poll or pollset_poll_ext operation on a pollset containing a closed file descriptor returns a POLLNVAL event for that file descriptor. 如果文件描述符后来分配给了一个新对象,那么在以后调用 "pollset_poll或 "pollset_poll_ext时将轮询该新对象。
应用程序可以使用 "pollset_ext来支持在不支持扩展轮询集功能的AIX版本中运行。 返回值为 "true(1)表示支持扩展轮询集,返回值为 "false(0)表示只支持传统轮询集结构和 API。
参数
| 项 | 描述 |
|---|---|
| array_length | 指定数组参数的长度。 |
| maxfd | 指定轮询集所属文件描述符的最大数量。 |
| pollctl_array | 指向 poll_ctl 和 poll_ctl_ext 结构的均匀或异构数组的指针,这些数组描述文件描述符 (通过 pollfd 或 pollfd_ext 结构) 以及要对每个文件描述符执行的唯一操作 (添加,除去或修改)。 |
| polldata_array | 返回轮询集上发生的请求事件。 |
| pollfd_query | 指向一个文件描述符,该文件描述符可能属于也可能不属于轮询集。 如果属于轮询集,则更新该参数的请求事件字段,以反映当前对该文件描述符的监控内容。 |
| ps | 指定轮询集 ID。 |
| 超时 | 指定等待任何受监视事件发生的时间量 (以毫秒计)。 -1 块的值,直到发生某些受监视事件为止。 |
返回值
成功时, pollset_destroy 子例程返回 0。 失败时, pollset_destroy 子例程返回 -1 ,并将 errno 设置为相应的代码。
成功后,"pollset_create子程序将返回一个 "pollset_t类型的投票站 ID。 如果调用失败,系统将返回 "-1,并适当设置 "errno。
成功时, pollset_ctl 或 pollset_ctl_ext 会返回 0。 发生故障时, pollset_ctl 或 pollset_ctl_exit 将返回 pollctl_array 的基于 0 的问题元素号 (例如,针对元素 3 返回 2)。 如果第一个元素是问题元素,或者在处理元素数组之前发生其他错误,那么将返回 -1 并将 errno 设置为相应的代码。 调用应用程序必须确认成功处理了该问题元素之前的数组中的元素,并且应该尝试使用问题元素之外的 pollctl_array 元素再次调用 pollset_ctl 或 pollset_ctl_ext 。
如果文件描述符不在投票集中,"pollset_query或 "pollset_query_ext将返回 "0。 如果文件描述符在轮询集中,"pollset_query或 "pollset_query_ext将返回 "1,"活动将设置为当前监控的事件,如果存在,"数据字段将设置为用户指定的数据。 如果在尝试确定文件描述符是否为 pollset 成员后出现错误,则 "pollset_query或 "pollset_query_ext将返回 "-1,"errno将设置为相应的返回代码。
pollset_poll 或 pollset_poll_ext 子例程返回发生所请求事件的文件描述符数。 如果在任何文件描述符上都未发生请求的事件,那么将返回 0 。 发生错误并且 errno 设置为相应的代码时,将返回值 -1 。
错误代码
| 项 | 描述 |
|---|---|
| EACCES | 进程没有访问投票站的权限。 |
| EAGAIN | 系统资源暂时不可用。 |
| EFAULT | 提供的地址无效。 |
| EINTR | 在系统调用期间接收到信号。 |
| EINVAL | 参数无效。 |
| ENOMEM | 可用系统内存不足。 |
| ENOSPC | 正在使用的轮询集的最大数量。 |
| EPERM | 进程没有权限创建投票集。 |
| ENOTSUP | 设备不支持所请求的事件组合。 |