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

Greetings Monks, I know that this is a silly question, but we are pressed for time and I wanted to draw on your experience. So here are the details:

We are writing a test script as part of a porting effort. We need to perform the following:

Open a program and
1. read its STDOUT
2. calculate its system time
3. calculate its max memory
4. kill it if the script is Control-C'd

Here are the caveats: it has to be Unix/Win portable and can not use any non-core modules.

So, we use open(PROG,"pogname|") to open the program with a handle to its stdout. We install a signal-handler to deal with keyboard interupts. Fine. But how do we kill a program started by 'open' from the signal handler? Simply closing the filehandle is not enough.

To get the system time, we use 'times' as such:

($user,$system,$cuser,$csystem) = times;

It seems to me that the program started with 'open' should be our child, but $cuser & $csystem are zero. Does this only work when you have forked a child?

As for the max memory issue, I could scan the ps table, but this is not portable. I am at a loss here.

I know that this is actually several questions. I am also painfully aware that there is no easy answer, but I will be happy to consider any advise you might have to offer.

"Never take yourself too seriously, because everyone knows that fat birds dont fly" -FLC

Replies are listed 'Best First'.
Re: Portable Process Control
by ikegami (Patriarch) on Aug 24, 2005 at 17:45 UTC

    Calculating a child/program's memory usage is not possible when using only core modules.

    Calculating a child/program's memory usage is not portable. You'd have to branch based on $^O.

    I'm not sure about the others.

Re: Portable Process Control
by MidLifeXis (Monsignor) on Aug 24, 2005 at 17:39 UTC
    But how do we kill a program started by 'open' from the signal handler?

    From perldoc -f open

    Open returns nonzero upon success, the undefined value otherwise. If the "open" involved a pipe, the return value happens to be the pid of the subprocess.

    --MidLifeXis

Re: Portable Process Control
by sk (Curate) on Aug 24, 2005 at 17:54 UTC
    This progam starts another prog and reads from its STDOUT. The other prog just prints hi there in an infinite loop.

    #!/usr/bin/perl -w open (PIPE,"printsth |") or die $!; while(<PIPE>) {print; last if $i++ >=1000; } close(PIPE) or die "Quit with exit status: $? message: $!" ;
    #prog printsth #!/usr/bin/perl -w print "hi there", $/ while(1);

    When i run my first scirpt it (close) terminates the pipe but fails for some reason. However i don't see my program opened through the script running in the system so it has been forced to terminate somehow

    hi there .... .... hi there hi there Quit with exit status: 13 message: at termpipe line 7.