-1

i understand well the parent-child relationship in unix processes creation. But i don't understand the rationale behind it :( why do we need to fork from the current process to create a new one, then overwrite its image with a new code if any? cheers

  • Because it's what you want in many cases (threads), and because it's extremely fast. You can spawn processes like there's no tomorrow and you only really incur costs for the stuff you actually need to do with them. – us2012 Feb 16 '13 at 13:07
  • The rationale behind this is that *COW* (Copy On Write) is cheap: the kernel **only** has to clone the pagetables, (and set the flags to indicate COW). If here is an exec() after the fork(), this is a bit wastful, but if there is no exec() the pages for the child will be faulted-in when needed. (historical sidenote: when the fork() mechanism was designed, MMU's with COW-features had not yet been invented) – wildplasser Feb 16 '13 at 13:22
  • `fork` existed before COW.... The original PDP-8 Unix did not have COW; at that time `fork` copied the entire 64Kbyte address space... – Basile Starynkevitch Feb 16 '13 at 13:24
  • I know, I added a sidenote. IIRC the WE-32000 was the first architecture with MMU. – wildplasser Feb 16 '13 at 13:25

1 Answers1

1

The rationale is that unix system calls (at least originally) are "elementary" operations done by the kernel.

In practice, applications often do some specific things between fork(2) and execve(2), in particular calls to close(2) and dup2(2), also sigaction(2) to ignore some signals (with perhaps some pipe(2) syscalls done before the fork).

If you wanted to have a single syscall handling all this at a time, it would have been very complex, and less flexible.

I suggest to read some book like Advanced Linux Programming (it is free and online) or Advanced Unix Programming in addition of intro(2).

I find on the contrary the intent to separate creating a process and executing a program quite natural. I don't really understand why you want both operations to be combined.

See also this mine answer about syscalls.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547