bash - subshells and parallel processing -


i looking @ http://tldp.org/ldp/abs/html/subshells.html, mention subshells can used parallelizing tasks. give example:

(cat list1 list2 list3 | sort | uniq > list123) & (cat list4 list5 list6 | sort | uniq > list456) & # merges , sorts both sets of lists simultaneously. # running in background ensures parallel execution. # # same effect #   cat list1 list2 list3 | sort | uniq > list123 & #   cat list4 list5 list6 | sort | uniq > list456 & 
wait   # don't execute next command until subshells finish.  diff list123 list456 

are first 2 commands not going finish in same time second 2 commands? had thought last 2 commands execute in parallel, , sleep loops, unable create situation differed. how last 2 commands differ first two? if don't differ, why subshells mentioned method of parallelization when same done putting processes in background?

in fact, both:

c1 | c2 &  # c1 , c2 arbitrary commands 

and:

(c1 | c2) & 

use subshells, does:

c1 | c2 

without &. reason that, in general, pipeline requires creating subshell.

(there specific cases shells, including bash, can avoid creating subshell: instance, prog | while read ... not create separate subshell while loop in bash, provided set lastpipe (shopt -s lastpipe) and job control not active. create subshell in other shells. can seen observing values of variables set or within while loop. when c2 command not shell built-in, shell has make sub-shell internally in order hook pipe.)


Comments