/* gcc -opolltest2 polltest2.c */ #include #include #include #include #include void callback_alarm(int sig) { return; } int main() { struct pollfd p[3]; memset(p, 0, sizeof(struct pollfd) * 3); p[0].fd = -1; p[1].fd = -1; p[2].fd = -1; printf("all descriptors are negative - poll() must wait for 2 seconds\n"); poll(p, 3, 2000); printf("all descriptors are negative - revents for any descriptor must be 0: %d, %d, %d\n", p[0].revents, p[1].revents, p[2].revents); p[2].fd = 0; printf("no descriptor is ready - poll() must return 0: %d\n", poll(p, 3, 500)); printf("no descriptor is ready - revents for any descriptor must be 0: %d, %d, %d\n", p[0].revents, p[1].revents, p[2].revents); signal(SIGALRM, callback_alarm); alarm(1); printf("SIGALRM - poll() must return -1: %d\n", poll(p, 3, 1500)); printf("SIGALRM - revents for any descriptor must be 0: %d, %d, %d\n", p[0].revents, p[1].revents, p[2].revents); p[1].fd = 1000; p[2].fd = 1001; printf("2 descriptors are invalid - poll() must return 2 immediately: %d\n", poll(p, 3, 10000)); printf("2 descriptors are invalid - revents must be POLLNVAL for 2. and 3. descriptor: %d, %d, %d\n", p[0].revents, p[1].revents, p[2].revents); }