Checking if the other end of a fifo is open

Frank Sorenson frank at tuxrocks.com
Thu Apr 9 18:32:57 MDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave Smith wrote:
> Frank Sorenson wrote:
>> How about opening the fifo read/write, which doesn't block:
>> # exec 4<> dave_fifo
>> # ls -alF /proc/$$/fd
>> ...
>> lrwx------ 1 sorenson sorenson 64 2009-04-09 18:14 4 -> /tmp/dave_fifo|
>>
>> Here's an example script:
>> #!/bin/bash
>>
>> exec 4<> /tmp/dave_fifo
>> (sleep 2 ; kill -SIGUSR1 $$ ; echo "I'm alive" >&4) &
>>
>> # Wait for the flag to get raised, or give up after a while:
>> read -t 5 -u 4 line
>> if [ $? -eq 0 ] ; then
>>         echo "The child said: $line"
>> else
>>         echo "Timed out waiting for child"
>> fi
>>   
> 
> Interesting idea. Doesn't this block my daemon process (a separate piece
> of C code) from opening the fifo for writing as well? My bash script
> reads from the fifo, but the C program writes to it (and is responsible
> for opening it for writing).
> 
> --Dave

Not unless you're doing file locking to get exclusive access.

# cat dave_parent
#!/bin/bash

/tmp/dave_child &
child_pid=$!

exec 4<> /tmp/dave_fifo

echo "Parent $$ waiting for child $child_pid"
read -t 5 -u 4 line
if [ $? -eq 0 ] ; then
        echo "The child said: $line"
else
        echo "Timed out waiting for child"
fi

# cat dave_child
#!/bin/bash

exec 4<> /tmp/dave_fifo
sleep 2
echo "Dear $PPID, I'm alive.  Love $$" >&4


./dave_parent
Parent 25732 waiting for child 25733
The child said: Dear 25732, I'm alive.  Love 25733

Frank
- --
Frank Sorenson - KD7TZK
Linux Systems Engineer, DSS Engineering, UBS AG
frank at tuxrocks.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAknek7kACgkQaI0dwg4A47yVpQCgnQLs6ME5YXbDZpqZAFITESJm
NkoAnA9+d2Nq2dtD6UEq5nugI1tEcGJc
=n6NG
-----END PGP SIGNATURE-----



More information about the PLUG mailing list