bug-bash
[Top][All Lists]
Advanced

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

Re: Strange behaviour on 'read' from a pipe


From: Lluís Batlle i Rossell
Subject: Re: Strange behaviour on 'read' from a pipe
Date: Mon, 2 Apr 2012 22:17:26 +0200
User-agent: Mutt/1.5.20 (2009-06-14)

On Mon, Apr 02, 2012 at 03:29:33PM -0400, Greg Wooledge wrote:
> On Mon, Apr 02, 2012 at 08:46:12PM +0200, Lluís Batlle i Rossell wrote:
> > #!/bin/sh
> 
> You're running this in sh?  But reporting it as a bug in bash?

In that case, sh points to bash. Sorry for the confusion I could have caused.

> > while true; do
> >     while read LINE < $PIPE; do
> >         echo $LINE
> >         spawn &
> >     done
> > done
> 
> I ran this, with #!/bin/bash and with sleep 1 instead of sleep 0.1,
> on an HP-UX 10.20 system and a Debian 6.0 (Linux) system.  For me,
> it printed "DONE" once per second until I pressed ctrl-C, on both
> systems.
> 
> Changing the sleep 1 back to sleep 0.1 and re-running on the Linux
> system produced the same result, just 10 times as fast.
> 
> Changing the #!/bin/bash to #!/bin/sh and re-re-running on Linux
> still produced the same result.

I found the race condition, after implementing it in C. It's there in the script
too.

It can happen that the child spawns, but does not reach the "open pipe writing"
call of echo DONE, while the loop goes another round, runs read LINE again, and
reads *EOF*, exiting the inner while loop.

Sorry for the headache.

Here is the proper version without race condition, that uses the close/read()=0
as synchronisation:

---------------
#!/bin/bash

PIPE=/tmp/pipe

rm -f $PIPE
mkfifo $PIPE

spawn() {
    echo DONE > $PIPE
}

spawn &

# read A  -- to read EOF from the child, to ensure it closed the pipe.
while (read LINE ; echo $LINE; read A; echo child closed) < $PIPE; do
    spawn &
done
----------------

Best regards,
Lluís.



reply via email to

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