How does copy-on-write work in fork-exec? [closed]

2019-03-02 15:44发布

A process forks 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?

1条回答
我想做一个坏孩纸
2楼-- · 2019-03-02 16:12

With copy-on-write, after fork the parent process and child process share the memory.

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 by exec(), the address space created for the child while fork()ing typically isn't used but is replaced by a new one, namely the one created for the process exec ()ed.

查看更多
登录 后发表回答