i trying pipe implementation shell. have implemented in following way , works. eg: if want do, ls | grep x | grep y | grep z, creating 4 child process parent process , working them. there other ways of doing ?
for ex: can create using following flow ? instead of having 4 children single parent process, can 'grep z' child of 'grep y' , 'grep y' child of 'grep x' , on ?
i curious on how piping functionality implemented in bash shell. tried downloading source code , understanding lost.
this depends on shell , programs invoke.
theoretically, use variadic tree n non-root nodes combinatorial explosion of possibilities, of are:
shell shell shell / / \ / \ grep x ls grep y ls grep y / \ \ \ / \ ls grep y grep x grep z grep x grep z \ grep z a posix standard shell, though, required wait last stage finish before continuing next command. since process can wait on child process, means last stage must child of main shell. posix allowance additionally wait stages, bash , other shells (try sleep 5 | true).
this implies bash starts processes children of itself, , can verify e.g. strace -f -e clone bash -c 'sleep 5 | sleep 5 | sleep 5' or sleep 5 | sleep 5 | sleep 5 & pstree -p $$, if don't want study execute_pipeline in execute_cmd.c in bash source code.
this has additional benefit of allowing bash's pipestatus array , pipefail option act on status of stages in pipeline, can't if stages aren't direct child processes.
another consideration programs handle children didn't expect. @ best, zombie process, @ worst interfere correctness of process. means ls should never direct child of grep or vice versa. however, can make grandchild through double fork, init can take responsibility instead.
so yes, can use configuration want, in practice (like in bash, dash, ash , zsh), it'll tend flat.
Comments
Post a Comment