1TIMES(3P) POSIX Programmer's Manual TIMES(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 times — get process and waited-for child process times
14
16 #include <sys/times.h>
17
18 clock_t times(struct tms *buffer);
19
21 The times() function shall fill the tms structure pointed to by buffer
22 with time-accounting information. The tms structure is defined in
23 <sys/times.h>.
24
25 All times are measured in terms of the number of clock ticks used.
26
27 The times of a terminated child process shall be included in the
28 tms_cutime and tms_cstime elements of the parent when wait(), waitid(),
29 or waitpid() returns the process ID of this terminated child. If a
30 child process has not waited for its children, their times shall not be
31 included in its times.
32
33 * The tms_utime structure member is the CPU time charged for the exe‐
34 cution of user instructions of the calling process.
35
36 * The tms_stime structure member is the CPU time charged for execu‐
37 tion by the system on behalf of the calling process.
38
39 * The tms_cutime structure member is the sum of the tms_utime and
40 tms_cutime times of the child processes.
41
42 * The tms_cstime structure member is the sum of the tms_stime and
43 tms_cstime times of the child processes.
44
46 Upon successful completion, times() shall return the elapsed real time,
47 in clock ticks, since an arbitrary point in the past (for example, sys‐
48 tem start-up time). This point does not change from one invocation of
49 times() within the process to another. The return value may overflow
50 the possible range of type clock_t. If times() fails, (clock_t)−1
51 shall be returned and errno set to indicate the error.
52
54 No errors are defined.
55
56 The following sections are informative.
57
59 Timing a Database Lookup
60 The following example defines two functions, start_clock() and
61 end_clock(), that are used to time a lookup. It also defines variables
62 of type clock_t and tms to measure the duration of transactions. The
63 start_clock() function saves the beginning times given by the times()
64 function. The end_clock() function gets the ending times and prints the
65 difference between the two times.
66
67 #include <sys/times.h>
68 #include <stdio.h>
69 ...
70 void start_clock(void);
71 void end_clock(char *msg);
72 ...
73 static clock_t st_time;
74 static clock_t en_time;
75 static struct tms st_cpu;
76 static struct tms en_cpu;
77 ...
78 void
79 start_clock()
80 {
81 st_time = times(&st_cpu);
82 }
83
84 /* This example assumes that the result of each subtraction
85 is within the range of values that can be represented in
86 an integer type. */
87 void
88 end_clock(char *msg)
89 {
90 en_time = times(&en_cpu);
91
92 fputs(msg,stdout);
93 printf("Real Time: %jd, User Time %jd, System Time %jd\n",
94 (intmax_t)(en_time - st_time),
95 (intmax_t)(en_cpu.tms_utime - st_cpu.tms_utime),
96 (intmax_t)(en_cpu.tms_stime - st_cpu.tms_stime));
97 }
98
100 Applications should use sysconf(_SC_CLK_TCK) to determine the number of
101 clock ticks per second as it may vary from system to system.
102
104 The accuracy of the times reported is intentionally left unspecified to
105 allow implementations flexibility in design, from uniprocessor to
106 multi-processor networks.
107
108 The inclusion of times of child processes is recursive, so that a par‐
109 ent process may collect the total times of all of its descendants. But
110 the times of a child are only added to those of its parent when its
111 parent successfully waits on the child. Thus, it is not guaranteed that
112 a parent process can always see the total times of all its descendants;
113 see also the discussion of the term ``realtime'' in alarm().
114
115 If the type clock_t is defined to be a signed 32-bit integer, it over‐
116 flows in somewhat more than a year if there are 60 clock ticks per sec‐
117 ond, or less than a year if there are 100. There are individual systems
118 that run continuously for longer than that. This volume of POSIX.1‐2008
119 permits an implementation to make the reference point for the returned
120 value be the start-up time of the process, rather than system start-up
121 time.
122
123 The term ``charge'' in this context has nothing to do with billing for
124 services. The operating system accounts for time used in this way. That
125 information must be correct, regardless of how that information is
126 used.
127
129 None.
130
132 alarm(), exec, fork(), sysconf(), time(), wait(), waitid()
133
134 The Base Definitions volume of POSIX.1‐2008, <sys_times.h>
135
137 Portions of this text are reprinted and reproduced in electronic form
138 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
139 -- Portable Operating System Interface (POSIX), The Open Group Base
140 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
141 cal and Electronics Engineers, Inc and The Open Group. (This is
142 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
143 event of any discrepancy between this version and the original IEEE and
144 The Open Group Standard, the original IEEE and The Open Group Standard
145 is the referee document. The original Standard can be obtained online
146 at http://www.unix.org/online.html .
147
148 Any typographical or formatting errors that appear in this page are
149 most likely to have been introduced during the conversion of the source
150 files to man page format. To report such errors, see https://www.ker‐
151 nel.org/doc/man-pages/reporting_bugs.html .
152
153
154
155IEEE/The Open Group 2013 TIMES(3P)