Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: Fork() Function

by oiskuu (Hermit)
on May 13, 2014 at 17:12 UTC ( #1085931=note: print w/replies, xml ) Need Help??

in reply to Re^2: Fork() Function
in thread Fork() Function

Keep in mind that perl fork does not return -1, it returns undef on failure. In your code, fork failure results in system() and exit() on the parent side. A useful idiom with fork is my $pid = fork() // die; you may want if (!defined $pid) {} else if ($pid) {} else {}.

Keep in mind that a call to system results in another fork and wait, hence your work runs in a child of a child. You may have intended a direct exec instead of system.

Your OP question is somewhat ambiguous. Just in case, I'll mention that there is no guarantee that a newly spawned child has a different pid from some earlier (already reaped) process. Pid numbers are recycled.

Replies are listed 'Best First'.
Re^4: Fork() Function
by carlriz (Beadle) on May 14, 2014 at 15:03 UTC

    I like your response a lot, because it makes perfect sense. So, in my case, I want to execute a separate script in the child process (which is the system call in the code posted). My while loop continuously grabs commands from a mysql database. In the event that a stop command is encountered, my loop will kill the child process, stopping the script that was started with a 'start' command. I agree you that a system command may be unnecessary in this case. But, with an exec call, I don't think it returns a child ID, so how would I keep track of the script and end it when the command comes in?

    General Question: When I call system, there is an implicit wait call until the child finishes?

    An Approach: I think someone mentioned that an array would be useful, to keep track of the child pid's. I am trying that route, because it may work in my case. Thoughts?

      General Question: When I call system, there is an implicit wait call until the child finishes?
      Yes. See "perldoc -f system"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1085931]
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2018-06-25 16:05 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.