Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

using fork

by teamassociated (Sexton)
on Oct 14, 2013 at 15:31 UTC ( [id://1058194]=perlquestion: print w/replies, xml ) Need Help??

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

Hi! Only used fork a few times so bare with me. I want to kick off a backup process then once it completes kick off another process. This sample script start its own process and waits. I want to mimic it. Where would I put my backup command, then the other commands to follow?
use strict; use warnings; # Fork child process my $pid = fork(); ############################################################ is this how? --> die "cannot fork: $!" unless defined($pid = open(KID +_TO_READ, "-|", "/usr/openv/netbackup/bin/bpbackup ..."); ############################################################ # Check if parent/child process if ($pid) { # Parent print "Started child process id: $pid\n"; } elsif ($pid == 0) { # Child sleep 30; # Example child process exit 0; # It is STRONGLY recommended to exit your child process # instead of continuing to run the parent script. } else { # Unable to fork die "ERROR: Could not fork new process: $!\n\n"; } ############################### print "Waiting for the child process to complete...\n"; waitpid ($pid, 0); print "The child process has finished executing.\n\n";

Replies are listed 'Best First'.
Re: using fork
by jethro (Monsignor) on Oct 14, 2013 at 16:06 UTC

    Are you sure you need fork at all? If you don't have anything to do while the backup process is doing its thing or you don't need the separate process as a safety feature (for example to check if the process completed) then you don't need it.

    If yes, the layout of your script depends also on why you need it. I'll just assume you need it for the latter reason, i.e. to monitor execution.

    Then the backup script goes where "sleep 30" is at the moment. Then either duplicate and append the whole script for the next command or if there are more than two or a variable number of commands put this whole script into a loop. Also you need a noblocking version of waitpid so that you really can monitor the child process instead of hanging indefinitely. See the documentation to waitpid

    Now if you want more comfort you should take a look at some of the powerful modules that handle all the details of forking, for example Parallel::ForkManager

Re: using fork
by moritz (Cardinal) on Oct 14, 2013 at 16:27 UTC

    What jethro said: Only fork if you need to do something in both processes.

    If that's the case, here is a working example:

    #!/usr/bin/env perl use 5.014; use strict; use warnings; my $pid = fork; if (!defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { # client process say "Client starting..."; sleep 10; # do something useful instead! say "Client terminating"; exit 0; } else { # parent process say "Parent process, waiting for child..."; # do something useful here! waitpid $pid, 0; } say "Parent process after child has finished";

    One thing to note is that fork returns undef on failure, and undef == 0 compares as true, so your error handling wouldn't work. To easily test the error case, start a new bash and set ulimit -u $number_of_allowed_processes, then start the perl script.

Re: using fork
by Laurent_R (Canon) on Oct 14, 2013 at 16:51 UTC

    My first reaction reading your code was exactly the same as the previous monks': why would you want to fork if the parent is just going to wait for the child to complete?

      Indeed, why fork when spooning can be so much fun? (There is a bad joke about forking, spooning and the creation of unexpected child processes in there somewhere.)

      The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-28 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found