Timing a spawned process

Timing a spawned process

am 06.05.2008 12:35:58 von Raymond Wan

Hi all,

I'm having a problem with spawning a long-running process and timing it
(i.e., with /usr/bin/time, for example). A while back, I posted a
problem with spawning a process which I did not want to know if/when it
comes back. Based on many helpful replies, I am now using:

-----
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

$SIG{CHLD} = 'IGNORE';

defined (my $kid = fork) or die "Cannot fork: $!\n";
if ($kid) {
## Parent has finished
}
else {
setsid () or die "Can't start a new session: $!";

open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";

exec $cmd or die "Cannot execute exec: $!";
}
-----

I am interested in timing $cmd. If I just wanted the real time, I could
just record the time when the process starts and when it stops.
However, what I'm more interested in is the user time. I guess [and I
could be wrong...] that the easiest way to do it is to have
/usr/bin/time at the front of $cmd. When I put that, I get an error like:

/usr/bin/time: error waiting for child process: No child processes

$cmd is actually a C executable. So, I changed it to a bash script that
runs the executable and in the script, I put /usr/bin/time again. I'm
not sure if this was a silly thing to try, but it didn't work and I'm
not sure if it is because of my Perl code above.

It seems like I'm either misunderstanding something or just doing it
wrong, but I don't quite know what. Is there anyway for me to get the
user time of $cmd?

Thank you in advance!

Ray