fdegir has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a script which spawns 5 parallel processes. This script runs correctly when I use it but when another user tries to run it, it starts correctly but in couple of seconds, 2 processes exit and remaining 3 processes continue running as normal. What can be the reason for this? Addition: Below script is not the script itself. I just wanted to show the part processes created. In real script, each process takes couple of hours to finish. 2 exiting processes don't touch files in the script when different user tries to run it. Simplified script is below,
#!/usr/bin/env perl for($i = 1; $i < 6; $i++){ my $pid = fork(); if($pid){ print "INFO\t: Started job $i (PID: $pid)\n"; push @childs, $pid; }elsif($pid == 0){ pJob($i); exit 0; }else{ die "ERROR\t: Can not fork:$!\n"; } } for(@childs){ my $tmp = waitpid($_, 0); } sub pJob($){ my $id = shift; `touch /tmp/file_$id`; print "INFO\t: Done with job $i\n"; }
OS is solaris10. Any comments?

Replies are listed 'Best First'.
Re: Problem with Parallel Processes
by almut (Canon) on May 31, 2010 at 14:40 UTC
    but in couple of seconds, 2 processes exit and remaining 3 processes continue running

    As the given script doesn't even run a single second, I suppose this is not the real code that actually exhibits the problem...  So, can you replicate the issue when you add a simple sleep to pJob()? If not, what is the real code doing that might make the two processes terminate prematurely?

Re: Problem with Parallel Processes
by JavaFan (Canon) on May 31, 2010 at 14:43 UTC
    First of all, you aren't correctly determining the fork fails. If the fork fails, $pid will be undefined. But undef == 0 is a true statement (which will generate a warning, if you have them turned on).

    I don't understand the but in couple of seconds, 2 processes exit and remaining 3 processes continue running as normal bit. All the processes are doing are forking, printing, and touching files. I'd expect the entire process, including the children, to be finished within a single second.

    Note however that if another user runs this as well, the touch will likely fail (depending on the umask of the original user) - but that shouldn't matter in whether or not the processes fail (and hardly in how fast they are).