Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: fork - child executes code outside of block

by ikegami (Patriarch)
on Mar 28, 2018 at 18:47 UTC ( [id://1211938]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211938]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found