in reply to Re: fork - child executes code outside of block
in thread fork - child executes code outside of block

Why split the parent's code like that?

defined( my $pid = fork() ) or die("fork: $!"); if (!$pid) { print "CHILD --" . "\n"; exit; } print "PARENT --" . "\n"; waitpid($pid, 0) ; print "-- PARENT " . "\n"; print "PID - $pid , MAIN PRG --" . "\n";

Replies are listed 'Best First'.
Re^3: fork - child executes code outside of block
by tybalt89 (Monsignor) on Mar 28, 2018 at 18:53 UTC

    I just made the minimum change to fix the problem.

    It's not my favorite way of doing fork, which is:

    if( my $pid = fork ) { # parent } elsif( defined $pid ) { # child exit; } else { die "$! on fork"; }

      The question wasn't necessarily directed at you originally, but you showed that it applies to you too. Why do you arbitrarily split the parent code in three (partly before the if, partly in the if, partly after the if)? And why do you hide the error handling code far from where the error code occurred?

        To me fork() is essentially a three way branch or switch. I enclose all three parts in the same statement.