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

I am running a program in which i am using 10 forks. The different processes starts and intend to go for 3-4 hours. But the main exits after some time without any error message. I ll paste the snippet of the code here.

for ( my $count = 1; $count <= 10; $count++) { my $pid; if ($pid=fork) { } else { exec(&sub1($count, $lines,\@cnArray)); exit; # Ends the child process. } } while (wait() != -1) { }

It exits in the wait i guess and do not traverse the code which is below. Please help.

  • Comment on Main program exits even before the child process completes on windows
  • Download Code

Replies are listed 'Best First'.
Re: Main program exits even before the child process completes on windows
by cdarke (Prior) on Sep 01, 2011 at 14:30 UTC
    Which OS are you running on? I ask because sometimes try to use fork/exec on Windows, and it doesn't do what they expect.

    Assuming Linux/UNIX, then you could check the PID returned from wait, rather than just looking for -1, and track which child processes are ending, and what their exit code is ($?).

    You do not show any error handling, maybe because it is only a code fragment, but if fork returns undef then it failed (usually because too many processes are running) and exec should not return, so the exit should be reporting an error ($!).

    Then again, someone could be killing the parent, did you check its exit code?

    Update: Opps! I did not read the title correctly, you are using Windows. fork creates a new thread on windows, and exec spawns a new process, so your exit is probably closing down the main process.

      Thanks for the reply. But even without exit, i face the same issue.

      Even if i use threads instead of fork, my main program exits even though the threads have not finished. I am not very well with debugging efforts in perl. With threads, the code looks something like this.

      my @threads;

      for ( my $count = 1; $count <= 10; $count++) {

      my ( $lines,@cnArray) = getLineCount($count);

      my $t = threads->create(\&sub1, $count, $lines, \@cnArray);

      push(@threads,$t);

      }

      foreach my $thr_num (@threads) {

      while ($thr_num->is_running()) {

      sleep(10);

      }

      if ($thr_num->is_joinable()) {

      $thr_num->join();

      }

      }

Re: Main program exits even before the child process completes on windows
by chrestomanci (Priest) on Sep 01, 2011 at 15:05 UTC

    As cdarke said, under windows perl emulates fork with threads, and that is likely to break if you call exec in the child threads.

    It has been a long time, but in the past, I have had success with Win32::Process. I suggest you use that to start each of your worker processes, and store the process objects of each in an array or has in the parent perl process. Then when they have all started, just call $ProcessObj->Wait($timeout) on each in a loop until they all quit.

    The other thing I would do would be to experiment with starting instances of familiar windows GUI programs such as notepad that you can easily quit manually, so that you can quickly get things working before switching to long running programs.

Re: Main program exits even before the child process completes on windows
by RichardK (Parson) on Sep 01, 2011 at 16:07 UTC

    Why are you calling exec in your child ?

    An alternative approach may be to use Parallel::ForkManager. then your code could look something like this

    my $pm = new Parallel::ForkManager(10); for my $count (1..10) { $pm->start and next; # do the fork sub1($count,$lines,\@cna); $pm->finish; # do the exit in the child process } $pm->wait_all_children;