linuxgccfork

sys/user time & childs sys/user time in linux


Based on example 8.31 of APUE, suppose the following code. This example shows user/sys time of a process and also user/sys times of child processes for each command (passed as argvs)

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/times.h>

void do_cmd(int, const char *);
void pr_times(clock_t, clock_t, struct tms *, struct tms *);

int main (int argc, char *argv[]) {
        long clktck = 0;

        clktck=sysconf(_SC_CLK_TCK);

        for (int i=1; i<argc; i++)
                do_cmd(clktck, argv[i]);

        exit (0);
}

void do_cmd(int clktck, const char *cmd) {
        struct tms      tmsstart, tmsend;
        clock_t         start, end;

        start=times(&tmsstart);
        system(cmd);
        end = times(&tmsend);

        printf("command:%s\n", cmd);
        pr_times(clktck, end-start, &tmsstart, &tmsend);
        printf("\n");

}

void pr_times(clock_t clktck, clock_t real, struct tms *start, struct tms *end) {
        printf("real:%7.2f\n",  real/(double)clktck);
        printf("user:%7.2f\n",  (end->tms_utime - start->tms_utime) / (double)clktck); // this line
        printf("sys:%7.2f\n",   (end->tms_stime - start->tms_stime) / (double)clktck);  // and this line
        printf("cuser:%7.2f\n", (end->tms_cutime- start->tms_cutime)/ (double)clktck);
        printf("csys:%7.2f\n",  (end->tms_cstime- start->tms_cstime)/ (double)clktck);
}

My question is, by this scheme of command execution, is it possible to have user/sys time more than zero (and how) or always these two values is zero (becuase executed as forked(child process) ) ?

I execute the program like following

./a.out "sleep 10" "find / -name a* 1>/dev/null 2>&1" 'bash -c "for ((i=0;i<1000000;i++)); do :; done;" '

Solution

  • By this type of command execution, I think always user time and sys time will be zero. Becuase it is executed as child process