当前位置: 技术问答>linux和unix
linux下读取管道 总是返回1
来源: 互联网 发布时间:2016-07-03
本文导语: #include #include #include #include #include int main() { int pipe_fd[2]; pid_t pid; char buf_r[100]; char* p_wbuf; ssize_t r_num; memset(buf_r,0,sizeof(buf_r)); if ( pipe(pipe_fd)0) ){ printf("%u numbers read from the pipe is %sn",r_num,buf_r); } close...
#include
#include
#include
#include
#include
int main()
{
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char* p_wbuf;
ssize_t r_num;
memset(buf_r,0,sizeof(buf_r));
if ( pipe(pipe_fd)0) ){
printf("%u numbers read from the pipe is %sn",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid > 0){
printf("i'm in parent processn");
close(pipe_fd[0]);
if( (write(pipe_fd[1],"hello",5)!=-1) ){
printf("parent write1 successn");
}
if( (write(pipe_fd[1]," pipe",5)!=-1) ){
printf("parent write2 successn");
}
close(pipe_fd[1]);
//sleep(3);
waitpid(pid,NULL,0); /*wait for pid process done*/
exit(0);
}
return 0;
}
为什么总是返回1?谢谢啦
|
> 的优先级比 = 高
所以,如果不加括号,编译器就会以为是
if( (r_num = ( read(pipe_fd[0],buf_r,100)>0) ) ){
所以,如果不加括号,编译器就会以为是
if( (r_num = ( read(pipe_fd[0],buf_r,100)>0) ) ){