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

Re^3: forking and timeouts

by Crackers2 (Parson)
on Aug 09, 2007 at 15:02 UTC ( [id://631580]=note: print w/replies, xml ) Need Help??


in reply to Re^2: forking and timeouts
in thread forking and timeouts

No it's the other way round. It will always be 0 in the child process, and the PID of the child process in the parent.

To show it a bit clearer perhaps:

$pid = fork(); if (!defined $pid) { print "Fork failed: $@"; } elsif ($pid) { print "I am the parent, my PID is $$ and my child's PID is $pid\n"; } else { print "I am the child, my PID is $$ and my parent's PID is " . getpp +id() . "\n"; }

Replies are listed 'Best First'.
Re^4: forking and timeouts
by Earindil (Beadle) on Aug 09, 2007 at 16:10 UTC
    So based on the comments and more reading. This is what I came up with. Make more sense? The bolded parts are ones that I'm not sure I even need. Bits and pieces of this were pulled from other scripts I came across.
    foreach my $node (@nodes) {
            print "$node ";
            chomp $node;
            wait_for_a_kid() if keys %pid_to_node > 8;
            $pid = fork;
            if ($pid) {
                    ## parent does...
                    $pid_to_node{$pid} = $node;
            }
            else {
                    local $SIG {ALRM} = sub {
                    kill -15, $$ or die "kill: $!";
                    print "\tKilled PID $$\n"}; # Just SIGTERM.
                    eval {
                            ## child does...
                            setpgrp(0,0);
                            print $$."\n";
                            exit !&GetSvrStatus($node);
                            alarm 5;
                            waitpid $pid => 0;
                    };
            }
    }
    ## final reap:
    1 while wait_for_a_kid();
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found