in reply to fork(): when parent exits, what happens to child?

Hi,

Thanks for the responses. The os is mac osx 10.4.11.

It doesn't seem like a buffering problem to me. If the child actually finished executing, then wouldn't the close() execute on the output file handle, thus flushing the buffer to the file?

Here is what happens when I turn on autoflush for the output filehandle:

use strict; use warnings; use 5.010; my $child_pid = fork; $| = 1; if (!defined $child_pid) { say "**My error**: couldn't fork: $!"; } if ($child_pid) { #then in parent say "in parent..."; sleep 1; } else { #then in child open my $OUTFILE, '>', 'data1.txt' or die "**My error: couldn't open data1.txt: $!"; my $old = select $OUTFILE; $| = 1; select $old; for (1 .. 10) { say $OUTFILE $_; sleep 2; } close $OUTFILE; }
--output:-- $ perl 1perl.pl in parent... $ cat data1.txt 1 2 $

Replies are listed 'Best First'.
Re^2: fork(): when parent exits, what happens to child?
by jethro (Monsignor) on Mar 26, 2010 at 16:39 UTC
    If the child actually finished executing, then wouldn't the close() execute on the output file handle, thus flushing the buffer to the file?
    Yes, but you would have to wait 20 seconds before the child has put out all 10 numbers because of the 'sleep 2' line. When I tried your script and executed the 'cat data1.txt' immmediately or 10 seconds after I got the shell prompt back I saw an empty file. But after 20 seconds the file suddenly had all 10 numbers in it. Because of buffering.

    Even if you turn off the buffering you should not expect more than one number every two seconds to arrive in data1.txt. Again because of 'sleep 2'. You could do a 'tail -f data1.txt' to see exactly what is happening in realtime

      Even if you turn off the buffering you should not expect more than one number every two seconds to arrive in data1.txt. Again because of 'sleep 2'.

      Of course! So then the rule is: when a parent terminates, the child keeps on executing.

        So then the rule is: when a parent terminates, the child keeps on executing.

        Indeed, you've forked a new process, but well behaved parents wait (or waitpid) on their children so they don't turn into zombies.

Re^2: fork(): when parent exits, what happens to child?
by ikegami (Patriarch) on Mar 26, 2010 at 16:25 UTC

    The os is mac osx 10.4.11.

    You were quoting documentation on the Windows emulation of forks.

      Ok, I missed this in perlfork:

      On some platforms such as Windows where the fork() system call is not available, Perl can be built to emulate fork() at the interpreter level...

      This document provides a general overview of the capabilities and limitations of the fork() emulation.

      I'm still looking for an answer to, "What happens to the child when the parent terminates on unix systems?"

      Based on some of my output, I would say: all children are killed. However, about 1 in ten times the child successfully writes all the data to the file. How is that possible if a child is killed when the parent terminates?

        I'm still looking for an answer to, "What happens to the child when the parent terminates on unix systems?"

        See rubasov's update