1

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?

Thanks.

1
  • In your first example the three commands can run in parallel too. The pipe has a buffer so for example command1 will fill (part of) the first pipe's buffer, command2 will be processing it while at the same time command1 will continue to run. On multi-core computers you can achieve noticeable speed gain using pipes for CPU-intensive processing. May 5, 2022 at 16:53

2 Answers 2

3

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.

0

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!)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .