linux pipeline/tee process sequence

Posted on

Problem :

I think for the linux command, the pipeline:
$ command1 | command2 | command3
will execute with sequence command1 -> command2 -> command3.

But what about using tee:
$ command1 | tee >(command2) >(command3)

Since that command2 and command3 have the same input, my question is, will command2 and command3 execute in parallel (multithread?) or in sequence 2->3?


Solution :

They will execute in parallel if they are capable of doing so. The tee command will feed input to both commands as it gets it. This will make them “ready to run” if they were blocked on input, and then the OS wills schedule them on whatever cores it has available. This is not multithreading because that takes place within a process. This is multiprocess operation.

Try running this command (Ctrl-C it after a bit):

(od -x /dev/urandom | tee >(sed 's/^/1 /') >(sed 's/^/2 /')) > /tmp/output

/tmp/output will be interleaved and overwritten as each process gets a chance to run.

Also when you say “will execute with sequence” that’s incorrect. I think DOS used to simulate pipelines with temporary files, but not Unix.

(I’ve never seen the >() syntax before. Learn something new everyday!)

Leave a Reply

Your email address will not be published. Required fields are marked *