5 * Select works on regular files, (pseudo) terminal devices, streams-based
6 * files, FIFOs, pipes, and sockets. This test verifies selecting with a time
13 #include <sys/types.h>
14 #include <sys/select.h>
22 #define DO_HANDLEDATA 1
27 #define DELTA(x,y) (x.tv_sec - y.tv_sec) * CLOCKS_PER_SEC \
28 + (x.tv_usec - y.tv_usec) * CLOCKS_PER_SEC / 1000000
30 int errct
= 0, subtest
= -1, got_signal
= 0;
33 void catch_signal(int sig_no
) {
37 void e(int n
, char *s
) {
38 printf("Subtest %d, error %d, %s\n", subtest
, n
, s
);
40 if (errct
++ > MAX_ERROR
) {
41 printf("Too many errors; test aborted\n");
46 float compute_diff(struct timeval start
, struct timeval end
, float compare
) {
47 /* Compute time difference. It is assumed that the value of start <= end. */
49 int seconds
, hundreths
;
52 delta
= DELTA(end
, start
); /* delta is in ticks */
53 seconds
= (int) (delta
/ CLOCKS_PER_SEC
);
54 hundreths
= (int) (delta
* 100 / CLOCKS_PER_SEC
) - (seconds
* 100);
56 diff
= seconds
+ (hundreths
/ 100.0);
58 if(diff
< 0) diff
*= -1; /* Make diff a positive value */
66 /* Let the parent do initial read and write tests from and to the pipe. */
67 tv
.tv_sec
= DO_PAUSE
+ DO_PAUSE
+ DO_PAUSE
+ 1;
69 (void) select(0, NULL
, NULL
, NULL
, &tv
);
71 /* At this point the parent has a pending select with a DO_TIMEOUT timeout.
72 We're going to interrupt by sending a signal */
73 if(kill(getppid(), SIGUSR1
) < 0) perror("Failed to send signal");
78 void do_parent(int child
) {
80 struct timeval tv
, start_time
, end_time
;
83 /* Install signal handler for SIGUSR1 */
84 signal(SIGUSR1
, catch_signal
);
86 /* Parent and child share an anonymous pipe. Select for read and wait for the
87 timeout to occur. We wait for DO_PAUSE seconds. Let's see if that's
88 approximately right.*/
90 FD_SET(fd_ap
[0], &fds_read
);
94 (void) gettimeofday(&start_time
, NULL
); /* Record starting time */
95 retval
= select(fd_ap
[0]+1, &fds_read
, NULL
, NULL
, &tv
);
96 (void) gettimeofday(&end_time
, NULL
); /* Record ending time */
98 /* Did we time out? */
99 if(retval
!= 0) e(1, "Should have timed out");
101 /* Approximately right? The standard does not specify how precise the timeout
102 should be. Instead, the granularity is implementation-defined. In this
103 test we assume that the difference should be no more than half a second.*/
104 if(compute_diff(start_time
, end_time
, DO_PAUSE
) > DO_DELTA
)
105 e(2, "Time difference too large");
107 /* Let's wait for another DO_PAUSE seconds, expressed as microseconds */
109 FD_SET(fd_ap
[0], &fds_read
);
111 tv
.tv_usec
= DO_PAUSE
* 1000000L;
113 (void) gettimeofday(&start_time
, NULL
); /* Record starting time */
114 retval
= select(fd_ap
[0]+1, &fds_read
, NULL
, NULL
, &tv
);
115 (void) gettimeofday(&end_time
, NULL
); /* Record ending time */
117 if(retval
!= 0) e(3, "Should have timed out");
118 if(compute_diff(start_time
, end_time
, DO_PAUSE
) > DO_DELTA
)
119 e(4, "Time difference too large");
121 /* Let's wait for another DO_PAUSE seconds, expressed in seconds and micro
124 FD_SET(fd_ap
[0], &fds_read
);
125 tv
.tv_sec
= DO_PAUSE
- 1;
126 tv
.tv_usec
= (DO_PAUSE
- tv
.tv_sec
) * 1000000L;
128 (void) gettimeofday(&start_time
, NULL
); /* Record starting time */
129 retval
= select(fd_ap
[0]+1, &fds_read
, NULL
, NULL
, &tv
);
130 (void) gettimeofday(&end_time
, NULL
); /* Record ending time */
132 if(retval
!= 0) e(5, "Should have timed out");
133 if(compute_diff(start_time
, end_time
, DO_PAUSE
) > DO_DELTA
)
134 e(6, "Time difference too large");
136 /* Finally, we test if our timeout is interrupted by a signal */
138 FD_SET(fd_ap
[0], &fds_read
);
139 tv
.tv_sec
= DO_TIMEOUT
;
142 (void) gettimeofday(&start_time
, NULL
); /* Record starting time */
143 retval
= select(fd_ap
[0]+1, &fds_read
, NULL
, NULL
, &tv
);
144 (void) gettimeofday(&end_time
, NULL
); /* Record ending time */
146 if(retval
!= -1) e(7, "Should have been interrupted");
147 if(compute_diff(start_time
, end_time
, DO_TIMEOUT
) < DO_DELTA
)
148 e(8, "Failed to get interrupted by a signal");
150 if(!got_signal
) e(9, "Failed to get interrupted by a signal");
152 waitpid(child
, &retval
, 0);
156 int main(int argc
, char **argv
) {
159 /* Get subtest number */
161 printf("Usage: %s subtest_no\n", argv
[0]);
163 } else if(sscanf(argv
[1], "%d", &subtest
) != 1) {
164 printf("Usage: %s subtest_no\n", argv
[0]);
168 /* Set up anonymous pipe */
169 if(pipe(fd_ap
) < 0) {
170 perror("Could not create anonymous pipe");
175 if(forkres
== 0) do_child();
176 else if(forkres
> 0) do_parent(forkres
);
177 else { /* Fork failed */
178 perror("Unable to fork");
182 exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/