The UNIX system call for process creation, fork(), creates a child process by copying the parent process. My understanding is that this is almost always followed by a call to exec() to replace the child process' memory space (including text segment). Copying the parent's memory space in fork() always seemed wasteful to me (although I realize the waste can be minimized by making the memory segments copy-on-write so only pointers are copied). Anyway, does anyone know why this duplication approach is required for process creation?
|
It's to simplify the interface. The alternative to With the If you want to start a process with a different current directory: If you want to redirect stdin/stdout: If you want to switch users: All these things can be combined as needed. If somebody comes up with a new kind of process attribute, you don't have to change As larsks mentioned, most modern Unixes use copy-on-write, so |
|||||||||||||||||
|
|
In adition to the cjm's answer, the Single Unix Specification defines a function named Thus pretty much the only use with defined behavior is:
So what does There is also the optional |
|||
|
|
fork(2)man page under Linux says:Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.I imagine (but do not know for certain) that this is the case for other modern Unix flavors. – larsks Feb 7 '12 at 19:17