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

Im trying to get this fork thing down. I created a program that kills a child process after a certain amount of time if it hasn't exited. However I do not know how to get the child's PID, fork return 0 when its a child not its PID. Right now Im using 1 + the programs id but if something gets invoded before my fork I know that that might not be the case. So any idea's on how to get this. Thanks.
#!/usr/bin/perl -w use strict; use POSIX qw(:errno_h); use POSIX qw(:sys_wait_h); $SIG{CHLD} = "IGNORE"; my ($child, $script, $cleanup, $time); &create_child; sleep($time); &reaper; sub create_child { unless($child = fork()) { die "Cannot fork: $!" unless defined $child; exec($script); } $child++; } # Some values are from get_args sub not shown sub reaper { if(kill 0 => $child) { print "Child timed out, sending signal 15\n"; kill 15, $child; exec($cleanup); } elsif ($! == EPERM) { print "Child has changed its UID\n"; } elsif ($! == ESRCH) { print "Child is already dead\n"; } else { warn "Could not check child's PID: $!\n"; } }

Replies are listed 'Best First'.
Re: Child PID?
by Juerd (Abbot) on Jun 06, 2002 at 21:06 UTC

    Erh, fork already returns the child's pid (to the parent, if succesful). There is absolutely no need to increment it. If there is, your operating system is seriously broken.

    if (my $pid = fork) { # parent } elsif (defined $pid) { # child exit; } else { # error }

    Some style issues: Don't use &foo: use foo() instead. &foo has some unpleasant side effects (passes @_ if you don't use parens. Put subs near the top to avoid sharing _all_ file scoped lexicals. This will be useful when you use strict. Third, use strict. Reasonably important: check the return value of kill. Reaping and killing are not the same, by the way. Use waitpid or wait to reap.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      I found out what was wrong. I was launching netscape for the first time and box that asks you a question has a differt PID than when the actual browser is launched. Anyway Im still a little confused about the difference between reaping and killing. In this situation would reaping be better than killing?

        Reaping is generally used to describe the action of terminating zombie processes, which are children that have outlived their parent. Since their parent does not exist to clean up after the child, the kernel's process table retains the defunct child. Such zombies must be reaped.

        Killing a process is a more general term used to describe the act of terminating an active process.

        If you are trying to automate the response to a dialog box, that's different. Just killing it could be bad.


Re: Child PID?
by jsprat (Curate) on Jun 06, 2002 at 21:02 UTC
    Why are you incrementing $child? When you call fork from the parent, the return _is_ the child's pid. If you call fork from inside the child, the return is zero. That's how a script knows if it is the parent or the child process.