Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-18 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found