Question

Say, for example, we have the following executed in the shell:

ls | grep "abc" | wc

I understand how a child process would fork from the shell and how its a child, like this,

Shell (pid=12)
       \
        \
         ls (pid=13)

but I'm not sure how the pipe and the trailing commands fit in. Are they also children of the shell, and not ls?

Was it helpful?

Solution

Each component of the pipeline will be a child process of the shell. If you use pstree -p from another terminal, you'll probably see something like this:

...
sshd(11)---bash(12)-+-ls(13)
                    |-grep(14)
                    \-wc(15)
...

(assuming you can run this whilst your pipeline is still running!)

However, note that all the components will form a single process group.

The piping itself is a feature of Linux (or whatever OS you're using), it's not a separate user process. The shell creates some anonymous pipes, and hooks them up to the relevant file descriptors for each pair of processes in the pipeline.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top