Welcome to the Linux Foundation Forum!

Chapter 03 - Child inherits PID of parent with exec()?

I am a bit confused with this part of the chapter 03 - Creating Processes (about Fork and Exec):

Often, rather than just a fork, one follows it with an exec, where the parent process terminates, and the child process inherits the process ID of the parent.

When a process forks, the child gets a new PID. But if the child does an exec(), it terminates the parent and inherits its PID?

What if a process creates multiple forks and each child runs the exec()? Would that create multiple processes with the same PID?

Doesn't terminal emulators use this fork-exec pattern to run the commands that we type? Running a command foo (that I would expect to fork and exec(foo)) does not kill the terminal (the parent process). So, terminals don't use this pattern?

Comments

  • Hi @heitorpb ,

    I'd suggest starting by reading the man pages for exec(3), execve(2), and fork(2). Fork(2) will provide you with valuable information about how it works.

    I also found a couple of URLs that can be of help:

    https://stackoverflow.com/questions/1653340/differences-between-fork-and-exec

    https://programmerbay.com/difference-between-fork-and-exec/

    Regards,
    Luis.

  • coop
    coop Posts: 862

    It's actually pretty simple. We are not talking about doing a fork() and then an exec*(). Note the text says:

    Often, rather than just a fork, one follows it with an exec, where the parent process terminates, and the child process inherits the process ID of the parent.

    Note the rather. an exec creates a new process with the same ID, one doesn't do both.

  • Thanks for the answers! Those man pages were very helpful.

    Often, rather than just a fork, one follows it with an exec, where the parent process terminates, and the child process inherits the process ID of the parent.

    I think my main source of confusion was English here (English is not my first language)... what I understood of this sentence is that if a child does an exec(), the parent dies and the child changes its PID to the parent PID.

    Is it possible to suggest to rewrite this paragraph in the course material? I would be glad to help :)

    Just to be sure that I understood this:

    • if a process does a fork() and it succeeds, it clones itself. The child is another process with a different PID.
    • if a process does an exec(), regardless of being a child or not, it changes its own program to a different one. This does not affect the parent of the process doing the exec. This does not change the PID of the process, only the program running.
    • if a process has multiple threads, they are all terminated when one of them calls exec. The thread that called exec will survive, but with a different program.

    Are those items correct?

    In the case that a process has multiple threads: what happens to the other threads if the execve fails? Do they terminate as well?

Categories

Upcoming Training