0
$\begingroup$

What does it mean that "replace the process's memory space with a new program"? So who's memory space will be replaced by whom?

fork1

$\endgroup$

1 Answer 1

3
$\begingroup$

A process memory space is :

enter image description here

and when you call fork with a process, the entire address space of that process gets copied, and we allocate some part of the memory to it, and when we call exec after doing fork, it replaces that copy (the child) with a new program. (note that we usually call exec only with the child, we do this in our code using the fact that the child will return 0 to the fork value and the parent will return the process id of that child, so we use the if command and use exec if its the child)

so to answer your question, we basically replace the address space of the child that was created, with another program.

$\endgroup$
2
  • $\begingroup$ Re, "...and when we call exec after doing fork..." Just to be perfectly, crystal clear, it is the child process that calls exec(...) to replace its own address space with an image freshly loaded from some executable file. $\endgroup$ Jul 26, 2018 at 19:41
  • $\begingroup$ @jameslarge That is correct, the child process uses exec to replace its own address space; although i already said this in my answer tho, also in some scenarios we might want to use exec with the parents too tho, like the parent might be a child of a process itself or maybe we have other reasons to do so, but most of the times its just the child. $\endgroup$
    – John P
    Jul 26, 2018 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.