Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Forking problems

by fawkin (Initiate)
on Mar 21, 2008 at 14:09 UTC ( [id://675419] : perlquestion . print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Forking problems
by poolpi (Hermit) on Mar 21, 2008 at 14:14 UTC

    from perldoc

    It returns the child pid to the parent process, 0 to the child process, or undef if the fork is unsuccessful

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
Re: Forking problems
by lidden (Curate) on Mar 21, 2008 at 16:31 UTC
    Here is a little example of using fork():
    if(my $pid = fork){ print "I am the parent my pid is $$ and my child has pid: $pid\n"; } elsif(defined $pid and $pid == 0){ print "I am the child and my pid is $$\n"; } else{ print "Oh no :-(\n"; }
    Update: Oops fixed bug pointed out by pc88mxer below.
      undef will evaluate to 0 in numeric context, so you'll really want:
      if (!defined(my $pid = fork)) { print "Oh no\n"; } elsif ($pid == 0) { print "in child\n"; } else { print "in parent\n"; }
      The and $pid == 0 in your updated solution is redundant. It will always be true.
Re: Forking problems
by Fletch (Bishop) on Mar 21, 2008 at 14:14 UTC

    As anyone can see by the relevant code you posted along with your incredibly verbose and detailed question the problem lies on line 17. HTH.

    P.S. See How (Not) To Ask A Question.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.