tags:

views:

272

answers:

4

My issue is related to using fork() within Perl code. I wish to fork a new process and capture its PID and return it back to the callee program. Is there some command in Perl which would make this possible?

+13  A: 

yes, fork

Quoting from that page:

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

dsm
+4  A: 

Well, Perl's fork function returns PID of child to parent and 0 to child, isn't that what you want?

Michael Krelin - hacker
+6  A: 

fork returns child pid to the parent and 0 to the child.

Arkaitz Jimenez
+5  A: 
my $pid = fork();
if ($pid == 0)
{
    # We are the child.
}
elsif defined($pid)
{
    # We are the parent of child with PID=pid
}
else
{
    # The fork failed
}
David Sykes
'We are the proud parent ...' unless the child was stillborn...there's a third path - if $pid is undef.
Jonathan Leffler
Updated, thanks
David Sykes