Pipe in C UNIX shell

后端 未结 2 1493
抹茶落季
抹茶落季 2021-01-21 00:17

I am not very sure how to create a pipe between two child processes. This is the way I did it:

pipe(&fd[0]);                               //Create a pipe
pr         


        
2条回答
  •  温柔的废话
    2021-01-21 00:51

    In the child process 1 setup, close(STD_INPUT); dup(fd[1]); will duplicate fd[1] to the lowest available descriptor, which is 0 (STD_INPUT) rather than 1 (STD_OUTPUT). Don't you want to close STD_OUTPUT instead? Also, I'd recommend dup2(fd[1], STD_OUTPUT) in place of this sequence; it will do what you want and is more clear anyway.

    Likewise child process 2 uses STD_OUTPUT where you probably want STD_INPUT.

提交回复
热议问题