Child & Parent processes with getrusage()

Hey guys, I am working on an assignment that requires me to use getrusage(), but I can't seem to figure out how to use it. This is the assignment description:

The child process computes the sum of the first n prime numbers and prints the result. Then prints its process identification and the amount of CPU time it has used and finally terminates. The parent catches child's termination and performs some other computations. The parent prints the child's ID and CPU time and its own ID and CPU time and the first n prime numbers and terminates.


I have the entire program working correctly, except for printing the CPU time since I cannot grasp how to use getrusage(). Can anyone help explaining this function maybe? Here is my code: http://pastebin.com/bd19GX63 (compiled with g++, ran like a.out # .)

And here is the link I've been trying to use for guidance:
http://pubs.opengroup.org/onlinepubs/009604599/functions/getrusage.html
I saw that link too when I was searching, but I'm still confused as to how to implement the code :/
Something like this:

1
2
3
4
5
6
7
8
long long total_cpu_time_consumed_in_usecs( bool for_completed_child_processes = false )
{
    rusage usage ;
    getrusage( for_completed_child_processes ? RUSAGE_CHILDREN : RUSAGE_SELF, &usage ) ;
    enum : long long { USECS_PER_SEC = 1000000 } ;
    return ru.ru_utime * USECS_PER_SEC + ru.ru_utime // user
            + ru.ru_stime * USECS_PER_SEC + ru.ru_stime ; // system
}
So would I call this function when I want to print it during the cout statements? What would I pass in the parameter?
Topic archived. No new replies allowed.