Home > Archive > Unix Programming > February 2007 > getrusage call and results...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
getrusage call and results...
|
|
| Solomon_Man 2007-02-19, 7:05 pm |
| All,
I am working on a graduate project that I am scratching my head at one
of the results I am getting and I am not sure why.....Here is some
code....
pid = fork();
if (pid == 0) /* child */
{ //Do some work Mathematical work
..........
//Print 1
error = getrusage(RUSAGE_SELF,&TimeVal); //Get Usage Info
cout<<"User:"<<TimeVal.ru_utime.tv_sec<<"
"<<TimeVal.ru_utime.tv_usec<<endl;
cout<<"Sys:"<<TimeVal.ru_stime.tv_sec<<"
"<<TimeVal.ru_stime.tv_usec<<endl;
cout<<"Child Process Exitting "<<endl;
exit(0); //End Child Process
}
else /* parent */
{
signal (SIGCHLD, parentProcessSignalCatcher);
while (1){
}
}
void parentProcessSignalCatcher(int x)
{
struct rusage TimeVal;
int error = 0;
....
//Print 2
error = getrusage(RUSAGE_SELF,&TimeVal); //Get Parent Usage Info
cout<<"PUser:"<<TimeVal.ru_utime.tv_sec<<"
"<<TimeVal.ru_utime.tv_usec<<endl;
cout<<"PSys:"<<TimeVal.ru_stime.tv_sec<<"
"<<TimeVal.ru_stime.tv_usec<<endl;
//Print 3
error = getrusage(RUSAGE_CHILDREN,&TimeVal); //Get Children Usage
Info
cout<<"CUser:"<<TimeVal.ru_utime.tv_sec<<"
"<<TimeVal.ru_utime.tv_usec<<endl;
cout<<"CSys:"<<TimeVal.ru_stime.tv_sec<<"
"<<TimeVal.ru_stime.tv_usec<<endl;
exit(0);
}
So I get results that seem reasonable for the child output (Print 1)
and I get values that are reasonable for the parent section Parent
values (Print 2) but for the parent section for child usage (Print 3)
I get zeros. I am not sure on why.
Any one have any ideas?
Thanks in advance,
Chris
| |
| Solomon_Man 2007-02-19, 7:05 pm |
| On Feb 19, 1:31 pm, "Solomon_Man" <cmgra...@gmail.com> wrote:
> All,
> I am working on a graduate project that I am scratching my head at one
> of the results I am getting and I am not sure why.....Here is some
> code....
>
> pid = fork();
> if (pid == 0) /* child */
> { //Do some work Mathematical work
> .........
> //Print 1
> error = getrusage(RUSAGE_SELF,&TimeVal); //Get Usage Info
> cout<<"User:"<<TimeVal.ru_utime.tv_sec<<"
> "<<TimeVal.ru_utime.tv_usec<<endl;
> cout<<"Sys:"<<TimeVal.ru_stime.tv_sec<<"
> "<<TimeVal.ru_stime.tv_usec<<endl;
> cout<<"Child Process Exitting "<<endl;
> exit(0); //End Child Process
> }
> else /* parent */
> {
> signal (SIGCHLD, parentProcessSignalCatcher);
> while (1){
> }
> }
>
> void parentProcessSignalCatcher(int x)
> {
> struct rusage TimeVal;
> int error = 0;
> ....
> //Print 2
> error = getrusage(RUSAGE_SELF,&TimeVal); //Get Parent Usage Info
> cout<<"PUser:"<<TimeVal.ru_utime.tv_sec<<"
> "<<TimeVal.ru_utime.tv_usec<<endl;
> cout<<"PSys:"<<TimeVal.ru_stime.tv_sec<<"
> "<<TimeVal.ru_stime.tv_usec<<endl;
> //Print 3
> error = getrusage(RUSAGE_CHILDREN,&TimeVal); //Get Children Usage
> Info
> cout<<"CUser:"<<TimeVal.ru_utime.tv_sec<<"
> "<<TimeVal.ru_utime.tv_usec<<endl;
> cout<<"CSys:"<<TimeVal.ru_stime.tv_sec<<"
> "<<TimeVal.ru_stime.tv_usec<<endl;
> exit(0);
>
> }
>
> So I get results that seem reasonable for the child output (Print 1)
> and I get values that are reasonable for the parent section Parent
> values (Print 2) but for the parent section for child usage (Print 3)
> I get zeros. I am not sure on why.
>
> Any one have any ideas?
>
> Thanks in advance,
> Chris
Maybe I should add I would expect to see a number similar to the Print
1 Section.
Am I wrong on thinking this?
Thanks,
Chris
| |
| Bin Chen 2007-02-19, 10:03 pm |
| On 2=D4=C220=C8=D5, =C9=CF=CE=E73=CA=B149=B7=D6, "Solomon_Man" <cmgra...@gm=
ail.com> wrote:
> On Feb 19, 1:31 pm, "Solomon_Man" <cmgra...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
> Maybe I should add I would expect to see a number similar to the Print
> 1 Section.
>
> Am I wrong on thinking this?
I don't run your program but I don't know why you think its wrong.
When the SIGCHILD is fired it indicates the child is exited. This time
you get the child usage it returns zero, whats the problem?
>
> Thanks,
> Chris
| |
| Solomon_Man 2007-02-19, 10:03 pm |
| On Feb 19, 8:25 pm, "Bin Chen" <binary.c...@gmail.com> wrote:
> On 2=E6=9C=8820=E6=97=A5, =E4=B8=8A=E5=8D=883=E6=97=B649=E5=88=86,
"Solom=
on_Man" <cmgra...@gmail.com> wrote:[color=darkred]
>
>
>
>
ge[color=darkred]
>
>
>
>
>
>
>
> I don't run your program but I don't know why you think its wrong.
> When the SIGCHILD is fired it indicates the child is exited. This time
> you get the child usage it returns zero, whats the problem?
>
>
>
All,
I was expecting the time that was originally returned from the child
process (Print 1) to match the time received in the parent process for
the child (Print 3). Am I wrong on this assumption and is it even
possible to get an exited child process usage from its parent process?
Thanks,
Chris
| |
| Geoff Clare 2007-02-20, 8:04 am |
| "Solomon_Man" <cmgray74@gmail.com> wrote, on Mon, 19 Feb 2007:
> pid = fork();
> if (pid == 0) /* child */
> { //Do some work Mathematical work
> .........
> //Print 1
> error = getrusage(RUSAGE_SELF,&TimeVal); //Get Usage Info
> cout<<"User:"<<TimeVal.ru_utime.tv_sec<<"
> "<<TimeVal.ru_utime.tv_usec<<endl;
> cout<<"Sys:"<<TimeVal.ru_stime.tv_sec<<"
> "<<TimeVal.ru_stime.tv_usec<<endl;
> cout<<"Child Process Exitting "<<endl;
> exit(0); //End Child Process
> }
> else /* parent */
> {
> signal (SIGCHLD, parentProcessSignalCatcher);
> while (1){
> }
> }
>
> void parentProcessSignalCatcher(int x)
> {
> struct rusage TimeVal;
> int error = 0;
> ....
> //Print 2
> error = getrusage(RUSAGE_SELF,&TimeVal); //Get Parent Usage Info
> cout<<"PUser:"<<TimeVal.ru_utime.tv_sec<<"
> "<<TimeVal.ru_utime.tv_usec<<endl;
> cout<<"PSys:"<<TimeVal.ru_stime.tv_sec<<"
> "<<TimeVal.ru_stime.tv_usec<<endl;
> //Print 3
> error = getrusage(RUSAGE_CHILDREN,&TimeVal); //Get Children Usage
> Info
> cout<<"CUser:"<<TimeVal.ru_utime.tv_sec<<"
> "<<TimeVal.ru_utime.tv_usec<<endl;
> cout<<"CSys:"<<TimeVal.ru_stime.tv_sec<<"
> "<<TimeVal.ru_stime.tv_usec<<endl;
> exit(0);
> }
>
>
> So I get results that seem reasonable for the child output (Print 1)
> and I get values that are reasonable for the parent section Parent
> values (Print 2) but for the parent section for child usage (Print 3)
> I get zeros. I am not sure on why.
The parent hasn't waited for the child (unless there was a waitpid()
or equivalent call in the part of parentProcessSignalCatcher() you
didn't show).
The description of getrusage() in SUSv3 says:
"If the value of the who argument is RUSAGE_CHILDREN, information
shall be returned about resources used by the terminated and
waited-for children of the current process. If the child is never
waited for (for example, if the parent has SA_NOCLDWAIT set or
sets SIGCHLD to SIG_IGN), the resource information for the child
process is discarded and not included in the resource information
provided by getrusage()."
--
Geoff Clare <netnews@gclare.org.uk>
|
|
|
|
|