t40c term[] count fix
[minix.git] / test / select / test03.c
blob14e2eba8d9304244167bc3622603003ce58b42e6
1 /*
2 * Test name: test03.c
4 * Objetive: The purpose of this test is to make sure that select works
5 * when working with files.
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.
11 * Jose M. Gomez
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>
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");
31 int main(int argc, char *argv[])
33 int fd1, fd2, fd3, fd4, fd5, fd6; /* file descriptors of files */
34 fd_set fds_read, fds_write; /* bit maps */
35 struct timeval timeout; /* timeout */
36 int retval; /* ret value */
38 /* Creates the dummy files with different modes */
39 fd1 = open("dummy1.txt", O_CREAT | O_RDONLY);
40 if (fd1 < 0) {
41 perror("Error opening file");
42 exit(-1);
45 fd2 = open("dummy2.txt", O_CREAT | O_RDONLY);
46 if (fd2 < 0) {
47 perror("Error opening file");
48 exit(-1);
51 fd3 = open("dummy3.txt", O_CREAT | O_WRONLY);
52 if (fd3 < 0) {
53 perror("Error opening file");
54 exit(-1);
57 fd4 = open("dummy4.txt", O_CREAT | O_WRONLY);
58 if (fd4 < 0) {
59 perror("Error opening file");
60 exit(-1);
63 fd5 = open("dummy5.txt", O_CREAT | O_RDWR);
64 if (fd5 < 0) {
65 perror("Error opening file");
66 exit(-1);
69 fd6 = open("dummy6.txt", O_CREAT | O_RDWR);
70 if (fd6 < 0) {
71 perror("Error opening file");
72 exit(-1);
75 /* Create the fd_set structures */
76 FD_ZERO(&fds_read);
77 FD_ZERO(&fds_write);
78 FD_SET(fd1, &fds_write); /* fd1 => O_RDONLY */
79 FD_SET(fd2, &fds_write); /* fd2 => O_RDONLY */
80 FD_SET(fd3, &fds_read); /* fd3 => O_WRONLY */
81 FD_SET(fd4, &fds_read); /* fd4 => O_WRONLY */
82 FD_SET(fd5, &fds_read); /* fd5 => O_RDWR */
83 FD_SET(fd5, &fds_write); /* fd5 => O_RDWR */
84 FD_SET(fd6, &fds_read); /* fd6 => O_RDWR */
85 FD_SET(fd6, &fds_write); /* fd6 => O_RDWR */
87 printf("* Dump INPUT fds_read:\n");
88 dump_fdset(&fds_read);
89 printf("* Dump INPUT fds_write:\n");
90 dump_fdset(&fds_write);
92 retval=select(9, &fds_read, &fds_write, NULL, NULL);
93 printf("\n***********************\n");
94 printf("After select: \n");
95 printf("Return value: %d\n", retval);
96 printf("* Dump RESULTING fds_read:\n");
97 dump_fdset(&fds_read);
98 printf("* Dump RESULTING fds_write:\n");
99 dump_fdset(&fds_write);
101 /* make a select call where none of them are ready (don't use fd5 and fd6) */
103 FD_ZERO(&fds_read);
104 FD_ZERO(&fds_write);
105 FD_SET(fd1, &fds_write); /* fd1 => O_RDONLY */
106 FD_SET(fd2, &fds_write); /* fd2 => O_RDONLY */
107 FD_SET(fd3, &fds_read); /* fd3 => O_WRONLY */
108 FD_SET(fd4, &fds_read); /* fd4 => O_WRONLY */
110 /* make a select call where none of them are ready (don't use fd5 and fd6) */
111 /* create a timeout as well */
112 timeout.tv_sec = 5;
113 timeout.tv_usec = 0;
114 retval=select(7, &fds_read, &fds_write, NULL, NULL);
115 printf("\n***********************\n");
116 printf("After select: \n");
117 printf("Return value: %d\n", retval);
118 printf("* Dump RESULTING fds_read:\n");
119 dump_fdset(&fds_read);
120 printf("* Dump RESULTING fds_write:\n");
121 dump_fdset(&fds_write);
123 /* close and delete dummy files */
124 close(fd1);
125 close(fd2);
126 close(fd3);
127 close(fd4);
128 close(fd5);
129 close(fd6);
130 unlink("dummy1.txt");
131 unlink("dummy2.txt");
132 unlink("dummy3.txt");
133 unlink("dummy4.txt");
134 unlink("dummy5.txt");
135 unlink("dummy6.txt");
136 return 0;