How do I fork a new process and get back its PID in Perl?
Question
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?
Solution
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
}
OTHER TIPS
yes, fork
Quoting from that page:
It returns the child pid to the parent process,
0
to the child process, orundef
if the fork is unsuccessful.
fork returns child pid to the parent and 0 to the child.
Well, Perl's fork
function returns PID
of child to parent and 0 to child, isn't that what you want?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow