Re: Executing System Program

Dodger <[email protected]> Tue, 18 Dec 2007 19:52:40 -0800
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
On 18/12/2007, Greg Meckes <[email protected]> wrote:
> Greetings, I have the following task:
>
> I have to run a Perl script to run spawn multiple processes on a Linux server. The issue is that
> each process is huge and may take a while to run.
>
> I simply want my program to run, then start process 1 and let it run in the background, then
> immediately start process 2 etc.
>
> Example: let's say I want to tar several directories, and each is 5 GB in size. I would like
> program to run and spawn separate tar processes for each and NOT sit there and wait for each one
> to finish before it goes to the next.
>
> I have tried system, but it runs and waits and returns the output to the screen. Exec sits there
> and waits too.
>
> Right now I run through a loop and on each iteration I do:
> tar -xvf foo.tar
> chdir($dir);
>     my @args = ("tar -cvf $File");
>     system(@args) == 0 or die "system @args failed: $?";

Something like this...

my (%kids, @kids);
for my $file (replace_with_your_loop()) {
    my $k;

    if ($k = fork) {
        $kids{$k} = 'running';
        push @kids, $k;
    }
    else {
        my @args = ("tar", "-cvf", $file);
        system @args and die "system @args failed: $?";
    }
}

# wait for all kids to come home
my $done;
while (($done = wait) > -1) {
    next unless $done;
    $kids{$done} = 'done';
    print "Child process $done exited with status $?\n";
    print join("\n" map "$_ : $kids{$_}", @kids), "\n";
}


Note that I have had problems on Solaris 9 where Solaris's fork does
not actually return the proper process ID of the child process. Never
figured out why though. It was a pain.

-- 
Sean "Dodger" Cannon

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