板友們晚安,我想透過Linux C, popen執行一個binary,並獲得binary的stdout
單執行binary 是正常的。會印出 start test,也會每隔3秒印出
但我如果透過另支程式 popen 加 fgets就會卡住,試著 fflush(fp)也無用,請問
有人知道是啥問題嗎? 謝謝。
binary code:
int main () {
int ret = 0;
struct pollfd fds[1] = {0};
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
char rbuf[1024] = {0};
struct itimerspec itval;
itval.it_interval.tv_sec = 3;
itval.it_interval.tv_nsec = 0;
itval.it_value.tv_sec = 3;
itval.it_value.tv_nsec = 0;
fd[0].fd = fd;
fds[0]│events = POLLIN;
timerfd_settime (fd, 0, &itval, NULL);
printf("start test\n");
while(1) {
ret = poll(fds, 1, -1);
if(ret < 0) {
printf("poll fail\n");
} else if(ret == 0) {
printf("time out\n");
} else {
read(fds[0].fd, &rbuf, 1024);
}
printf("finish poll\n");
}
}
test.c:
int main()
{
char line[1024] = {0};
FILE *fp = popen("./burn", "r");
fflush(fp);
while(fgets(line, 1022, fp) != NULL) {
printf("line:%s\n", line);
}
printf("123\n");
return 0;
}