help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] silly question - which is better to pipe 2 sources into


From: Russell Lewis
Subject: Re: [Help-bash] silly question - which is better to pipe 2 sources into one command
Date: Mon, 30 Apr 2018 07:47:38 -0700

You are basically correct.  pgm1 and pgm2 run serially in the first
example, and in parallel in the second.  But, as you say, there are limited
size buffers (in the kernel) which will cause pgm2 to pause if it produces
a lot of output.

I use the second style when pgm1 and/or pgm2 tend to run for a very long
time without producing a *lot* of output; then you can (to a rough
approximation) have the *entire* output come available (piped out of 'cat')
when both of them finish.  If they are both chewing up CPU, this might not
be attractive - but if they are both blocked for a long time (say, waiting
for network or disk I/O) it might make sense.  Of course, if they are
blocked for disk I/O, there's also a chance that you are just making them
*slower* by having them contend for the disk.  Lots of complications to
consider.  :)

Russ

On Mon, Apr 30, 2018 at 7:10 AM, John McKown <address@hidden>
wrote:

> This really is probably a stupid question. Or maybe a "self evident" one.
> But I'll ask anyway. I need to feed the output of two programs into another
> program. I have thought of two ways to do it. I am not really sure which is
> "best". Or maybe it is a toss up.
>
> Way one:  { pgm1; pgm2; } | pgm3
> Way two: cat <(pgm1) <(pgm2) | pgm3
>
> Way one _seems_ better to me mainly because there are fewer processes
> running. Basically pgm1 runs and feeds its stdout into pgm3; then pgm2 runs
> and pgm3 continues to get input but from pgm2's stdout. I'm fairly sure
> that this executes pgm1 & pgm3 concurrently followed with pgm2 taking
> pgm1's "place" while pgm3 continues running - so 2 user processes.
>
> But I'm wondering a bit about way two. I don't know really how that is
> working. I think that "pgm1" and "pgm2" are both fork()'d & exec()'d to run
> concurrently. But I imagine that there is some sort of "buffer" involved
> with their stdout so that pgm2 will block after a while because the "cat"
> will not start reading it until it has read all of the output from pgm1.
>
> Anyway, all (kindly worded) thoughts are appreciated.
>
> --
> We all have skeletons in our closet.
> Mine are so old, they have osteoporosis.
>
> Maranatha! <><
> John McKown
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]