Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
use strict; use warnings; use Carp; use File::Copy; use Fcntl qw/LOCK_SH LOCK_EX LOCK_NB LOCK_UN/; sub x_copy # NOT USED { my ($src, $dst) = @_; open (my $f, "<", $src) or confess; binmode $f; read $f, my $buf, -s $f or confess ; close $f; open (my $o, ">", $dst) or confess; binmode $o; print $o $buf; close $o; } sub getlock { open my $f, ">", "lock.tmp" or confess; flock $f, LOCK_EX or confess; shift->(); flock $f, LOCK_UN or confess; close $f; } for (1..5) { unless (fork()) { for (1..5) { my $filename = "somefile$_.tmp"; my $f=undef; getlock sub { unless (-e $filename) { open ($f, ">", $filename) or confess; binmode $f; } }; if ($f) { print ($f "x") for (1..40_000_000); close $f; } my $newfilename = "c_${$}_$filename"; unless (-e $newfilename) { copy($filename, $newfilename) or confess; die if -s $filename != -s $newfilename; } } exit; } } while (wait != -1) { };
outputs the following (tested on Linux, perl 5.10 and 5.18):
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24171_somefile1.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24171_somefile2.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24171_somefile3.tmp
-rw-r--r-- 1 vse vse 32288768 2013-09-28 19:10 c_24171_somefile4.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24171_somefile5.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24172_somefile1.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24172_somefile2.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24172_somefile3.tmp
-rw-r--r-- 1 vse vse 28540928 2013-09-28 19:10 c_24172_somefile4.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24172_somefile5.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24173_somefile1.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24173_somefile2.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24173_somefile3.tmp
-rw-r--r-- 1 vse vse 21893120 2013-09-28 19:10 c_24173_somefile4.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24173_somefile5.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24174_somefile1.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24174_somefile2.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24174_somefile3.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24174_somefile4.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24174_somefile5.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24175_somefile1.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24175_somefile2.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24175_somefile3.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 c_24175_somefile4.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 c_24175_somefile5.tmp
-rw-r--r-- 1 vse vse        0 2013-09-28 19:10 lock.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 somefile1.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 somefile2.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 somefile3.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 somefile4.tmp
-rw-r--r-- 1 vse vse 40000000 2013-09-28 19:10 somefile5.tmp
So:
1. file size of some c_* files is less than 40000000, however this should not happen, because of line:
die if -s $filename != -s $newfilename;

2. If I replace copy() with x_copy() call, the following line reports error (or eof):
read $f, my $buf, -s $f or confess ;


3. there is locking via getlock() where it should be.
4. all files are closed, so buffering is not an issue?

5. this code works, with 1 child process, but fails on concurrent processes.

Question: I assume, after file is closed, it's written to filesystem Am I right? If yes, then I have to suspect some bug in perl, please help me find where I am wrong.
Initially I had this issue in a bigger program, but now I simplified it to this proof-of-concept code which reproduce issue.

UPD: Solved: Re^2: Strange IO + concurrency issue

In reply to [SOLVED] Strange IO + concurrency issue by vsespb

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2014-08-01 03:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (256 votes), past polls