Operating System Concepts and APUE say
With vfork(), the parent process
is suspended, and the child process uses the address space of the parent.
Because vfork() does not use copy-on-write, if the child process changes
any pages of the parent’s address space, the altered pages will be visible to the
parent once it resumes. Therefore, vfork() must be used with caution to ensure
that the child process does not modify the address space of the parent.vfork()
is intended to be used when the child process calls exec() or exit() immediately after
creation.
How shall I understand the last sentence?
When a child process created by vfork()
calls exec()
, doesn't exec()
modify the address space of the parent process, by loading the new program?
When a child process created by vfork()
calls exit()
, does exit()
not modify the address space of the parent process when terminating the child?
I have preference to Linux.
Thanks.
Best Answer
No,
exec()
provides a new address space for the new program; it doesn’t modify the parent address space. See for example the discussion of theexec
functions in POSIX, and the Linuxexecve()
manpage.Plain
exit()
might – it runs exit hooks installed by the running program (including its libraries).vfork()
is more restrictive; thus, on Linux, it mandates the use of_exit()
which doesn’t call the C library’s clean-up functions.vfork()
turned out to be quite difficult to get right; it’s been removed in current versions of the POSIX standard, andposix_spawn()
should be used instead.However, unless you really know what you’re doing, you should not use either
vfork()
orposix_spawn()
; stick to good oldfork()
andexec()
.The Linux manpage linked above provides more context: