/* Program: test_timeout.c */ #include #include #include #include #include #include /* timespec definition */ #include #include #include /* timespec definition */ #include int main(void) { printf("\nPoll test\n"); /* Data structure describing a polling request. */ //struct pollfd //{ // int fd; /* File descriptor to poll. */ // short int events; /* Types of events poller cares about. */ // short int revents; /* Types of events that actually occurred. */ //}; int nfound = -1, nfds = -1; int timeout_msecs = 0; struct pollfd fds[1]; nfound = -1; nfds = 1; fds[0].fd = 0; fds[0].events = POLLIN | POLLRDNORM | POLLOUT | POLLRDBAND; fds[0].revents = 0; timeout_msecs = 0; fprintf(stderr,"\nTest 1: timeout_msec=%d\n", timeout_msecs); printf("Input: nfound=%d, fds[0].{fd,events,revents}={%d,%d,%d}\n", nfound, fds[0].fd, fds[0].events, fds[0].revents); nfound = poll(fds, nfds, timeout_msecs); printf("Output: nfound=%d, fds[0].{fd,events,revents}={%d,%d,%d}\n", nfound, fds[0].fd, fds[0].events, fds[0].revents); nfound = -1; nfds = 1; fds[0].fd = 0; fds[0].events = POLLIN | POLLRDNORM | POLLOUT | POLLRDBAND; fds[0].revents = 0; timeout_msecs = 1; fprintf(stderr,"\nTest 2: timeout_msec=%d\n", timeout_msecs); printf("Input: nfound=%d, fds[0].{fd,events,revents}={%d,%d,%d}\n", nfound, fds[0].fd, fds[0].events, fds[0].revents); nfound = poll(fds, nfds, timeout_msecs); printf("Output: nfound=%d, fds[0].{fd,events,revents}={%d,%d,%d}\n", nfound, fds[0].fd, fds[0].events, fds[0].revents); printf("\nSelect test\n"); // typedef struct // { // __fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS]; // # define __FDS_BITS(set) ((set)->__fds_bits) // } fd_set; // struct timeval { // __time_t tv_sec; /* Seconds. */ // __suseconds_t tv_usec; /* Microseconds. */ // }; /* Code inspired by: http://bugs.debian.org/79358*/ fd_set rfds; struct timeval timeout_usecs = {0,0}; nfound = -1; nfds = 1; FD_ZERO(&rfds); FD_SET(0, &rfds); timeout_usecs.tv_sec = 0; timeout_usecs.tv_usec = 0; fprintf(stderr,"\nTest 1: timeout_usecs=(%ld,%ld)\n", timeout_usecs.tv_sec,timeout_usecs.tv_usec); printf ("Press Enter "); fflush(stdout); sleep(3); nfound = select(nfds, &rfds, NULL, NULL, &timeout_usecs); printf("Output: nfound=%d\n",nfound); nfound = -1; nfds = 1; FD_ZERO(&rfds); FD_SET(0, &rfds); timeout_usecs.tv_sec = 0; timeout_usecs.tv_usec = 1; fprintf(stderr,"\nTest 2: timeout_usecs=(%ld,%ld)\n", timeout_usecs.tv_sec,timeout_usecs.tv_usec); printf("Input: nfound=%d\n", nfound); printf ("Press Enter "); fflush(stdout); sleep(3); nfound = select(nfds, &rfds, NULL, NULL, &timeout_usecs); printf("Output: nfound=%d\n",nfound); return 0; }