[9] | 1 | /*
|
---|
| 2 | * Test name: test01.c
|
---|
| 3 | *
|
---|
| 4 | * Objective: The purpose of this test is to make sure that the timeout mechanisms
|
---|
| 5 | * work without errors.
|
---|
| 6 | *
|
---|
| 7 | * Description: Executes a select as if it was a sleep and compares the time it
|
---|
| 8 | * has been actually sleeping against the specified time in the select call.
|
---|
| 9 | * Three cases are tested: first, a timeout specified in seconds, second, a timeout in
|
---|
| 10 | * microseconds, and third, a timeout with more precision than seconds.
|
---|
| 11 | *
|
---|
| 12 | * Jose M. Gomez
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | #include <sys/types.h>
|
---|
| 16 | #include <sys/select.h>
|
---|
| 17 | #include <sys/time.h>
|
---|
| 18 | #include <time.h>
|
---|
| 19 | #include <stdio.h>
|
---|
| 20 | #include <errno.h>
|
---|
| 21 | #include <string.h>
|
---|
| 22 |
|
---|
| 23 | #define SECONDS 3
|
---|
| 24 | #define USECONDS 3000000L
|
---|
| 25 |
|
---|
| 26 | int main(void) {
|
---|
| 27 | int r;
|
---|
| 28 | time_t start, end; /* variables for timing */
|
---|
| 29 | struct timeval timeout; /* timeout structure */
|
---|
| 30 |
|
---|
| 31 | /* Set timeout for 3 seconds */
|
---|
| 32 | timeout.tv_sec = SECONDS;
|
---|
| 33 | timeout.tv_usec = 0;
|
---|
| 34 | printf("Sleeping now for %d seconds...\n", SECONDS);
|
---|
| 35 | /* Record time before starting */
|
---|
| 36 | start = time(NULL);
|
---|
| 37 | r = select(0, NULL, NULL, NULL, &timeout);
|
---|
| 38 | printf("select return code: %d error: %s\n",
|
---|
| 39 | r, strerror(errno));
|
---|
| 40 | end = time(NULL);
|
---|
| 41 | printf("For a timeout with select of %d seconds, it took %d actual seconds\n",
|
---|
| 42 | SECONDS, end-start);
|
---|
| 43 |
|
---|
| 44 | /* Set timeout for 3 seconds , but specified in microseconds */
|
---|
| 45 |
|
---|
| 46 | timeout.tv_sec = 0;
|
---|
| 47 | timeout.tv_usec = USECONDS;
|
---|
| 48 | printf("\n***************************\n");
|
---|
| 49 | printf("Sleeping now for %ld microseconds...\n", USECONDS);
|
---|
| 50 | /* Record time before starting */
|
---|
| 51 | start = time(NULL);
|
---|
| 52 | r = select(0, NULL, NULL, NULL, &timeout);
|
---|
| 53 | printf("select return code: %d error: %s\n",
|
---|
| 54 | r, strerror(errno));
|
---|
| 55 | end = time(NULL);
|
---|
| 56 | printf("For a timeout with select of %ld useconds, it took %d actual seconds\n",
|
---|
| 57 | USECONDS, end-start);
|
---|
| 58 |
|
---|
| 59 | /* Set timeout for 1.5 seconds, but specified in microseconds */
|
---|
| 60 |
|
---|
| 61 | timeout.tv_sec = 0;
|
---|
| 62 | timeout.tv_usec = USECONDS/2;
|
---|
| 63 | printf("\n***************************\n");
|
---|
| 64 | printf("Sleeping now for %ld microseconds...\n", USECONDS/2);
|
---|
| 65 | /* Record time before starting */
|
---|
| 66 | start = time(NULL);
|
---|
| 67 | r = select(0, NULL, NULL, NULL, &timeout);
|
---|
| 68 | printf("select return code: %d error: %s\n",
|
---|
| 69 | r, strerror(errno));
|
---|
| 70 | end = time(NULL);
|
---|
| 71 | printf("For a timeout with select of %ld useconds, it took %d actual seconds\n",
|
---|
| 72 | USECONDS/2, end-start);
|
---|
| 73 | return 0;
|
---|
| 74 | }
|
---|