Hi,

I was recently asked in an interview the number of processes that are created when I enter in a command that contains a pipe.

For eg. If I enter the command: $who | tee /dev/tty | wc -l

Could anyone tell me how many(which) processes are created and in what order are they created?

Thanks.

This is bash running on OS/X. The pv command is "pipeviewer", which invoked -L1 limits throughput to 1 byte per second. Using two windows, first I chanted ps -l in window 2. The result is as seen below, and is the base case of two bash shells running.

508% ps -l
  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S     ADDR TTY           TIME CMD
  501 36186 36185     4006   0  31  0  2435468   1056 -      S+    add8a80 ttys000    0:00.20 -bash
  501 46127 46126     4006   0  31  0  2435468   1336 -      S     c9f7540 ttys001    0:00.04 -bash

Then I ran this in window 1 who | pv -L1 -q | tee /dev/tty |wc -l while running the ps command in window 2.

509% ps -l
  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S     ADDR TTY           TIME CMD
  501 36186 36185     4006   0  31  0  2435468   1056 -      S     add8a80 ttys000    0:00.22 -bash
  501 46184 36186     4006   0  31  0  2427104    348 -      S+    8bf7d20 ttys000    0:00.01 pv -q -L1
  501 46185 36186     4006   0  31  0  2434756    436 -      S+    add8000 ttys000    0:00.00 tee /dev/tty
  501 46186 36186     4006   0  31  0  2434768    440 -      S+    ca02540 ttys000    0:00.00 wc -l
  501 46127 46126     4006   0  31  0  2435468   1336 -      S     c9f7540 ttys001    0:00.04 -bash

As you can see, there are 5 processes running, of which three are the pv, tee and wc subshells. Since the PIDs are in ascending order, that is the order of creation.

Hey thanks. So this is how I understand it. Initially a process will be forked and "exec"ed with the image of who. So after the who command there will be 2 processes one of which is the shell program (-bash) and the other process which is running who. For every subsequent pipe command there will be an new process forked which will be "exec"ed with the corresponding command's image. So I guess, in the command I provided there ought to be 4 processes in all. Am I right?

Also why are there 2 processes for bash?

I read it as three:

  1. bash is the parent process, and who is an executable that runs in that process without spawning another process.
  2. tee is a sub process of bash
  3. wc -l is a sub process of bash
  4. ( pv -q -L1 is a sub process of bash too, so in my case: 4)

Here's the same test, with pstree looking at the processes:

513% pstree -p 36186
-+= 00001 root /sbin/launchd
 \-+= 00275 griswolf /sbin/launchd
   \-+= 36183 griswolf /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_15154803
     \-+= 36185 root login -pf griswolf
       \-+= 36186 griswolf -bash
         |--- 47193 griswolf pv -q -L1
         |--- 47194 griswolf tee /dev/tty
         \--- 47195 griswolf wc -l

Note there is no who process, despite that it is the first thing in the left to right chain of commands. The invocation window looks like this (though it takes forever to print at one char per second):

569% who | pv -q -L1 | tee /dev/tty |  wc -l 
griswolf console  Jun 22 09:25 
griswolf ttys000  Jul 21 01:11 
griswolf ttys001  Jul 26 19:48 
       3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.