Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 3 Jan 2002 18:44:13 +0100 (CET)
From:      Oliver Fromme <olli@secnetix.de>
To:        freebsd-questions@FreeBSD.ORG, chkno@dork.com
Subject:   Re: sh redirect optimization + fifos
Message-ID:  <200201031744.g03HiDi25777@lurza.secnetix.de>
In-Reply-To: <200201031428.g03ESKO03852@chk.phattydomain.com>

next in thread | previous in thread | raw e-mail | index | archive | help
chk no <chuck@chk.phattydomain.com> wrote:
 > [...]
 > However, if you go & try this, it doesn't operate as outlined above.
 > For each access of the fifo, it returns anywhere from 1 to ~5000
 > words.  sh caches the lines redirected with ">", and then writes
 > them in chunks.

No, sh does not cache anything.

 > This is a great optimization for flat files, but
 > it goes aginst the intention of the code when used with fifos.

No, your code goes against the way FIFOs work.  :-)
I'd suggest you take a good UNIX book and read about FIFOs
a.k.a. named pipes.

Let me explain what happens in more detail.

1. When the loop executes the first ``echo > FIFO'', it
   blocks and wait until someone opens the FIFO for reading.

2. When you execute ``cat FIFO'' on another terminal, the
   cat process opens the FIFO for reading.  As soon as this
   happens, the shell in the first terminal starts writing
   the first line to the FIFO.

3. The cat reads that line and displays it.  In the mean-
   time, the loop in the first terminal has turned around
   and executes the second ``echo > FIFO''.  Since the cat
   still has the FIFO open for reading, it doesn't block,
   but writes the second line immediately.

4. The cat process tries to read another line, checking
   for EOF.  It successfully got the second line (no EOF),
   so it continues.

The above steps 3. and 4. repeat until the cat process has
outrun the shell loop (which is clearly slower).  Then it
continues like this:

5. The cat process looks for the next line, but the shell
   loop hasn't had a chance to echo it yet.  Therefore,
   cat receives an EOF and exits.

6. The shell loop reaches the next iteration.  When it
   tries to echo the next line, there is no other process
   who has opened the FIFO for reading, so it blocks.

Then goto step 2. above.  :-)

When you inserted sleep commands into the shell loop, you
slowed that loop down considerably, so it was _much_ slower
than the cat process.  Now it is clear why that seemed to
"fixed" your problem: the loop never reached the next echo
before the cat process checked for the next line.
Of course, when the load increases significantly, the cat
process will be slowed down, too ...  So using sleep is
certainly not a solution.

I think FIFOs are not the appropriate way to do what you
are trying to do.

Regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

"All that we see or seem is just a dream within a dream" (E. A. Poe)

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200201031744.g03HiDi25777>