A process fork
s a child process, and calls exec()
in the child process.
With copy-on-write, after fork
the parent process and child process share the memory.
When the child process calls exec()
to load another process, will Linux copy the parent memory to the new memory and the child loads another process also to this new memory? If so, does that mean the process forked with copy-on-write got no data when doing fork-exec?
Yes for reading and no for writing. A new address space is created for the forked child process, only it is not populated until a write to it occurs by the child.
If
fork()
is immediately followed byexec()
, the address space created for the child whilefork()
ing typically isn't used but is replaced by a new one, namely the one created for the processexec ()
ed.