9 /*******************************************************************
10 find the difference in milliseconds between two struct timeval
12 ********************************************************************/
13 int TvalDiff(struct timeval *tvalold,struct timeval *tvalnew)
15 return((tvalnew->tv_sec - tvalold->tv_sec)*1000 +
16 ((int)tvalnew->tv_usec - (int)tvalold->tv_usec)/1000);
19 /*******************************************************************
20 sleep for a specified number of milliseconds
21 ********************************************************************/
25 struct timeval tval,t1,t2;
27 gettimeofday(&t1, NULL);
28 gettimeofday(&t2, NULL);
31 tval.tv_sec = (t-tdiff)/1000;
32 tval.tv_usec = 1000*((t-tdiff)%1000);
35 select(0,NULL,NULL, NULL, &tval);
37 gettimeofday(&t2, NULL);
38 tdiff = TvalDiff(&t1,&t2);
42 /****************************************************************************
43 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
47 ****************************************************************************/
48 int set_blocking(int fd, int set)
52 #define FLAG_TO_SET O_NONBLOCK
55 #define FLAG_TO_SET O_NDELAY
57 #define FLAG_TO_SET FNDELAY
61 if((val = fcntl(fd, F_GETFL, 0)) == -1)
63 if(set) /* Turn blocking on - ie. clear nonblock flag */
67 return fcntl( fd, F_SETFL, val);
84 FD_SET(fdpair[1], &fds);
86 if (select(10, NULL, &fds, NULL, NULL) == 1) {
87 printf("writing ..\n"); fflush(stdout);
88 ret = write(fdpair[1], buf, sizeof(buf));
89 printf("wrote %d\n", ret); fflush(stdout);
93 set_blocking(fdpair[0], 0);
95 int ret = read(fdpair[0], buf, 40);