Pipe in C UNIX shell

后端 未结 2 1468
抹茶落季
抹茶落季 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:43

    The best answer I can give is Beej's Guide to Unix IPC.

    (Peek at section 4.3, where he gives a very similar example to the question you asked...)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题