02-20-07 06:17 PM
"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>
[ Post a follow-up to this message ]
|