2016-05-19 6 views
0

私は2つの子プロセスを作成し、それらをパイプに接続しようとしていますが、コードが間違っています。フォークを2回使用する - 奇妙な動作

int des_p[2]; 
if(pipe(des_p) == -1) { 
    perror("Pipe failed"); 
    exit(1); 
} 
if((this->procId = fork())==0){ //filter for only the SON process to pass 
    close(STDOUT_FILENO); //closing stdout 
    dup(des_p[1]);  //replacing stdout with pipe write 
    close(des_p[0]); //closing pipe read 
    close(des_p[1]); //closing pipe write 

    execvp(op1, argsp1); 
    perror("execvp failed"); 
    exit(1); 
} 
if(this->procIdSecondary = fork() == 0)   //creating 2nd child, also a filter for only the SON process to pass 
{ 
    close(STDIN_FILENO); //closing stdin 
    dup(des_p[0]);  //replacing stdin with pipe read 
    close(des_p[1]); //closing pipe write 
    close(des_p[0]); //closing pipe read 

    execvp(op2, argsp2); 
    perror("execvp failed"); 
    exit(1); 
} 
else 
{ 
     cout <<"pid=" << this->procIdSecondary<<endl; 

} 

close(des_p[0]); 
close(des_p[1]); 

`

私はpidを取得= 0

はどのように可能ということでしょうか?

+3

オペレータの優先順位。 if条件の代入の前後にかっこを追加します。 –

答えて

0

あなたの問題は、比較の結果が割り当てられます。このif声明

if(this->procIdSecondary = fork() == 0) 

this->procIdSecondaryにおける演算子の優先順位です。最初のifのようにかっこを追加します。