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

Proper Forking

by P0w3rK!d (Pilgrim)
on Jul 24, 2001 at 23:01 UTC ( [id://99450]=perlquestion: print w/replies, xml ) Need Help??

P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script 'A.pl' which executes a perl script 'B.pl'
that executes (1 sh script, 1 perl script, and 1 shell script).

I think an exit code from B.pl is causing A.pl to exit.

How do I properly form a fork to get it to work correctly?
I've tried the following and it just hangs:
# A.pl unless(fork) { sleep 1 until getppid == 1; for (1..10) { @argsfoo = ("$cd $TSTDIR; B.pl"); if (system(@argsfoo) != 0) { ... } else { ... } } exit 0; } wait;

Replies are listed 'Best First'.
Re: Proper Forking
by nardo (Friar) on Jul 24, 2001 at 23:17 UTC
    In the child process you:
    sleep 1 until getppid == 1;
    which will loop until the child processes parent is pid 1 which will happen after the parent terminates. Then in the parent you:
    wait;
    which will wait for the child to terminate. So you have the child waiting for the parent to terminate and the parent waiting for the child to terminate thus they are both stuck. Normally when you fork you don't wait for your parent to be init so i would get rid of the sleep until line, also the example you provided might just as well be written without a fork since the parent does nothing other than wait for the child to finish its job the parent could just as well have done that job instead.
Re: Proper Forking
by decnartne (Beadle) on Jul 24, 2001 at 23:33 UTC
    in answer to your first question, one way to use fork() that has worked for me is the following:
    unless( fork )
    {
       exec "/some/cmd/here";
    }
    
    i believe your script hangs since your getppid never returns a 1. instead, it is returning the pid of the forked child's parent - in this case A.pl - which will not be process id 1. process id 1 is probably your box's /etc/init process if it's unix and not even there if on win32...

    decnartne ~ entranced

(tye)Re: Proper Forking
by tye (Sage) on Jul 25, 2001 at 03:11 UTC

    To simply execute another script, I'd just use system and skip fork altogether.

            - tye (but my friends call me "Tye")
Re: Proper Forking (silly)
by suaveant (Parson) on Jul 24, 2001 at 23:21 UTC
    Sorry... hands... won't... stop...

    There is of course only one way to do Proper Forking in the Monastery... ;-)

    Sorry... I'll be quiet now that I said that...

                    - Ant

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://99450]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-16 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found