Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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]
[holli]: i am testing
[holli]: something
[holli]: so
LanX ts ... always those operl testers ...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2017-09-24 13:08 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (274 votes). Check out past polls.