Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^5: System Function...script does not move until process ends.

by RazorbladeBidet (Friar)
on Apr 05, 2005 at 19:56 UTC ( #445092=note: print w/replies, xml ) Need Help??


in reply to Re^4: System Function...script does not move until process ends.
in thread System Function...script does not move until process ends.

try something like:
use POSIX ":sys_wait_h"; sub startTheDB { (defined $pid = fork()) or die "Unable to fork!\n"; if ( !$pid ) { exec "StartTheDB" or die "Can't start the db!\n"; } else { do { $kid = waitpid(-1, WNOHANG); } until $kid > 0; } }
Note that you'll want to test the db from the parent after you return from the sub.


Updated: It still waits for the child - as pointed out by frodo72

Just a note that the scary looking stuff (e.g. the waitpid) is explained in the docs mentioned throughout this node and here
--------------
"But what of all those sweet words you spoke in private?"
"Oh that's just what we call pillow talk, baby, that's all."

Replies are listed 'Best First'.
Re^6: System Function...script does not move until process ends.
by polettix (Vicar) on Apr 06, 2005 at 18:25 UTC
    Sorry, I don't see the point.

    If my synapses are not too cluttered due to hanger (which could be, it's dinner time here :), your sub keeps the parent waiting for child's death, which is exactly what system does. But while system does this "efficiently", putting the parent process to sleep (I hope this happens in Windows as well, can't swear on it >;), this function does a quite active wait, given the WNOHANG option. So, it seems to me that not only this does not solve the problem, but increases CPU load!

    What am I missing (apart a good dinner)?

    Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

    Don't fool yourself.
      What you're missing is that exec abandons the process after it starts it. So it will only wait until the child dies, which should be right after it starts the database :)

      I had reservations about this when I wrote it, and I confirmed them:
      #!/usr/bin/perl -w use strict; use POSIX ":sys_wait_h"; defined (my $pid = fork()) or die "D'oh"; if ( $pid ) { print "$$ gonna wait...\n"; my $kid; do { $kid = waitpid $pid, WNOHANG; } until ( $kid > 0 ); print "Ok, done waiting\n"; } else { exec 'sleep 10; echo "Done sleeping\n"'; }
      What I had thought it would do was to start the execution of the command and then bail. Well.. it didn't. It waited.

      Now go get that dinner.

      No dinner for you :)
      --------------
      "But what of all those sweet words you spoke in private?"
      "Oh that's just what we call pillow talk, baby, that's all."
        Too late, I had a wonderful dinner yesterday, based on hand-made pasta with shrimps and then grilled swordfish. Just to reward myself of being right ;-)

        Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

        Don't fool yourself.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-10-28 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (259 votes). Check out past polls.

    Notices?