in reply to Main program exits even before the child process completes on windows

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.
  • Comment on Re: Main program exits even before the child process completes on windows

Replies are listed 'Best First'.
Re^2: Main program exits even before the child process completes on windows
by vivekarcot (Initiate) on Sep 02, 2011 at 05:25 UTC

    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();

    }

    }