返回信息流p1是fork前建立的通道,p2是子进程的运行,player会先向stdout输出一个字符(见p3),p4是父进程读取,read和fread其实只运行一个,但是没有一个读取,子进程改成p5也无法读取,这怎么回事,谢谢各位了!
这是一条镜像帖。来源:北邮人论坛 / cpp / #99905同步于 2020/4/26
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[问题]父进程无法读取子进程的stdout
lpx853063360
2020/4/26镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
fork前建立的通道:
FILE *fpw=fdopen(pipes1[1],"w");
FILE *fpr=fdopen(pipes2[0],"r");
FILE *fcw=fdopen(pipes2[1],"w");
FILE *fcr=fdopen(pipes1[0],"r");
子进程运行的是:
fclose(fpw);
fclose(fpr);
dup2(fileno(fcr),STDIN_FILENO);
dup2(fileno(fcw),STDOUT_FILENO);
fclose(fcw);
fclose(fcr);
execl("./Player","./Player","1","0",NULL);
Player会先向stdout输出一个字符:fprintf(stdout,"^");
父进程读取:
read(pipes2[0],test,4);
//s=fgetc(fpr);
//printf("Parentsssss%c\n",s);
printf("Parent%s\n",test);
fread(test,sizeof(char),4,fpr);
子进程改成这个也无法读取:
fclose(fpw);
fclose(fpr);
dup2(fileno(fcr),STDIN_FILENO);
dup2(STDOUT_FILENO,fileno(fcw));
fclose(fdopen(STDOUT_FILENO,"w"));
fclose(fcr);
execl("./Player","./Player","1","0",NULL);
fclose(fcw);