getrusage on child process

2.1k views Asked by At

I am working on a C program in which I have to fork() a process and use the getrusage() function for printing the user time and the kernel time of the child process.

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>

int main(int argc, const char * argv[]) {

    struct rusage child1_usage, child2_usage;
    struct timeval child1_stime, child2_stime, child1_utime, child2_utime;
    pid_t child1_pid, child2_pid;



    switch (child1_pid = fork()) {

        case -1:
            perror("Fork failed!\n");
            break;

        case 0:

            printf("Hi I am the first child!!\nGive me one number and I multiply it by 4:\n");
            int num1;
            scanf("%d", &num1);
            printf("%d\n", num1*4);

            switch (child2_pid = fork()) {

                case -1:
                    perror("Second Fork failed!\n");
                    break;

                case 0:
                    printf("Hi I am the grand child!!\nGive me one number and I multiply it by 3:\n");
                    int num2;
                    scanf("%d", &num2);
                    printf("%d\n", num2*4);
                    getrusage(RUSAGE_SELF, &child2_usage);
                    child2_utime = child2_usage.ru_utime;
                    child2_stime = child2_usage.ru_stime;
                    printf("Time in user mode: %lld\n", (long long) child2_utime.tv_sec);
                    printf("Time in kernel mode: %lld\n", (long long) child2_stime.tv_sec);

                default:
                    sleep(10);
                    break;

            }

            getrusage(RUSAGE_SELF, &child1_usage);
            child1_utime = child1_usage.ru_utime;
            child1_stime = child1_usage.ru_stime;
            printf("Time in user mode: %lld\n", (long long) child1_utime.tv_sec);
            printf("Time in kernel mode: %lld\n", (long long) child1_stime.tv_sec);

        default:
            sleep(10);
            break;
    }

    return 0;
}

And my output are all zeros... Probably I have misunderstood how getrusage() works. Otherwise I can't see where is the error.

1

There are 1 answers

0
Nickolay On BEST ANSWER

You're only printing out timeval's tv_sec, which contains the number of whole seconds. Since the program doesn't do anything computationally intensive, its CPU time (both user and system) is much less than 1 second - around 0.00001, so you'll only get non-zero values in the microseconds (tv_usec) field.

https://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html

The struct timeval structure represents an elapsed time. It is declared in sys/time.h and has the following members:

time_t tv_sec - This represents the number of whole seconds of elapsed time.

long int tv_usec - This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds. It is always less than one million.