Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

I am having a fork problem

by krujos (Curate)
on Jun 12, 2002 at 14:27 UTC ( #173842=perlquestion: print w/replies, xml ) Need Help??
krujos has asked for the wisdom of the Perl Monks concerning the following question:

I am running some automated tests at night and I want to catch it when a client hangs. To do this I have a forked process in a much bigger script. The forked part basically sleeps, and then goes into an infinite loop to check for hung clients, the problem is I cant stop this from killing the parent process.
A bit of explanation about the code. I am not killing the hung clients with kill but rather canceling them through a software app.
The steps I have taken so far are to save the parent pid and compare it to the PID being read in. If they match I donít stuff it into the list of things to be killed. Below is the offending code
### BEGINING OF FORK # my $kidpid; my $parentpid = $$; if (!defined($kidpid = fork())) { # fork returned undef, so failed print "cannot fork: $!"; } elsif ($kidpid == 0) { sleep(3600); my ($date, $Stime, $Ctime, $J, $Temp1, $Temp2, $bpdbjobs); my (@JOBID, @PIDS, @times, @bpdbLines); while(1) { ### parsing the input $bpdbjobs = `$BPDBJOBS | grep Active | grep -v PID`; @bpdbLines = split /\n/, $bpdbjobs; for($J = 0; $J < @bpdbLines; $J++) { $bpdbLines[$J] = $bpdbLines[$J] . "\n"; $bpdbLines[$J] =~ /^(\d+)\s/; $JOBID[$J] = $1; $bpdbLines[$J] =~ /\s(\d+)$/; $PIDS[$J] = $1; if( $PID[$J] == $parentpid) { $PIDS[$J]=undef; $JOBID[$J]=undef; $J--; } } for( $J = 0; $J < @PIDS ; $J++) { #get the time the jo +b started chomp ($times[$J] = `/bp/bin/bpps | grep $PIDS +[$J] | /usr/local/bin/perl -lane 'print \$F[4]'`); $times[$j]=parsedate($times[$j]); } ### time checks $Ctime = time; for($J = 0; $J < @PIDS; $J++) { $Temp1 = $Ctime - $times[$j]; if($Temp1 > 7200) { $Temp2 = "/usr/openv/netbackup/bin/adm +incmd/bpdbjobs -cancel $JOBID[$J]"; #cancel if job runs longer than 2 hours print "$Temp2\n"; system($Temp2); unless (open (todayHungClientsLogP, "> +>$HungClientsLog")) { print (" HungClients: unable t +o open output <$HungClientsLog>\n"); } else { # print (todayHungClientsLogP +" Running Time = $Temp1 >>"); print (todayHungClientsLogP "$ +Temp2\n"); close (todayHungClientsLogP); } } } sleep(1800); $alive = `/bin/ps -ef | grep $parentpid | grep -v grep +`; unless($alive ne "") { exit; } } } else { #do the rest of the script here.

I have tested my basic idea to make sure it works with this script.
#!/usr/local/bin/perl -w use strict; my $parentpid = $$; my $kidpid; print "This is the original process the pid = $$\n"; if (!defined ($kidpid = fork())) { #fork failed. print "Can't fork: $!"; } elsif ($kidpid ==0) { print "\tThis is the child process.. parentpid = $parentpid\n" +; print "\tThis is the child process its pid = $$\n"; } else { sleep(1); print "This is the return to the parent process\n"; }

and this is the output I get from that.
This is the original process the pid = 5292 This is the child process.. parentpid = 5292 This is the child process its pid = 5293 This is the return to the parent process

Any help you have to offer would be great.

Replies are listed 'Best First'.
Re: I am having a fork problem
by Zaxo (Archbishop) on Jun 13, 2002 at 05:40 UTC

    You don't know whether the child or the parent will run first after a fork. You test code could print

    This is the original process the pid = 5292
    This is the return to the parent process
            This is the child process.. parentpid = 5292
            This is the child process its pid = 5293
    next time.

    If the issue is unexpected death of the parent, it would be good to show the parent's code. You may be running off the end of the file to end naturally.

    In any case, the child appears to loop, running once every hour and a half or more for as long as the parent is running. That loop might be more clearly written as:

    while (kill 0, $parentpid or exit 0) { # ... }
    instead of parsing ps output and applying a double negative condition to exit.

    I have no idea whether your main processing is appropriate or not, since you don't say what is run by $BPDBJOBS or what data it gives back. The match and assignment to $1 sequences look a little awkward, but may be all right.

    Your trial code is apt to be littering the machine with zombies, might want to check that

    After Compline,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://173842]
Approved by DamnDirtyApe
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-06-24 00:41 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (555 votes). Check out past polls.