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);
31 if ((fd_out = open(topcmd, O_RDONLY | O_NONBLOCK)) < 0) {
32 fprintf(stderr, "error executing top\n");
36 if ((fd_in = open(top_in_file, O_WRONLY)) < 0) {
37 fprintf(stderr, "error opening %s\n", top_in_file);
40 if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
41 printf("fcntl get failed\n");
47 FD_SET(fd_out, &readSet);
49 res = select(fd_out + 1, &readSet, NULL, NULL, NULL);
51 if (errno == EINTR || errno == EAGAIN) {
55 fprintf(stderr,"select failed errno=%d errstr=%s\n", errno, strerror(errno));
58 break; /* we're done */
61 if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
62 printf("fcntl set failed\n");
66 if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
67 printf("fcntl get failed\n");
71 if (flag & O_NONBLOCK == 0) {
72 printf("fd_out still nonblocking\n");
76 if ((fp = fdopen(fd_out, "r")) == NULL) {
77 printf("fdopen failed\n");
81 while (fgets(buf, sizeof(buf), fp) != NULL) {
86 printf("Test returned different results - run again to verify\n");
95 printf("test successful.\n");