No, fork() doesn't share much of anything between processes. The new process inherits copies of everything, including open file descriptors. In particular, fork() never creates shared memory1.

Another thing that isn't shared is the buffers that efficiently reading of a file one-line-at-a-time requires. So using <$fh> isn't going to do a very good job of distributing lines between processes because each process is going to read much more than just the next line and buffer it.

Now the current file position can be shared between file descriptors. My first guess would have been that it wouldn't be shared after a simple fork(), but you seem to imply otherwise. But I think it can be shared between processes and if fork() didn't share that, then I don't know how you'd go about sharing it (perhaps by passing an open file descriptor over a socket?).

To do something like this I'd resort to a pipe with record lengths preceeding each record so that the readers could efficiently read an entire record. This requires a writer process that reads the input file and puts the records onto the pipe. (If the records are always short, then you could just use the behavior of pipes with multiple readers under Unix and have the writer just put the records onto the pipe with a single syswrite() per record and then each reader would get a single record for each sysread() -- but I'd probably avoid that type of fragile solution.)

Alternately you could have the writer process have a separate pipe to each process and pick between the pipes using select() (this would avoid the need for writing the record length in front of each record).

1Well, it probably makes shared read-only memory that gets copied by an exception handler when and if a process ever tries to write to it. But this is really just an optimization trick, not a way to share anything between processes.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Threading read access to a filedescriptor by tye
in thread Threading read access to a filedescriptor by smferris

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.