don't set page size by default
[tridge/junkcode.git] / select.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 /****************************************************************************
9 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
10 else
11 if SYSV use O_NDELAY
12 if BSD use FNDELAY
13 ****************************************************************************/
14 int set_blocking(int fd, int set)
15 {
16   int val;
17 #ifdef O_NONBLOCK
18 #define FLAG_TO_SET O_NONBLOCK
19 #else
20 #ifdef SYSV
21 #define FLAG_TO_SET O_NDELAY
22 #else /* BSD */
23 #define FLAG_TO_SET FNDELAY
24 #endif
25 #endif
26
27   if((val = fcntl(fd, F_GETFL, 0)) == -1)
28         return -1;
29   if(set) /* Turn blocking on - ie. clear nonblock flag */
30         val &= ~FLAG_TO_SET;
31   else
32     val |= FLAG_TO_SET;
33   return fcntl( fd, F_SETFL, val);
34 #undef FLAG_TO_SET
35 }
36
37 #define LEN 0x1000
38 #define ADDR 0x0
39
40 int main()
41 {
42         int fdpair[2];
43         char buf[LEN];
44
45         pipe(fdpair);
46         if (fork()) {
47                 fd_set  fds;
48                 int no, ret=0;
49
50                 set_blocking(fdpair[0], 0);
51
52                 FD_ZERO(&fds);
53                 FD_SET(fdpair[0], &fds);
54
55                 no = select(32, &fds, NULL, NULL, NULL);
56                 
57                 if (no == 1) {
58                         ret = read(fdpair[0], buf, LEN);
59                         if (ret == 0) {
60                                 fprintf(stderr,"Error: EOF on pipe\n");
61                                 exit(1);
62                         }
63                 }
64                 printf("read %d bytes\n", ret);
65                 waitpid(-1, NULL, 0);
66         } else {
67                 write(fdpair[1], buf, 0);
68                 sleep(1);
69                 write(fdpair[1], buf, LEN);
70                 _exit(0);
71         }
72
73         return 0;
74 }