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 < 100000) {
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));
40 printf("%d open\n",fd_in);
43 if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
44 printf("fcntl get failed\n");
50 if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
51 printf("fcntl get failed\n");
59 FD_SET(fd_out, &readSet);
62 res = select(fd_out + 1, &readSet, NULL, NULL, NULL);
65 if (errno == EINTR || errno == EAGAIN) {
69 fprintf(stderr,"select failed errno=%d errstr=%s\n", errno, strerror(errno));
72 break; /* we're done */
76 if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
77 printf("fcntl set failed\n");
83 if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
84 printf("fcntl get failed\n");
91 if (fcntl(fd_in, F_SETFL, flag2 & ~O_NONBLOCK) == -1) {
92 printf("fcntl set failed\n");
97 if ((flag2 = fcntl(fd_in, F_GETFL)) == -1) {
98 printf("fcntl get failed\n");
103 if (flag & O_NONBLOCK == 0) {
104 printf("fd_out still nonblocking\n");
108 if (flag & O_NONBLOCK == 0) {
109 printf("fd_in still nonblocking\n");
112 if ((fp = fdopen(fd_out, "r")) == NULL) {
113 printf("fdopen failed\n");
117 while (fgets(buf, sizeof(buf), fp) != NULL) {
122 printf("Test returned different results - run again to verify\n");
131 printf("test successful.\n");