Abe has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.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 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HPUX corruption of file handle after fork
by roboticus (Chancellor) on Dec 10, 2007 at 16:07 UTC | |
by Abe (Acolyte) on Dec 10, 2007 at 16:49 UTC | |
|
Re: HPUX corruption of file handle after fork
by almut (Canon) on Dec 10, 2007 at 16:31 UTC | |
by Abe (Acolyte) on Dec 10, 2007 at 17:07 UTC | |
by almut (Canon) on Dec 10, 2007 at 17:32 UTC | |
by Abe (Acolyte) on Dec 10, 2007 at 17:45 UTC | |
by sgt (Deacon) on Dec 10, 2007 at 22:50 UTC | |
|