Re: Executing System Program

"Wagner, Chris (GEAE, CBTS)" <[email protected]> Wed, 19 Dec 2007 00:11:54 -0500
Newsgroups gmane.comp.db.mysql.perl
Organization GE Aircraft Engines \Cincinnati Bell
Message-ID <[email protected]>
For the sake of completeness I'll offer this.  Portability with
fork-like functions is a stickty wicket in Perl.  Something that works
under unix might not work (read: does not) under Windows.  I put
together this little fork-open script and tested it on unix and
Windows.  This is a tad obnoxious and really the "wrong way" to do this
but unfortunately the "right way" doesn't work everywhere.


$SIG{CHILD} = "IGNORE";
$a = 65;
foreach $dir (@dirs) {
	if ($pid = fork) {
		# parent here
		print "post fork here! spawned $pid for $dir\n";
	}
	elsif (defined $pid) { # $pid is zero here if defined
		# child here 
		my $b = chr($a);
		close STDOUT; close STDIN; close STDERR;
		open "$b", "tar -cvf $dir |" or print "could not exec!\n";
		exit;
	}
	else {
		# weird fork error
		die "Can't fork: $!\n";
	}
$a++;
}
exit;
__END__



Greg Meckes wrote:
> 
> thanks Chris. Fork is confusing to me. How can I execute the following using fork?
> 
> -----SNIP
> 
> opendir (DIR,"/somedir") or die "Couldn't open somedir: $!\n";
> my @list  = grep (!/^\.\.?$/, readdir(DIR));
> closedir(DIR);
> 
> foreach my $dir (@list) {
> 
>     #Spawn this tar command, then continue.
>     #Don't wait for it to finish.
>     #Go immediately to the next one and
>     # spawn another process of tar - as many as needed.
> 
>     my @args = ("tar -cvf $dir");
>     exec @args or print STDERR "Couldn't exec tar -cvf $dir: $!";
> 
> }



-- 
Chris Wagner
CBTS
GE Aircraft Engines
[email protected]

-- 
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe:    http://lists.mysql.com/[email protected]