fork(), pipe() and exec() process creation and communication

前端 未结 2 1609
有刺的猬
有刺的猬 2021-01-05 18:48

I have to write program that create process using pipe().

My first task is to write a parent process that generates four child processes using the

2条回答
  •  囚心锁ツ
    2021-01-05 19:23

    If you are supposed to use exec, then you should split your program into two binaries.

    Basically, the code that now gets executed by the child should be in the second binary and should be invoked with exec.

    Before calling one of the exec family of functions, you'll also need to redirect the pipe descriptors to the new process' standard input/output using dup2. This way the code in the second binary that gets exec'd won't be aware of the pipe and will just read/write to the standard input/output.

    It's also worth noting that some of the data you are using now in the child process is inherited from the parent through the fork. When using exec the child won't share the data nor the code of the parent, so maybe you can consider transmitting the needed data through the pipe as well.

提交回复
热议问题