In my script, deletion of the named pipe occurred in two places:

unlink $named_pipe if -e $named_pipe;

This was partly to start with a clean slate and also a bit of defensive programming. Note that it only checks for filename existence (-e); not specifically for a named pipe (which would have been -p).

I'd recommend you implement a file naming convention for your named pipes that both identifes them as named pipes and also identifies their source (in my script that was: pm for this site, your OP's node ID, parent PID and .fifo extension). Depending on the level of robustness required and in-house standards/policies/etc., you may want to do more than just leaving error handling to autodie (e.g. custom messages, timestamps, logging, and so on).

unlink $named_pipe;

This is just for housekeeping purposes. Without this, you could accumulate as many old named pipes as there are potential PIDs of the parent (that's tens of thousands on my system).

The named pipe doesn't actually hold the data (at least, not in any implementation that I'm aware of). The data is written to, and read from, a buffer. You can close the $writer filehandle, open another one, and write more data to the buffer. Similarly, you can close the $reader filehandle, open another one, and read more data from the buffer. If you close both the $reader and $writer, the data in the buffer is lost (this has nothing to do with whether the named pipe was deleted); if the named pipe wasn't deleted, and (after closing both the $reader and $writer) you opened a new filehandle, it would start with an empty buffer.

[You can have multiple readers and writers accessing the same named buffer at the same time. Multiple writers may be useful (cf. parent and child processes both writing to STDOUT at the same time); multiple readers is probably a very poor choice (again, cf. parent and child processes both trying to read from STDIN at the same time). If you do have multiple filehandles, all need to be closed for the "data in the buffer is lost" scenario (in the last paragraph) to occur.]

Yes, the size of named pipes is reported as zero, e.g.

prw------- 1 ken staff 0 10 May 09:45 pm_1085515_64171.fifo

Finally, do note that the script I provided was only intended to show a "technique". There may well be lots of things you'll want to tweak or substantially modifiy.

-- Ken


In reply to Re^3: Filehandle reference lost between parent and child by kcott
in thread Filehandle reference lost between parent and child by italdesign

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.