Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Forking problems

by lidden (Curate)
on Mar 21, 2008 at 16:31 UTC ( #675458=note: print w/replies, xml ) Need Help??

in reply to Forking problems

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.

Replies are listed 'Best First'.
Re^2: Forking problems
by pc88mxer (Vicar) on Mar 21, 2008 at 17:00 UTC
    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"; }
Re^2: Forking problems
by ikegami (Patriarch) on Mar 22, 2008 at 01:14 UTC
    The and $pid == 0 in your updated solution is redundant. It will always be true.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2023-11-29 11:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found