Home > Back-end >  How does copy-on-write give one optimization for fork-exec?
How does copy-on-write give one optimization for fork-exec?

Time:12-30

I really searched through topics on stackoverflow but still cannot get it.

vfork() is an obsolete optimization. Before good memory management, fork() made a full copy of the parent's memory, so it was pretty expensive. since in many cases a fork() was followed by exec(), which discards the current memory map and creates a new one, it was a needless expense. Nowadays, fork() doesn't copy the memory; it's simply set as "copy on write", so fork() exec() is just as efficient as vfork() exec().

When calling fork() I get no copy of memory for a child. Okay. But then -- the child calls exec() and overwrites its code with the code of another binary. So the write happens. So it looks like the copy of parent process's memory must happen. So where is the optimization for this fork() exec()? I mean when I call exec() the copy does happen, is not it? So COW does not help in any way for fork() exec() and does not optimize out anything, am not I right?

CodePudding user response:

If the old pages got overwritten with the new code, then it would indeed be pointless, but that's not what happens. When you call exec, all of the old process's COW memory pages get unmapped, and entirely new ones get mapped, so other than the tiny amount of memory written to in between the vfork and exec (basically just the page with the top of the stack), there won't be any copying.

  • Related