c++ - Why does poll keep returning although there is no input? -
i wrote small test program figure out how talk poll. created 3 files testa,testb,testc , wrote string hello\n first. so, here invocation of poll:
poll(polls.data(),polls.size(),-1) according manpage, timeout of -1 should indicate syscall never times out. however, keeps returning without having read. consume 1 byte of input , can see hello\n being printed, poll doesn't stop there. keeps on pretending there read.
#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 = 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)) { (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"; } } } } } what this:
{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 (repeats last 2 lines forever)
i'm building g++ -wall -wextra -std=c++11 poll.cpp -o poll on 3.10-2-amd64 kernel.
an eof condition in regular file still readable. in other words, read() won't block. here's nice list of how different implementations of poll() react eof in different sorts of file descriptors: http://www.greenend.org.uk/rjk/tech/poll.html
note regular files return pollin. need test eof separately. in fact, poll on regular file doesn't you. you'll need sockets or pipes or test code.
other notes: want check other results in .revents. pollerr, pollhup, , pollnval signal different error conditions, , need special handling.
Comments
Post a Comment