unix - How does shell execute piped commands? -
i want understand how shell executes piped commands ? e.g. cat | more. aware executing normal command shell fork, execute , child returns. how shell internally handle execution of piped commands ?
considering example cat | grep
, shell first forks start cat
, , forks once more start grep
.
before calling 1 of exec*
family of functions in 2 newly created processes start 2 programs, tricky part setting pipe , redirecting descriptors. pipe(2)
system call used in shell process before forking return pair of descriptors both children inherit - reading end , writing end.
the reading end closed in first process (cat
), , stdout redirected writing end using dup2(2)
system call. similarly, writing end in second process (grep
) closed , stdin redirected reading end again using dup2(2)
.
this way both programs unaware of pipe because work standard input/output.
Comments
Post a Comment