2 * select() example, you can test by doing:
5 * $ cc -o select select.c
8 * And, in other terminal:
10 * $ echo 'Hello, world!' > my-fifo
12 * Luiz Fernando N. Capitulino
13 * <lcapitulino@gmail.com>
19 #include <sys/types.h>
21 #include <sys/select.h>
23 int main(int argc
, char *argv
[])
31 printf("usage: select < fifo-path >");
35 fd
= open(argv
[1], O_RDONLY
);
42 * select() receives a set fo file-descriptors to
43 * monitor. The 'fd_set' data type should be used
44 * to define the set variable.
46 * Before calling select() you have to:
48 * 1. Initialize each set with FD_ZERO()
49 * 2. Add file-descriptors to the set
55 * The first parameter is always confusing to
58 * The rule is simple: it's the highest-numbered
59 * file descriptor in any of the sets, plus 1.
61 * In this example we only have one file-descriptor,
62 * so all we have to do is to add 1 to it. But if you
63 * have, say, 30 file-descriptors, you'll have to
64 * find the highest one and add 1 to it.
66 err
= select(fd
+ 1, &rset
, NULL
, NULL
, NULL
);
72 if (FD_ISSET(fd
, &rset
)) {
74 bytes
= read(fd
, &c
, 1);
83 "Something strange has happend\n");
89 fprintf(stderr
, "select() returned but fd is not set\n");