blkstrat has asked for the wisdom of the Perl Monks concerning the following question:

oh monks, I seek the answer to a vexing question: I have some code which I need to add a section that forks to handle some long-running process. I already have some open file handles. When I add the forking code, I see errors with File::Remove::remove at line 109, where it tries to expand a file name with glob (which fails). My question is this, how can I make the child exit cleanly without executing any further code it has inherited? I am not sure exactly what behaviour I should expect with a fork (for I am a forking virgin - please be kind!).

Replies are listed 'Best First'.
Re: my children are driving me crazy
by tachyon (Chancellor) on Jul 03, 2001 at 06:51 UTC

    So are mine! Your title made me laugh, thanks. Anyway consider this code:

    my $pid = fork; die "Can't fork!" unless defined $pid; &do_parent_stuff if $pid != 0; &do_kids_stuff if $pid == 0; exit;

    This is the essence of forking. The command fork creates two *almost* identical copies of the executing program. Both parent and child share everyting except the fork returns the systems process identifier (for the new child process) to the parent and 0 to the child. It returns undef if it fails. Thus by assigning the return value of fork to $pid our program can tell who it is - parent or child and behave accordingly.

    Normally you would use an if/else structure and not botther with the == or != . They are the being gentle bit!

    Hope this helps. There is heaps of info on fork on the site. Use super search with fork in the name and content fields

    tachyon

    s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print