#undef HAVE_POLL_H #define HAVE_POLL_H 1 #include #include #include #include #include // Included by or #include #include /* timespec definition */ #include /* timeval definition */ // Included by if _TIME_H is defined #include /* timeval definition */ #include #ifdef HAVE_POLL_H # include #else /* According to POSIX.1-2001 */ # include #endif /* Struct pollfd defined here */ //struct pollfd { // int fd; /* file descriptor */ // short events; /* requested events */ // short revents; /* returned events */ //}; /* Struct fdset defined here */ /* typedef struct __fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS]; # define __FDS_BITS(set) ((set)->__fds_bits) } fd_set; gcc -E: typedef long int __fd_mask; __fd_mask fds_bits[256 / (8 * (int) sizeof (__fd_mask)) */ int main(void) { int i, j, nfds = 0, nfound; int stime, etime; time_t *currtime = 0; char ans[2] = " "; char c[2] = " "; enum { DELAY = -1, POLL = 0, SELECT = 1 } ispoll; /* poll stuff */ struct pollfd fds[3]; int timeout = 5; /* select stuff */ int fd[3]; fd_set readfds, writefds, exceptfds; struct timeval timevalue = {5,0}; printf("Program test_poll+select\n"); printf("Test poll or select [p,s]: p? "); int key_stroke = 0; for (i = 0; ((char)key_stroke != '\n') && (i < 2); i++) { key_stroke = getchar(); ans[i] = (char)key_stroke; } ans[1] = '\0'; if( ans[0] != '\n') printf("ans = %s\n", ans); printf ("Number of file descriptors [0(delay), 1, 2, 3]: 0? "); c[0] = (char)getchar(); if (c[0] != '\n') { c[1] = '\0'; nfds = strtol(c, NULL, 10); } else c[0] = '\0'; printf( "nfds=%d\n", nfds); if (nfds == 0) ispoll = DELAY; else if (strcmp(ans,"s") != 0) ispoll = POLL; else ispoll = SELECT; switch (ispoll) { case DELAY: printf("\nDELAY test\n"); printf("\nInput data\n"); printf("timeout=%d seconds.\n", timeout); printf("\nOutput data\n"); if (strcmp(ans,"s") != 0) { stime = (int)time(currtime); nfound = poll(fds, 0, timeout*1000); etime = (int)time(currtime); printf("POLL timeout delay= %d(%d) seconds.\n", timeout, etime-stime); } else { stime = (int)time(currtime); nfound = select(0, NULL, NULL, NULL, &timevalue); etime = (int)time(currtime); printf("SELECT timeout delay= %d(%d) seconds.\n", (int)timevalue.tv_sec, etime-stime); } break; return 0; case POLL: printf("\nPOLL test\n"); for(i = 0; i <= 2; ++i) { fds[i].fd = i; fds[i].revents = 0; } fds[0].events = POLLIN; fds[1].events = POLLOUT; fds[2].events = POLLIN | POLLOUT; printf("#define POLLIN 001 /* There is data to read. */\n"); printf("#define POLLPRI 002 /* There is urgent data to read. */\n"); printf("#define POLLOUT 004 /* Writing now will not block. */\n"); printf("#define POLLERR 010 /* Error condition. */\n"); printf("#define POLLHUP 020 /* Hung up. */\n"); printf("#define POLLNVAL 040 /* Invalid polling request. */\n"); printf("\nInput data\n"); printf("timeout=%d seconds.\n", timeout); for (j=0; j < nfds; ++j) printf("fds[%d].{fd,events,revents}={%d,%d,%d}\n", j, fds[j].fd, fds[j].events, fds[j].revents); stime = (int)time(currtime); nfound = poll(fds, (unsigned int)nfds, timeout*1000); etime = (int)time(currtime); printf("\nOutput data\n"); printf("nfound=%d\n", nfound); for (j=0; j < nfds; ++j) { printf( "fds[%d].{fd,events,revents}={%d,%d,%d}\n", j, fds[j].fd, fds[j].events, fds[j].revents); if (nfound == -1) { perror("Poll error"); printf("error=%s, revents[%d]=%d\n", strerror(errno), j, fds[j].revents); printf("Timed out %d(%d) seconds.\n", timeout, etime-stime); } else if (nfound == 0) printf("Timed out %d(%d) seconds.\n", timeout, etime-stime); else printf("Data is available now, delay=%d\n", etime-stime); } break; return 0; case SELECT: printf("\nSELECT test\n"); for(i = 0; i <= 2; ++i) fd[i] = i; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); FD_SET(fd[0], &readfds); FD_SET(fd[1], &writefds); FD_SET(fd[2], &exceptfds); if (nfds == 1) { /* Watch stdin (fd 0) to see when it has input. */ stime = (int)time(currtime); nfound = select(nfds + 1, &readfds, (fd_set*)0, (fd_set*)0, &timevalue); etime = (int)time(currtime); } else if (nfds == 2) { stime = (int)time(currtime); // nfound = select(nfds + 1, &readfds, &writefds, (fd_set*)0, &timevalue); nfound = select(nfds + 1, &readfds, (fd_set*)0, &exceptfds, &timevalue); etime = (int)time(currtime); } else { stime = (int)time(currtime); nfound = select(nfds + 1, &readfds, &writefds, &exceptfds, &timevalue); etime = (int)time(currtime); } printf("nfound=%d\n", nfound); if (nfound == -1) perror("select()"); else if (nfound) printf("Data is available now, delay=%d\n", etime-stime); else printf("No data within %d(%d) seconds.\n", (int)timevalue.tv_sec, etime-stime); break; return 0; } return 0; }