I think this is one of those cases where it could make a difference if we knew: "Why do you ask?"

In particular, if only the child is going to be using the locked file handle, why would you need or want to open/lock the file before forking? You could just do this in the child process after the fork, and not worry about what happens across a fork. The same reasoning would apply if only the parent were doing stuff with the file in question. OTOH, if the plan is for the parent to do something with the locked file either before or after the child works on it, why even do the fork in the first place (unless there is enough other stuff to do, not involving the file, that makes the fork worthwhile)?

Now, if your idea is that both child and parent are going to be doing things "in parallel" with this one locked file, you're likely to run into problems whose symptoms might be really strange, unpredictable and hard to debug. One of the main reasons for file locking in the first place is to keep concurrent processes from trampling each other when working with the same external resource.


In reply to Re: Fork + Flock = Who Gets the Lock? by graff
in thread Fork + Flock = Who Gets the Lock? by Anonymous Monk

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.