質問

Fork()-ing a process will end up calling do_fork() inside kernel, making an exact copy of itself. When I read through books, it says that child of fork will call exec to create the new process.

example:

ls command on a shell, will create this way.

   sh(Parent)
       |
   sh(Child)
       |
   ls(New Process)

But, I am not able to understand how & where the exec*() is called? Because, All I can see is the shell(child) is what created in fork. But, when and where will the new process be created/executed?

役に立ちましたか?

解決

You have to exec() if you actually want a new program running in one of the processes (usually the child but not absolutely necessary). In your specific case where the shell executes ls, the shell first forks, then the child process execs. But it's important to realise that this is two distinct operations.

All fork() does is give you two (nearly) identical processes and you can then use the return code from fork() to decide if you're the parent (you get the positive PID of the child, or -1 if the fork() failed) or child (you get 0).

See this answer for a description on how fork() and exec() work together (under your control) and how they can be used without each other.

Similar to do_fork(), the exec stuff all boils down to calls to do_execve, located in exec.c.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top