我发现 poll() 函数对于多路复用管道和套接字非常有用,但我想对此进行扩展并轮询我自己的介质,例如实现我自己的管道并让它与 poll 一起工作以获取 POLLIN 和 POLLOUT 事件,如何我会这样做吗?
int self = GenerateMyPipe();
int sock = socket(...);
struct pollfd fd[2];
//Init Pollfd and Stuff...
poll(fd, 2, -1);
...
感谢您的阅读...
最佳答案
对此没有标准的 POSIX 方法,但在 Linux 上您可以使用 eventfd()
eventfd()
creates an "eventfd object" that can be used as an event wait/notify mechanism by user-space applications, and by the kernel to notify user-space applications of events. The object contains an unsigned 64-bit integer (uint64_t) counter that is maintained by the kernel. This counter is initialized with the value specified in the argument initval.
...
The returned file descriptor supportspoll(2)
(and analogouslyepoll(7)
) andselect(2)
, as follows:
The file descriptor is readable (the
select(2)
readfds
argument; thepoll(2)
POLLIN
flag) if the counter has a value greater than 0.The file descriptor is writable (the
select(2)
writefds
argument; thepoll(2)
POLLOUT
flag) if it is possible to write a value of at least "1" without blocking.
您可以通过写入描述符来更改计数器。
关于c - 为 poll 函数创建描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44338325/