#include <sys/times.h> clock_t times(struct tms *buffer);
All times are measured in terms of the number of clock ticks used.
The times of a terminated child process shall be included in the tms_cutime and tms_cstime elements of the parent when wait(), waitid(), or waitpid() returns the process ID of this terminated child. If a child process has not waited for its children, their times shall not be included in its times.
The following sections are informative.
The following example defines two functions, start_clock() and end_clock(), that are used to time a lookup. It also defines variables of type clock_t and tms to measure the duration of transactions. The start_clock() function saves the beginning times given by the times() function. The end_clock() function gets the ending times and prints the difference between the two times.
#include <sys/times.h> #include <stdio.h> ... void start_clock(void); void end_clock(char *msg); ... static clock_t st_time; static clock_t en_time; static struct tms st_cpu; static struct tms en_cpu; ... void start_clock() { st_time = times(&st_cpu); } /* This example assumes that the result of each subtraction is within the range of values that can be represented in an integer type. */ void end_clock(char *msg) { en_time = times(&en_cpu); fputs(msg,stdout); printf("Real Time: %jd, User Time %jd, System Time %jd\n", (intmax_t)(en_time - st_time), (intmax_t)(en_cpu.tms_utime - st_cpu.tms_utime), (intmax_t)(en_cpu.tms_stime - st_cpu.tms_stime)); }
The inclusion of times of child processes is recursive, so that a parent process may collect the total times of all of its descendants. But the times of a child are only added to those of its parent when its parent successfully waits on the child. Thus, it is not guaranteed that a parent process can always see the total times of all its descendants; see also the discussion of the term ``realtime'' in alarm().
If the type clock_t is defined to be a signed 32-bit integer, it overflows in somewhat more than a year if there are 60 clock ticks per second, or less than a year if there are 100. There are individual systems that run continuously for longer than that. This volume of POSIX.1-2017 permits an implementation to make the reference point for the returned value be the start-up time of the process, rather than system start-up time.
The term ``charge'' in this context has nothing to do with billing for services. The operating system accounts for time used in this way. That information must be correct, regardless of how that information is used.
The Base Definitions volume of POSIX.1-2017, <sys_times.h>
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .