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

Could you give me more of an example of what fork does. I think this what I want to use. I'm trying to set a value back to 0 after the database is started. Once that value is 0 then I want to connect to the database and proceed to unload some tables from it.

Thanks,

Bobby

  • Comment on Re^2: System Function...script does not move until process ends.

Replies are listed 'Best First'.
Re^3: System Function...script does not move until process ends.
by Joost (Canon) on Apr 05, 2005 at 18:40 UTC

      Ok, excuse my ingnorance on this fork issue. I have never used it before. Could someone please take a look at the code I wrote (above) and suggest something or how I should do it.

      After I start the database again, I want to be able make a connection to it using the dbi module in perl. I've done that, but I need to know more or shown the suggestion on how fork works.

      Thanks,

      Bobby

        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."
Re^3: System Function...script does not move until process ends.
by Mugatu (Monk) on Apr 05, 2005 at 20:47 UTC

    Is the code you pasted a simplified sample of the original? If not, I do not understand why you have the whole $stopvalue business in the first place. Your code progresses linearly, and the if ($stopvalue == 1) condition is pointless. At that point, $stopvalue is always set to 1.

    Putting that point aside, unless you already know what fork and exec do, it would probably be best to avoid them. They are the low level primitives, and the other available methods are usually simpler and more convenient. Doing the fork/exec yourself should be reserved for times when you really need it.

    In your case, I would lean toward a piping open, as it will provide enough control but also enough simplicity. As an added bonus, you'd be able to read the program's output, if necessary. Here's an example:

    my $pid = open my $lsfh, "-|", "sleep", 10 or die "Cannot exec sleep: $!\n"; print "'sleep' is running in the background.\n"; print "The background process has a PID of $pid.\n"; waitpid $pid, 0; print "All done.\n";