3 ms·
Here is a use case... We have a shell that must execute $ cmd_a | cmd_b | cmd_c The simplest way for the shell to accomplish this request is to fork itself
by kniwor 17y ago
Here is a use case... We have a shell that must execute
$ cmd_a | cmd_b | cmd_c
The simplest way for the shell to accomplish this request is to fork itself multiple times. Doing so without fork would be difficult. I figure since multitasking and pipes are old as eternity in the linux world, fork must have been an early necessity and this use case might have something to do with their prominence but then again I am just guessing.
- billpg 17y agoIf you'll excuse the pseudo code... Pipe pipe1 = new Pipe() Pipe pipe2 = new Pipe() NewProcess("cmd_a", null, pipe1) NewProcess("cmd_b", pipe1, pipe2) NewProcess("cmd_c", pipe2, null)
- kniwor 17y agoYou are not passing the current shell state though. So you could start 3 new shell processes with enough data to set the state right and then start the individual processes but that is both inelegant and inefficient. Instead if we fork thrice and each fork execs a command and does the pipeline plumbing, all three processes start simultaneously and inherit the exact same shell state all for free. And copy on write means we did not waste any memory replicating the shell state for 3 processes.