Dredging up an old thread here. :-)<div><br></div><div>I&#39;m finally working on the &quot;flock()&quot; work on my test systems.  I found some sample Perl+flock() code from the Internet and they too were showing different problems with flock() on different platforms.</div>

<div>    URL: <a href="http://www.justskins.com/forums/flock-on-different-unix-86761.html">http://www.justskins.com/forums/flock-on-different-unix-86761.html</a></div><div><br></div><div><a href="http://www.justskins.com/forums/flock-on-different-unix-86761.html"></a>In this initial test code, the lock file is created before the fork() code, and when I run their test code it too creates bad output on my workstation (Ubuntu 10.04.1, Perl 5.10.1).</div>

<div><br></div><div>If we move the lock file creation after the fork (i.e. Parent and Child each create their own file handle to it), the resulting output is correct.  (I&#39;ll include the code at the end of this e-mail -- is there a better on-line notepad someone can recommend if we want to collaborate on it?)</div>

<div><br></div><div>The  Perl documentation for flock (<a href="http://perldoc.perl.org/functions/flock.html">http://perldoc.perl.org/functions/flock.html</a>) alludes to some fork&amp;flock anomalies, but doesn&#39;t explain.  From some additional research, this appears to be an expected result.  If the file handle is created before the fork, both parent and child share the file descriptors and flock doesn&#39;t see them as being different:</div>

<div>    <a href="http://www.perlmonks.org/?node_id=463377">http://www.perlmonks.org/?node_id=463377</a></div><div><br></div><div>Thankfully my code will have the parent spawning multiple child processes running a different perl script with their own file handles.  I just have to update them to perform the flock() on the common file(s) each could be updating.</div>

<div><br></div><div>Hopefully a future Googler will stumble across this and not spin their wheels for a long time.</div><div><br></div><div>Dan</div><div><br></div><div>=== begin sample code ===</div><div><div>#!/usr/bin/perl</div>

<div>#</div><div># Test file locking under Perl.</div><div># Code based on example from:</div><div>#    <a href="http://www.justskins.com/forums/flock-on-different-unix-86761.html">http://www.justskins.com/forums/flock-on-different-unix-86761.html</a></div>

<div><br></div><div>use Fcntl &#39;:flock&#39;;</div><div>use strict;</div><div><br></div><div># Set to run different test cases.</div><div># 1 : Test with creating the lock file BEFORE forking.</div><div># 2 : Test with creating the lock file AFTER forking.</div>

<div>#</div><div># View resulting output with something like this:</div><div>#  uniq lock_out | head -30</div><div>#</div><div># Good output will look like this:</div><div># 1111111111111111111111111111111111111111</div>
<div>
# 2222222222222222222222222222222222222222</div><div># 3333333333333333333333333333333333333333</div><div># 4444444444444444444444444444444444444444</div><div># AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div><div># BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</div>

<div># CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC</div><div># DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD</div><div>#</div><div># Bad output will have the lines interspersed:</div><div># 1111111111111111111111111111111111111111</div>

<div># 2222222222222222222222222222222222222222</div><div># 222222222222222222222222222222222AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div><div># AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div><div># AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2222222</div>

<div># 2222222222222222222222222222222222222222</div><div># 3333333333333333333333333333333333333333</div><div># 4444444444444444444444444444444444444444</div><div># A</div><div># AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div>

<div># BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB</div><div># CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC</div><div># DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD</div><div><br></div><div><br></div><div>my $test = 1;</div><div><br>

</div><div>my $res;</div><div>my $pid;</div><div><br></div><div>my $sleeptime1 = 2;</div><div>my $sleeptime2 = 1;</div><div><br></div><div>open(FH, &quot;&gt;lock_out&quot;) if ($test == 1);</div><div><br></div><div>if($pid=fork()) {</div>

<div>        # Parent...</div><div>        sleep(1);</div><div>        open(FH, &quot;&gt;lock_out&quot;) if ($test == 2);</div><div>        $res=mylock();</div><div>        print &quot;$$: lock res=$res\n&quot;;</div><div>

        sleep($sleeptime1); writefile(&#39;A&#39; x 40);</div><div>        sleep($sleeptime1); writefile(&#39;B&#39; x 40);</div><div>        sleep($sleeptime1); writefile(&#39;C&#39; x 40);</div><div>        sleep($sleeptime1); writefile(&#39;D&#39; x 40);</div>

<div>        $res=myunlock();</div><div>        print &quot;PARENT $$: unlock res=$res\n&quot;;</div><div>}</div><div>elsif($pid==0) {</div><div>        # Child...</div><div>        open(FH, &quot;&gt;lock_out&quot;) if ($test == 2);</div>

<div>        $res=mylock();</div><div>        print &quot;$$: lock res=$res\n&quot;;</div><div>        sleep($sleeptime2); writefile(&#39;1&#39; x 40);</div><div>        sleep($sleeptime2); writefile(&#39;2&#39; x 40);</div>

<div>        sleep($sleeptime2); writefile(&#39;3&#39; x 40);</div><div>        sleep($sleeptime2); writefile(&#39;4&#39; x 40);</div><div>        $res=myunlock();</div><div>        print &quot;CHILD $$: unlock res=$res\n&quot;;</div>

<div>        exit(0);</div><div>}</div></div><div><div><br></div><div>sub writefile {</div><div>        my($var)=@_;</div><div>        my($max)=80000;</div><div>        my($i);</div><div>        for($i=0;$i&lt;$max;$i++) {</div>

<div>                print FH &quot;$var\n&quot;;</div><div>        }</div><div>}</div><div><br></div><div>sub mylock {</div><div>        my $result = flock(FH, LOCK_EX);</div><div>        if (! $result) {</div><div>                printf (&quot;FLOCK returned error $result: $! \n&quot;);</div>

<div>        }</div><div>        seek(FH, 0, 2);</div><div>        return $result;</div><div>}</div><div><br></div><div>sub myunlock {</div><div>        my $result = flock(FH, LOCK_UN);</div><div>        close(FH);</div>
<div>
        return $result</div><div>}</div></div><div><br></div><div>=== end sample code ===</div><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>

<br><div class="gmail_quote">On Tue, Apr 21, 2009 at 21:56, Dan Linder <span dir="ltr">&lt;<a href="mailto:dan@linder.org">dan@linder.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">On Tue, Apr 21, 2009 at 15:13, James Harr <span dir="ltr">&lt;<a href="mailto:jharr@ist.unomaha.edu" target="_blank">jharr@ist.unomaha.edu</a>&gt;</span> wrote:<br>&gt; It
wouldn&#39;t be the first time perl didn&#39;t (or couldn&#39;t) hide a scary OS
behavior from the programmer. &gt; If you can&#39;t find anything on the
subject, just write a test program to hammer flock() for a while and
&gt; see if it grants two mutual locks on the same file. <br><br></div>Yup, that&#39;s my plan.  When I get the green light after some other projects come up I might send my test script out to the list and let others test it on their systems.  (Thankfully for me, I just have to test a handfull of UNIX variants...multiplied times the common filesystems...ouch!)<div class="im">

<br>
<br>&gt; Win32:: might also
have something that&#39;d work &gt; for you and behaves better on that platform.<br>
<br></div>Yeah, my worst fear is that someone will say &quot;Sure, this app will run on Windows ME with FAT-32 drives!&quot; and expect me to support it... :-O  :-D<div><div></div><div class="h5"><br><br>Dan<br><br clear="all">

&quot;Quis custodiet ipsos custodes?&quot; (Who can watch the watchmen?) -- from the Satires of Juvenal<br>
&quot;I do not fear computers, I fear the lack of them.&quot; -- Isaac Asimov (Author)<br>** *** ***** ******* *********** *************<br>
<br><br><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>***************** ************* *********** ******* ***** *** **<br>&quot;Quis custodiet ipsos custodes?&quot;<br>    (Who can watch the watchmen?)<br>    -- from the Satires of Juvenal<br>

&quot;I do not fear computers, I fear the lack of them.&quot;<br>    -- Isaac Asimov (Author)<br>** *** ***** ******* *********** ************* *****************<br>
</div>