- 论坛徽章:
- 0
|
- int poll_example(int fd)
- {
- int ready;
- struct pollfd pl[1];
- pl[0].fd = fd;
- pl[0].events = POLLIN | POLLOUT;
- ready = poll(pl, (unsigned long)1, -1);
- printf("ready = %d\n", ready);
- printf("%d %s\n", pl[0].fd, (pl[0].revents & POLLIN) ? "readable" : "");
- printf("%d %s\n", pl[0].fd, (pl[0].revents & POLLOUT) ? "writeable" : "");
- printf("%d %s\n", pl[0].fd, (pl[0].revents & POLLERR) ? "error" : "");
- return 0;
- }
复制代码 如果poll只监视一个fd的两个事件“可读”或“可写”
按照man中介绍的poll返回数组pl中,revent不为0的结构体的个数。
也就是说,只要某个fd上有注册的事件发生,则应该将 poll 的返回值 +1,即使fd上注册了两个或者两个以上的事件
如上所示的函数中,参数fd是一个普通文件的fd,并且运行结果 ready = 2
如果将pl[0].events = POLLIN | POLLOUT; 改为 pl[0].events = POLLIN; 运行结果 ready = 1
小弟不太明白,在只监视一个普通文件描述符的情况下,poll会返回2 |
|