in reply to creating multiple instances of a process

The wait function will take care of your needs. Here's how I like to organize it,

my %kid; for (0 .. $num) { defined(my $cpid = fork) or warn $! and next; $kid{$cpid} = undef, next if $cpid; # do kid stuff exit 0; } delete $kid{wait()} while %kid;

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: creating multiple instances of a process
by sweetblood (Prior) on Jun 20, 2005 at 14:04 UTC
    Sorry for the delay in responding, but I've been banging my head against this for awhile. I've tried what I believe'd is what you were telling me, but I'm a little dense. The system I'm on is redhat if that helps. Here is my ammended code after your suggestions, let me know what you think.
    #!/usr/bin/perl -w use strict; chomp (my $curdir = `pwd`); my $proc = '../delme.pl > proc.log 2>error.log'; my %kid; for (1 .. 4) { chdir $curdir; do { mkdir "test$_" or die $! } unless (-d "test$_"); chdir "test$_" or die $!; print "Launching: Process $_$/"; defined(my $cpid = fork) or warn $! and next; $kid{$cpid} = undef, next if %kid; exec $proc; exit 0; } delete $kid{wait()} while %kid; print "Done...$/";
    I get no errors but only one instance runs. I need four independant instances running and as you can see by code they should each run in their own directory. The delme.pl is trivial, essentially just printing a line to STDOUT as well as STDERR, then sleeping for 25 seconds.

    Thanks for your help!

    Sweetblood

      You have a typo in line 14 which is making both the parent and child run the child code and exit on the first time through the loop. Change . . . if %kid; to . . . if $cpid;.

      After Compline,
      Zaxo

        Indeed! That was the problem, Many Thanks!

        Sweetblood