in reply to How to kill forked process

The reason IPC examples are complicated is because IPC is complicated! Think about all the ways that either the parent or the child can end, and how each is to be notified of the other's death.

That said, you might be able to get away with the following (mostly pseudo-) code:

my $pid = fork(); if ($pid) { # in the parent wait for a keypress via your favourite method here if an abort occurs.. kill 15, $pid; # sigspec to taste exit; # parent is done # you'll probably want POSIX WNOHANG here, otherwise waitpid will bl +ock waitpid, $pid # child exited normally } elsif (defined $pid) { # in the child system("run_groovy_command"); exit; # tell daddy i'm done } else { die "Cannot fork: try spoon\n"; }

Replies are listed 'Best First'.
Re^2: How to kill forked process
by redss (Monk) on Feb 17, 2005 at 05:22 UTC
    I think I goofed in my problem description, I didn't know what fork() was, I thought it was a general term to spawn a completely different program, not duplicate itself.

    In fact the system() command might work for me if I am able to interrupt it. I just want to spawn a system process but be able to kill it upon an interrupt signal from the user.

    Sorry for my ignorance. And thanks for your reply.

      You can just use the control-c(SIG-INT) to interrupt the current system, and move to the next in the list. Try:
      #!/usr/bin/perl $rc = system('top'); $rc = system('date; sleep 10'); $rc = system('top'); $rc = system('date; sleep 10'); $rc = system('top');

      I'm not really a human, but I play one on earth. flash japh