Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how come both the if and elsif block getting executed ?

by ghosh123 (Monk)
on Apr 20, 2014 at 17:34 UTC ( #1082953=perlquestion: print w/replies, xml ) Need Help??

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

In this code below, when I am fork ing, depending on the $pid value the if and elsif should be executed, but here I see that the print statement in the if block is also getting satisfied alongwith the elsif block which calls the sub1 ().
Can anybody please explain ?

use strict; use warnings; print "Starting main program\n"; my @childs; for ( my $count = 1; $count <= 3 ; $count++) { my $pid = fork(); if ($pid) { # parent print "pid is $pid, parent $$\n"; push(@childs, $pid); } elsif ($pid == 0) { # child sub1($count); exit 0; } else { die "couldnt fork: $!\n"; } } foreach (@childs) { my $tmp = waitpid($_, 0); print "done with pid $tmp\n"; } print "End of main program\n"; sub sub1 { my $num = shift; print "started child process for $num\n"; sleep $num; print "done with child process for $num\n"; return $num; }

Replies are listed 'Best First'.
Re: how come both the if and elsif block getting executed ?
by zentara (Archbishop) on Apr 20, 2014 at 17:50 UTC
    You know, I never really questioned the syntax, but from my limited experience, it is a way actually executing the fork. The if($pid) part is true, if the system can fork and return a non-zero pid. So the fork occurs and the parent gets a pid, and the child receives a 0. It is the c way of forking. So both sections of code get executed, but one is in the parent and the other is the child.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: how come both the if and elsif block getting executed ?
by Anonymous Monk on Apr 20, 2014 at 18:13 UTC

    The whole point of the fork system call is (a little simplified) to make a copy of the process - from the point of the fork call onwards there are two copies of your code executing. In one of them, fork() returns the PID of the child (that's the parent), and in the other (the child), it returns 0. So yes, both the if and elsif blocks are supposed to be executed, but in different processes.

    Try prefixing all of your output with the current PID, $$:

    #!/usr/bin/env perl use warnings; use strict; print "[$$] Starting main program\n"; my @childs; for my $count (1..3) { if ( my $pid = fork ) { # parent print "[$$] child pid is $pid\n"; push @childs, $pid; } elsif ( defined $pid ) { # child sub1($count); exit 0; } else { die "[$$] couldn't fork: $!" } } for my $pid (@childs) { my $tmp = waitpid($pid, 0); print "[$$] done with pid $tmp\n"; } print "[$$] End of main program\n"; sub sub1 { my $num = shift; print "[$$] started child process for $num\n"; sleep $num; print "[$$] done with child process for $num\n"; return $num; }

    Which outputs, for example:

    [7685] Starting main program [7685] child pid is 7686 [7686] started child process for 1 [7685] child pid is 7687 [7687] started child process for 2 [7685] child pid is 7688 [7688] started child process for 3 [7686] done with child process for 1 [7685] done with pid 7686 [7687] done with child process for 2 [7685] done with pid 7687 [7688] done with child process for 3 [7685] done with pid 7688 [7685] End of main program

    Which looks fine to me. You get three children executing sub1 and a parent that manages the children.

Re: how come both the if and elsif block getting executed ?
by LanX (Sage) on Apr 20, 2014 at 18:54 UTC
    You are forking without knowing what fork means???

    Are you asking homework questions?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    PS: posting intended code is a courtesy towards the people who try to help you..

      I up-voted your post, based on the rational argument. But, at the same time, if you never ever did something in CS without really knowing what it does, then I would assume your CS knowledge is quite poor. But, of course, I know that this is not the case, so I am pretty sure you also occasionally did it. We are all doing it, at least from time to time.
        Well look at his records of >140 posts in 2 years so far (hence not a naive newbie).

        He's not only listed in worst nodes of the years, he was also frequently asked to stop posting interview questions.

        If he would at least show some efforts like reading the documentation or posting intended code.

        > then I would assume your CS knowledge is quite poor.

        Indeed it could be better. But at least I know when people are exploiting the good will of the monastery.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-05-18 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?