Time waste of execv() and fork()

前端 未结 6 924
青春惊慌失措
青春惊慌失措 2020-12-16 09:30

I am currently learning about fork() and execv() and I had a question regarding the efficiency of the combination.

I was shown the followin

6条回答
  •  被撕碎了的回忆
    2020-12-16 09:56

    Another answer states:

    However, in the bad old days a fork(2) would require making a complete copy of the caller’s data space, often needlessly, since usually immediately afterwards an exec(3) is done.

    Obviously, one person's bad old days are a lot younger than others remember.

    The original UNIX systems did not have the memory for running multiple processes and they did not have an MMU for keeping several processes in physical memory ready-to-run at the same logical address space: they swapped out processes to disk that it wasn't currently running.

    The fork system call was almost entirely the same as swapping out the current process to disk, except for the return value and for not replacing the remaining in-memory copy by swapping in another process. Since you had to swap out the parent process anyway in order to run the child, fork+exec was not incurring any overhead.

    It's true that there was a period of time when fork+exec was awkward: when there were MMUs that provided a mapping between logical and physical address space but page faults did not retain enough information that copy-on-write and a number of other virtual-memory/demand-paging schemes were feasible.

    This situation was painful enough, not just for UNIX, that page fault handling of the hardware was adapted to become "replayable" pretty fast.

提交回复
热议问题