develooper Front page | perl.perl5.porters | Postings from December 2004

[perl #30040] fork in BEGIN repeats subsequent lines of script

From:
Steve Peters via RT
Date:
December 23, 2004 07:16
Subject:
[perl #30040] fork in BEGIN repeats subsequent lines of script
Message ID:
rt-3.0.11-30040-103490.12.2277061855689@perl.org
> [Robin.Barker@npl.co.uk - Wed Jun 09 02:17:44 2004]:
> 
> As far as I can see, there is no real patch for this 
> as it is a Solaris problem, not a perl problem.
> 
> All I can suggest is some documentation:
> 
> 	When exiting from a child process that has been generated by 
> 	C<fork> (including C<open(FOO, '|-')> and C<open(FOO, '-|')>)  
> 	if the process may be started from C<BEGIN> (including C<use>) 
> 	and might be running on Solaris, you may need something like the 
> 	following to avoid the child process resetting the file pointers.
> 
> 		exit unless $Config{archname} =~ /\bsolaris\b/;
> 		require POSIX;
> 		POSIX::_exit(0);
> 
> Anyone any suggestions where such documentation should go.
> 
> Robin
> 
> -----Original Message-----
> From: perl5-porters@ton.iguana.be [mailto:perl5-porters@ton.iguana.be]
> Sent: 05 June 2004 12:17
> To: perl5-porters@perl.org
> Subject: Re: [perl #30040] fork in BEGIN repeats subsequent lines of
> script
> 
> 
> In article <20040605105436.GC1147@plum._lirble.org>,
> 	Nicholas Clark <nick@ccl4.org> writes:
> > Use POSIX::_exit:
> > 
> > use POSIX;
> > BEGIN { unless( fork ) { POSIX::_exit(0) } }
> > printf "LINE = %d\n", __LINE__; i
> > 
> > This doesn't print __LINE__ twice for me on Solaris.
> > 
> > Nicholas Clark
> 
> Mmm, is this again a case of solaris setting the filepointer backwards 
> in the child ?
> 
> theory:
>   -parent parses input file, reads all into stdio buf, starts parsing
>   -child is queued for start while stdio pos at end of line 1
>   -parent parses line 2, reaches the end of the stdio buffer and emits
>    another read
>   -now its the childs turn, it exits and sets the filepos back to the end 
>    of line 2
>   -parent reads line 2 again, and on the next read gets EOF
> 
> so the parent saw line 2 twice
> 

Should this go into perlfaq8, or does anyone have a better suggestion
about where it should go?




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About