linux C编程问题,子进程往pipe里写,子进程再fork出来的子进程为什么读不了,求大神解答。
#include<sys/types.h>#include<unistd.h>#include<cstring>#include<iostream>#include<st...
#include<sys/types.h>
#include<unistd.h>
#include<cstring>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
int main(){
int fd[2];
if(pipe(fd)!=0){
std::cout<<"pipe failed"<<std::endl;
exit(0);
}
pid_t pid=fork();
if(pid<0){
std::cout<<"fork failed"<<std::endl;
exit(0);
}else if(pid > 0){
exit(0);
}else if(pid == 0){
close(fd[0]);
fd[0]=-1;
int n_w = write(fd[1],"XXXXXX",10);
std::cout<<"child1 write :"<<n_w<<"long words"<<std::endl;
pid_t pid1=fork();
if(pid1<0){
std::cout<<"fork1 failed"<<std::endl;
exit(0);
}else if(pid1 > 0){
exit(0);
}else{
std::cout<<"chiled2 process"<<std::endl;
close(fd[1]);
fd[1]=-1;
char buf[1024]={0};
int n_r = read(fd[0],&buf,10);
std::cout<<buf<<":"<<n_r<<std::endl;
if(n_r == -1){
std::cout<<"errno:"<<errno<<std::endl;
}
std::cout<<strlen(buf)<<std::endl;
}
}
return 0;
}
本人菜鸟一枚,想学习下pipe,试验了父进程写,孙子进程读;子进程1写,子进程2读都没问题……
写了个子进程写,孙子进程读,读不出来,求大神解答,程序执行结果如下:
child1 write :10long words
chiled2 process
:-1
errno:9
0 展开
#include<unistd.h>
#include<cstring>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
int main(){
int fd[2];
if(pipe(fd)!=0){
std::cout<<"pipe failed"<<std::endl;
exit(0);
}
pid_t pid=fork();
if(pid<0){
std::cout<<"fork failed"<<std::endl;
exit(0);
}else if(pid > 0){
exit(0);
}else if(pid == 0){
close(fd[0]);
fd[0]=-1;
int n_w = write(fd[1],"XXXXXX",10);
std::cout<<"child1 write :"<<n_w<<"long words"<<std::endl;
pid_t pid1=fork();
if(pid1<0){
std::cout<<"fork1 failed"<<std::endl;
exit(0);
}else if(pid1 > 0){
exit(0);
}else{
std::cout<<"chiled2 process"<<std::endl;
close(fd[1]);
fd[1]=-1;
char buf[1024]={0};
int n_r = read(fd[0],&buf,10);
std::cout<<buf<<":"<<n_r<<std::endl;
if(n_r == -1){
std::cout<<"errno:"<<errno<<std::endl;
}
std::cout<<strlen(buf)<<std::endl;
}
}
return 0;
}
本人菜鸟一枚,想学习下pipe,试验了父进程写,孙子进程读;子进程1写,子进程2读都没问题……
写了个子进程写,孙子进程读,读不出来,求大神解答,程序执行结果如下:
child1 write :10long words
chiled2 process
:-1
errno:9
0 展开
1个回答
展开全部
22 行在子进程开始把读文件描述符关了, 孙子当然读不出来. 改了就行了:
#include<sys/types.h>
#include<unistd.h>
#include<cstring>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
int main(){
int fd[2];
if(pipe(fd)!=0){
std::cout<<"pipe failed"<<std::endl;
exit(0);
}
pid_t pid=fork();
if(pid<0){
std::cout<<"fork failed"<<std::endl;
exit(0);
}else if(pid > 0){
exit(0);
}else if(pid == 0){
/** 孙进程还要读,这里不能关掉
close(fd[0]);
fd[0]=-1;
*/
int n_w = write(fd[1],"XXXXXX",10);
std::cout<<"child1 write :"<<n_w<<"long words"<<std::endl;
pid_t pid1=fork();
if(pid1<0){
std::cout<<"fork1 failed"<<std::endl;
exit(0);
}else if(pid1 > 0){
exit(0);
}else{
std::cout<<"chiled2 process"<<std::endl;
close(fd[1]);
fd[1]=-1;
char buf[1024]={0};
int n_r = read(fd[0],&buf,10);
std::cout<<buf<<":"<<n_r<<std::endl;
if(n_r == -1){
std::cout<<"errno:"<<errno<<std::endl;
}
std::cout<<strlen(buf)<<std::endl;
}
}
return 0;
}
执行结果:
child1 write :10long words
chiled2 process
XXXXXX:10
6
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询