Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Multiple forks get re-executed

by PrakashK (Pilgrim)
on Jul 10, 2001 at 00:18 UTC ( #95136=note: print w/ replies, xml ) Need Help??


in reply to Multiple forks get re-executed

You must explicitly terminate the child process that fork created, once it's job is done. Otherwise, it continues to exist.

In your pseudo-code, the code after fork is executed by both the parent and the child. If you want only one of them to do "stuff" (usually child -- that's why you forked, right?), you must explicitly check the return code of fork to determine the identity of the process and assign "work" to it.

fork returns the pid of the child process to the parent, and returns 0 to the child process.

Please see below:

sub RUN { my $arg = shift; if (fork) { # parent process # do whatever parent is supposed to do wait; } else { # child process do stuff on $arg; exit; } }
In practice, you should check for possible errors in the fork call. Please see the camel book for an example (p. 715 in 3rd edition).

/prakash


Comment on Re: Multiple forks get re-executed
Download Code
Re: Re: Multiple forks get re-executed
by pltommyo (Initiate) on Jul 19, 2001 at 18:30 UTC
    Thanks. This is what I ended up using. The code is a generalized subroutine that does a task for my users, but it is at their discretion as to whether they wish to fork or not, as it is also at their discretion to choose where to reap their children. Tommy O

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-07-22 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (123 votes), past polls