Dear Brothers of Logic -

I have a big sql file generated by a tool, which I want to scan into separate pieces of sql and execute in parallel. I open the file, loop, and fork upon each complete block, closing the file handle in the child.

I find the file handle is corrupted by the fork/child - I think the file position keeps flipping back, because I see the message to STDERR report early blocks repeatedly.

Here's the code:

open $fh, '<', $ARGV[0] or die "Can't open $ARGV[0]: $!"; while (<$fh>) { my $Sql ; my $ReportId ; if ( /SQL_REPORT_ID (\d+)/ ) { $ReportId = $1; while (<$fh>) { last if (/END SQL_REPORT_ID/) ; $Sql .= "$_"; } my $pid = fork ; die "Can't fork: $!" unless defined $pid; if ($pid) { $Pids{$pid} = 1; } else { close $fh or die "Can't close $fh"; #print "\n\n\n\n\n\n\n\n\n\n\n---------$ReportId------------- +\n\n$Sql\n"; print STDERR "Generated $ReportId\n"; exit(0); } } } ..etc.
It doesn't matter if I use a name FH for the file handle, or the scalar you see, nor does it if I fail to close $fh in the child.

IT WORKS OFF STDIN!

I have tried sysopen() too - no good.

The likliest thing is I've done something daft and fail to see it, and you monks'll spot it.

Otherwise I wonder if it's to do with HPUX specifically, and I've scoured the web for something about this, but see nothing. It works on Windows, but I don't think that's very significant, as that wraps fork() somehow.

I can work round this - fine, but I think it's essential to understand it, or we can't use fork(), and that'd be a problem for us (we can't be worrying about only using fork() if we've no files open.)

Has anyone seen this, or know of any guidance in the scriptures?

Thank you!

Abe


In reply to HPUX corruption of file handle after fork by Abe

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.