c - 为 poll 函数创建描述符

标签 c multithreading pipe

我发现 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 supports poll(2) (and analogously epoll(7)) and select(2), as follows:

  • The file descriptor is readable (the select(2) readfds argument; the poll(2) POLLIN flag) if the counter has a value greater than 0.

  • The file descriptor is writable (the select(2) writefds argument; the poll(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/

相关文章:

比较 c 上的字符(字母)

c - 代码中的语义错误

c - libpcap IP 数据包重组

java - 多线程执行顺序

python - 如何在不使用线程作业更改其位置的情况下更新字符串的值? [Python]

python - 在Windows中使用Python脚本检查程序是否挂起/崩溃?另外,管道

c - ARM交叉编译ZeroMQ zstr_rcv()给出段错误

linux - 将 cat 的输出重定向到排序

c - 这些功能有什么作用?

c# - 队列竞争条件