timergetrusagerusage

rusage() measured in seconds or millseconds?


Hi I'm trying to get the system time and user time of applications being forked by a shell. I'm just not sure what kind of time I'm getting, seconds? milliseconds? Does anybody know?

printf("System time: %ld.%06ld sec\n",usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
printf("User time:   %ld.%06ld sec\n\n",usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);

This is a sample of the results I get. I have the time as seconds, but I'm not really sure

#   PID Name    System Time User Time                 
*****************************************************
1   12420   firefox 0.148009 sec    0.816051 sec
2   12429   gimp    0.444027 sec    2.512157 sec
3   12442   clear   0.000000 sec    0.000000 sec

Solution

  • You get neither seconds nor milliseconds nor microseconds - you get struct timeval. This is a structure consisting of two fields: tv_sec gives full seconds, and tv_usec gives microseconds; the latter value being less than one million (so it is always a fraction of a second).

    So the resolution that this API supports is microseconds; the unit that you should use in your print statement is seconds - so you do it correctly already.