1 | /*
|
---|
2 | * Test name: test02.c
|
---|
3 | *
|
---|
4 | * Objetive: The purpose of this test is to make sure that select works
|
---|
5 | * when working with files.
|
---|
6 | *
|
---|
7 | * Description: This test shows more cases than in test02.c, where every
|
---|
8 | * descriptor is ready. Here in one select call only half of the fd's will
|
---|
9 | * be ready and in the next one none of them will be ready.
|
---|
10 | *
|
---|
11 | * Jose M. Gomez
|
---|
12 | */
|
---|
13 |
|
---|
14 | #include <sys/types.h>
|
---|
15 | #include <fcntl.h>
|
---|
16 | #include <unistd.h>
|
---|
17 | #include <sys/select.h>
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <stdlib.h>
|
---|
20 | #include <limits.h>
|
---|
21 | #include <sys/time.h>
|
---|
22 |
|
---|
23 | void dump_fdset(fd_set *set) {
|
---|
24 | int i;
|
---|
25 | for (i = 0; i < OPEN_MAX; i++)
|
---|
26 | if (FD_ISSET(i, set))
|
---|
27 | printf(" %d ", i);
|
---|
28 | printf("\n");
|
---|
29 | }
|
---|
30 |
|
---|
31 | void main(void) {
|
---|
32 | int fd1, fd2, fd3, fd4, fd5, fd6; /* file descriptors of files */
|
---|
33 | fd_set fds_read, fds_write; /* bit maps */
|
---|
34 | struct timeval timeout; /* timeout */
|
---|
35 | int retval; /* ret value */
|
---|
36 |
|
---|
37 | /* Creates the dummy files with different modes */
|
---|
38 | fd1 = open("dummy1.txt", O_CREAT | O_RDONLY);
|
---|
39 | if (fd1 < 0) {
|
---|
40 | perror("Error opening file");
|
---|
41 | exit(-1);
|
---|
42 | }
|
---|
43 |
|
---|
44 | fd2 = open("dummy2.txt", O_CREAT | O_RDONLY);
|
---|
45 | if (fd2 < 0) {
|
---|
46 | perror("Error opening file");
|
---|
47 | exit(-1);
|
---|
48 | }
|
---|
49 |
|
---|
50 | fd3 = open("dummy3.txt", O_CREAT | O_WRONLY);
|
---|
51 | if (fd3 < 0) {
|
---|
52 | perror("Error opening file");
|
---|
53 | exit(-1);
|
---|
54 | }
|
---|
55 |
|
---|
56 | fd4 = open("dummy4.txt", O_CREAT | O_WRONLY);
|
---|
57 | if (fd4 < 0) {
|
---|
58 | perror("Error opening file");
|
---|
59 | exit(-1);
|
---|
60 | }
|
---|
61 |
|
---|
62 | fd5 = open("dummy5.txt", O_CREAT | O_RDWR);
|
---|
63 | if (fd5 < 0) {
|
---|
64 | perror("Error opening file");
|
---|
65 | exit(-1);
|
---|
66 | }
|
---|
67 |
|
---|
68 | fd6 = open("dummy6.txt", O_CREAT | O_RDWR);
|
---|
69 | if (fd6 < 0) {
|
---|
70 | perror("Error opening file");
|
---|
71 | exit(-1);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* Create the fd_set structures */
|
---|
75 | FD_ZERO(&fds_read);
|
---|
76 | FD_ZERO(&fds_write);
|
---|
77 | FD_SET(fd1, &fds_write); /* fd1 => O_RDONLY */
|
---|
78 | FD_SET(fd2, &fds_write); /* fd2 => O_RDONLY */
|
---|
79 | FD_SET(fd3, &fds_read); /* fd3 => O_WRONLY */
|
---|
80 | FD_SET(fd4, &fds_read); /* fd4 => O_WRONLY */
|
---|
81 | FD_SET(fd5, &fds_read); /* fd5 => O_RDWR */
|
---|
82 | FD_SET(fd5, &fds_write); /* fd5 => O_RDWR */
|
---|
83 | FD_SET(fd6, &fds_read); /* fd6 => O_RDWR */
|
---|
84 | FD_SET(fd6, &fds_write); /* fd6 => O_RDWR */
|
---|
85 |
|
---|
86 | printf("* Dump INPUT fds_read:\n");
|
---|
87 | dump_fdset(&fds_read);
|
---|
88 | printf("* Dump INPUT fds_write:\n");
|
---|
89 | dump_fdset(&fds_write);
|
---|
90 |
|
---|
91 | retval=select(9, &fds_read, &fds_write, NULL, NULL);
|
---|
92 | printf("\n***********************\n");
|
---|
93 | printf("After select: \n");
|
---|
94 | printf("Return value: %d\n", retval);
|
---|
95 | printf("* Dump RESULTING fds_read:\n");
|
---|
96 | dump_fdset(&fds_read);
|
---|
97 | printf("* Dump RESULTING fds_write:\n");
|
---|
98 | dump_fdset(&fds_write);
|
---|
99 |
|
---|
100 | /* make a select call where none of them are ready (don't use fd5 and fd6) */
|
---|
101 |
|
---|
102 | FD_ZERO(&fds_read);
|
---|
103 | FD_ZERO(&fds_write);
|
---|
104 | FD_SET(fd1, &fds_write); /* fd1 => O_RDONLY */
|
---|
105 | FD_SET(fd2, &fds_write); /* fd2 => O_RDONLY */
|
---|
106 | FD_SET(fd3, &fds_read); /* fd3 => O_WRONLY */
|
---|
107 | FD_SET(fd4, &fds_read); /* fd4 => O_WRONLY */
|
---|
108 |
|
---|
109 | /* make a select call where none of them are ready (don't use fd5 and fd6) */
|
---|
110 | /* create a timeout as well */
|
---|
111 | timeout.tv_sec = 5;
|
---|
112 | timeout.tv_usec = 0;
|
---|
113 | retval=select(7, &fds_read, &fds_write, NULL, NULL);
|
---|
114 | printf("\n***********************\n");
|
---|
115 | printf("After select: \n");
|
---|
116 | printf("Return value: %d\n", retval);
|
---|
117 | printf("* Dump RESULTING fds_read:\n");
|
---|
118 | dump_fdset(&fds_read);
|
---|
119 | printf("* Dump RESULTING fds_write:\n");
|
---|
120 | dump_fdset(&fds_write);
|
---|
121 |
|
---|
122 | /* close and delete dummy files */
|
---|
123 | close(fd1);
|
---|
124 | close(fd2);
|
---|
125 | close(fd3);
|
---|
126 | close(fd4);
|
---|
127 | close(fd5);
|
---|
128 | close(fd6);
|
---|
129 | unlink("dummy1.txt");
|
---|
130 | unlink("dummy2.txt");
|
---|
131 | unlink("dummy3.txt");
|
---|
132 | unlink("dummy4.txt");
|
---|
133 | unlink("dummy5.txt");
|
---|
134 | unlink("dummy6.txt");
|
---|
135 | }
|
---|