我编写了一个小测试程序以弄清楚如何与人交谈poll。我创建了三个文件testa,testb,testc和写入字符串hello\n到第一。因此,这是我的调用poll:
poll
testa
testb
testc
hello\n
poll(polls.data(),polls.size(),-1)
根据联机帮助页,超时-1应指示syscall永不超时。但是,它不断返回而没有任何内容可供读取。我总是消耗输入的一个字节,并且可以看到hello\n正在打印的内容,但是民意调查并没有就此停止。它只是假装有一些东西要读。
-1
#include <sys/types.h> #include <sys/stat.h> #include <sys/fcntl.h> #include <sys/poll.h> #include <unistd.h> #include <errno.h> #include <vector> #include <map> #include <string> #include <iostream> typedef int fd_t; int main() { fd_t const a = open("testa",O_RDONLY); fd_t const b = open("testb",O_WRONLY); fd_t const c = open("testc",O_RDWR); std::map<fd_t,std::string> names{{{a,"testa"},{b,"testb"},{c,"testc"}}}; std::vector<pollfd> polls; polls.push_back(pollfd{a, POLLIN, 0}); polls.push_back(pollfd{b, 0, 0}); polls.push_back(pollfd{c, POLLIN, 0}); while (poll(polls.data(),polls.size(),-1)) { for (auto p : polls) { if ((p.revents & (POLLIN|POLLERR)) == POLLIN) { std::cout << "{" << p.fd << ", " << p.events << ", " << p.revents << "} "; char byte; auto const rr = read(p.fd,&byte,1); auto const en = errno; if (rr) { std::cout << "File " << names[p.fd] << " says something: '" << ((int)byte) << " (" << (((' '<byte) && (byte<127))?byte:'\0') << ")" << "' \n"; } else { std::cout << "Strange (file " << names[p.fd] << "). errno says " << en << "\n"; } } } } }
我得到的是:
{3, 1, 1} File testa says something: '104 (h)' {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} File testa says something: '101 (e)' {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} File testa says something: '108 (l)' {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} File testa says something: '108 (l)' {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} File testa says something: '111 (o)' {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} File testa says something: '10 ()' {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} Strange (file testa). errno says 0 {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} Strange (file testa). errno says 0 {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} Strange (file testa). errno says 0 {5, 1, 1} Strange (file testc). errno says 0 {3, 1, 1} Strange (file testa). errno says 0 {5, 1, 1} Strange (file testc). errno says 0
(永远重复最后两行)
我g++ -Wall -Wextra -std=c++11 poll.cpp -o poll在3.10-2-amd64内核上构建。
g++ -Wall -Wextra -std=c++11 poll.cpp -o poll
常规文件中的EOF条件仍然可读。换句话说,您read()将不会受阻。这是一个很好的列表,说明poll()了不同类型的文件描述符中不同的EOF 实现如何反应:http : //www.greenend.org.uk/rjk/tech/poll.html
read()
poll()
请注意,常规文件始终返回POLLIN。因此,您需要单独测试EOF。实际上,对常规文件进行轮询对您没有任何帮助。您将需要套接字或管道或其他东西来测试您的代码。
其他注意事项:您可能要检查中的其他结果.revents。POLLERR,POLLHUP和POLLNVAL都发出不同的错误情况信号,并且需要特殊处理。
.revents