According to vfork() man page, the behaviour is undefined if vfork() modifies any data, other than pid_t before it calls either _exit or exec family of syscalls.
By this I understand, that if the child process created by vfork() calls exec(), then it can modify any data, and the behaviour is still not undefined.
My questions are:
-
It is also known that child shares parent address space, so how come if child overwrites, self and parent image using exec, the behaviour is not undefined?
-
What happens to parent, if the child calls exec and after that it returns? Does the parent start using the new copy, created by child using exec?
I think your key misunderstanding is what
execdoes: it does not “overwrite memory” with the new process. Rather it throws away its entire virtual memory (whether it was previously private mappings, shared mappings, or whatever) and creates a completely new virtual address space for the calling process id corresponding to the new process image (executable). This has no bearing on the parent’s address space except that the reference count on the memory management structures is decremented (it was incremented byvfork).