2 #include <sys/select.h>
12 FILE *fp = NULL, *fp_in = NULL;
13 FILE *out_fp = NULL, *diff_fp = NULL;
14 const char* topcmd = "fe/test.out";
15 const char* top_in_file = "fe/test.in";
17 int fd_in = -1, fd_out;
21 struct timeval tv={.tv_sec=5,.tv_usec=0};
23 while (count < 1000000) {
29 printf("(%d)", count);fflush(stdout);
30 if ((fd_out = open(topcmd, O_RDONLY | O_NONBLOCK)) < 0) {
31 fprintf(stderr, "error executing top\n");
35 //printf("((0))");fflush(stdout);
36 while ((fd_in = open(top_in_file, O_WRONLY| O_NONBLOCK)) < 0) {
37 fprintf(stderr, "Waiting for %s (%s)\n", top_in_file,strerror(errno));
42 if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
43 printf("fcntl get failed\n");
49 if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
50 printf("fcntl get failed\n");
58 FD_SET(fd_out, &readSet);
61 res = select(fd_out + 1, &readSet, NULL, NULL, NULL);
64 if (errno == EINTR || errno == EAGAIN) {
68 fprintf(stderr,"select failed errno=%d errstr=%s\n", errno, strerror(errno));
71 break; /* we're done */
75 if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
76 printf("fcntl set failed\n");
82 if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
83 printf("fcntl get failed\n");
90 if (fcntl(fd_in, F_SETFL, flag2 & ~O_NONBLOCK) == -1) {
91 printf("fcntl set failed\n");
96 if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
97 printf("fcntl get failed\n");
102 if (flag & O_NONBLOCK == 0) {
103 printf("fd_out still nonblocking\n");
107 if (flag & O_NONBLOCK == 0) {
108 printf("fd_in still nonblocking\n");
111 if ((fp = fdopen(fd_out, "r")) == NULL) {
112 printf("fdopen failed\n");
116 while (fgets(buf, sizeof(buf), fp) != NULL) {
121 printf("Test returned different results - run again to verify\n");
130 printf("test successful.\n");