jeffthewookiee has asked for the wisdom of the Perl Monks concerning the following question:

I'm using named pipes to have 2 Perl processes communicate with each other. I've been able to set up a 2 named pipes for input/output, but I can't seem to find documentation on how to detect in either process when the other closes the pipe, deletes the file, or otherwise hangs up. I registered a signal handler with $SIG{PIPE} but a signal never seems to get sent. How can my code detect when the other process is gone?

Replies are listed 'Best First'.
Re: Detecting the close() of named pipes
by shmem (Chancellor) on Aug 23, 2006 at 15:19 UTC

    On which side of the pipe did you register a $SIG{PIPE} handler?

    If the reader closes the pipe, the writer to a pipe gets a SIGPIPE; if the writer closes it, the reader gets EOF.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      the writer to a pipe gets a SIGPIPE
      Or, if SIGPIPE is ignored, the writer gets an EPIPE error.
Re: Detecting the close() of named pipes
by innominate (Beadle) on Aug 23, 2006 at 15:20 UTC
    Have you tried the -e file test?

    Example:
    open(my $z, '| more'); # stupid call, just an example! print ("Exists: ", -e $z, "\n"); close($z); print ("\nExists: ", -e $z, "\n");
    Should return:
    Exists: 1 Exists:
      When I close() the named pipe, it seems to leave the files there though... they still Exist. I'm using POSIX:mknod to make it.
        If you don't remove them, there they are. As usual ;-)

        Named pipes are files, you have to do the cleanup yourself, as opposed to anonymous pipes. The latter are reaped by the OS. See pipe for those.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}