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

前端 未结 2 1254
無奈伤痛
無奈伤痛 2021-01-05 18:47

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:11

    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.

    0 讨论(0)
  • 2021-01-05 19:16

    The exec family of functions is used to replace the current process with a new process. Note the use of the word replace. Once exec is called, the current process is gone and the new process starts. If you want to create a separate process, you must first fork, and then exec the new binary within the child process.

    Using the exec functions is similar to executing a program from the command line. The program to execute as well as the arguments passed to the program are provided in the call to the exec function.

    For example, the following execcommand* is the equivalent to the subsequent shell command:

    execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

    /bin/ls -r -t -l

    * Note that "arg0" is the command/file name to execute


    Since this is homework, it is important to have a good understanding of this process. You could start by reading documentation on pipe, fork, and exec combined with a few tutorials to gain a better understanding each step.

    The following links should help to get you started:

    • IBM developerWorks: Delve into UNIX process creation
    • YoLinux Tutorial: Fork, Exec and Process control
    • Pipe, Fork, Exec and Related Topics
    0 讨论(0)
提交回复
热议问题