1 #include <stdio.h> 2 #include <poll.h> 3 4 int main() { 5 FILE* f = popen("/bin/bash -c 'for i in 1 2 3; do { echo $i; sleep 1; }; done'", "r"); 6 printf("f=%p\n", f); 7 int fd = fileno(f); 8 printf("fd=%d\n", fd); 9 10 struct pollfd pfd; 11 pfd.fd = fd; 12 pfd.events = POLLIN | POLLRDBAND; 13 14 char buffer[80]; 15 16 while (1) { 17 int rv = poll(&pfd, 1, 500); 18 printf("rv=%d\n", rv); 19 if (rv == 0) 20 continue; 21 if (rv < 0) 22 break; 23 printf("events=%08x revents=%08x\n", pfd.events, pfd.revents); 24 if ((pfd.events & pfd.revents) == 0) 25 break; 26 27 fgets(buffer, 79, f); 28 printf("output: %s", buffer); 29 } 30 31 return 0; 32 } 33