The times() function and child cpu time misbehavior

A

A. Lewenberg

The following code forks creating a child process that does some
CPU-intensive task. The parent process loops every 1 second and
displays the results of a call to the times() function. What is
puzzling is that the last 2 elements of the times() function call
never get larger than zero even though they are supposed
to reflect cpu time usage of all child processes.

Is the times() function broken on this platform or am I
misunderstanding its use?

Thanks for any help or pointers.

Here is the output (note that the child process shows that it has used
more than 5 seconds of CPU but that the parent never thinks its
children have used any):

Parent times() result: 0.01 : 0 : 0 : 0
Parent times() result: 0.01 : 0 : 0 : 0
Parent times() result: 0.01 : 0 : 0 : 0
Parent times() result: 0.01 : 0 : 0 : 0
Parent times() result: 0.01 : 0 : 0 : 0
Parent times() result: 0.01 : 0 : 0 : 0
Child times() result: 5.61 : 0.01 : 0 : 0
Parent times() result: 0.01 : 0 : 0 : 0
Parent times() result: 0.01 : 0 : 0 : 0
(Ctrl-C)


Here is the code:

$pid = fork() ;
if ($pid)
{

while (1)
{
my @xx = times() ;
print "Parent times() result: " ;
print $xx[0] . " : " . $xx[1] . " : " . $xx[2] . " : " . $xx[3] .
"\n" ;
sleep 1 ;
}
}
else
{
my ($i, $x) ;
for ($i=1; $i<= 9999999; ++$i)
{
$x = sin($i) ;
}
my @xx = times() ;
print "Child times() result: " ;
print $xx[0] . " : " . $xx[1] . " : " . $xx[2] . " : " . $xx[3] .
"\n" ;
exit 0 ;
}

In case it matters, here is the information on the perl version and
platform:

perl --version --> This is perl, v5.6.1 built for i386-linux
cat /etc/redhat-release --> Red Hat Linux release 7.2 (Enigma)
uname -a --> Linux cooper.library.uiuc.edu 2.4.20-27.7smp #1 SMP Thu
Dec 11 14:50:55 EST 2003 i686 unknown

(Note that I tried this on another 386 Linux platform running perl 5.8
with the same results.)
 
A

Anno Siegel

A. Lewenberg said:
The following code forks creating a child process that does some
CPU-intensive task. The parent process loops every 1 second and
displays the results of a call to the times() function. What is
puzzling is that the last 2 elements of the times() function call
never get larger than zero even though they are supposed
to reflect cpu time usage of all child processes.

Is the times() function broken on this platform or am I
misunderstanding its use?

I believe the latter. The times() function includes only the cpu times
of finished children you have wait()'ed for. I seem to understand you
want to watch the kid's cpu time grow. You can't do that from the
parent, not through times().

If I'm misunderstanding your question, please ignore me. This is just
a quick reply, without looking at the code.

Anno
 
A

A. Lewenberg

I believe the latter. The times() function includes only the cpu times
of finished children you have wait()'ed for. I seem to understand you
want to watch the kid's cpu time grow. You can't do that from the
parent, not through times().

If I'm misunderstanding your question, please ignore me. This is just
a quick reply, without looking at the code.

Anno

Yes, I believe you are correct. This is even hinted at in the man page
for times. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top