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
12 times - get process and waited-for child process times
13
15 #include <sys/times.h>
16
17 clock_t times(struct tms *buffer);
18
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() or wait‐
29 pid() returns the process ID of this terminated child. If a child
30 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 execution
37 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 shall
51 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
65 the difference between the two times.
66
67
68 #include <sys/times.h>
69 #include <stdio.h>
70 ...
71 void start_clock(void);
72 void end_clock(char *msg);
73 ...
74 static clock_t st_time;
75 static clock_t en_time;
76 static struct tms st_cpu;
77 static struct tms en_cpu;
78 ...
79 void
80 start_clock()
81 {
82 st_time = times(&st_cpu);
83 }
84
85
86 /* This example assumes that the result of each subtraction
87 is within the range of values that can be represented in
88 an integer type. */
89 void
90 end_clock(char *msg)
91 {
92 en_time = times(&en_cpu);
93
94
95 fputs(msg,stdout);
96 printf("Real Time: %jd, User Time %jd, System Time %jd\n",
97 (intmax_t)(en_time - st_time),
98 (intmax_t)(en_cpu.tms_utime - st_cpu.tms_utime),
99 (intmax_t)(en_cpu.tms_stime - st_cpu.tms_stime));
100 }
101
103 Applications should use sysconf(_SC_CLK_TCK) to determine the number of
104 clock ticks per second as it may vary from system to system.
105
107 The accuracy of the times reported is intentionally left unspecified to
108 allow implementations flexibility in design, from uniprocessor to
109 multi-processor networks.
110
111 The inclusion of times of child processes is recursive, so that a par‐
112 ent process may collect the total times of all of its descendants. But
113 the times of a child are only added to those of its parent when its
114 parent successfully waits on the child. Thus, it is not guaranteed that
115 a parent process can always see the total times of all its descendants;
116 see also the discussion of the term ``realtime'' in alarm().
117
118 If the type clock_t is defined to be a signed 32-bit integer, it over‐
119 flows in somewhat more than a year if there are 60 clock ticks per sec‐
120 ond, or less than a year if there are 100. There are individual systems
121 that run continuously for longer than that. This volume of
122 IEEE Std 1003.1-2001 permits an implementation to make the reference
123 point for the returned value be the start-up time of the process,
124 rather than system start-up time.
125
126 The term ``charge'' in this context has nothing to do with billing for
127 services. The operating system accounts for time used in this way. That
128 information must be correct, regardless of how that information is
129 used.
130
132 None.
133
135 alarm(), exec(), fork(), sysconf(), time(), wait(), the Base Defini‐
136 tions volume of IEEE Std 1003.1-2001, <sys/times.h>
137
139 Portions of this text are reprinted and reproduced in electronic form
140 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
141 -- Portable Operating System Interface (POSIX), The Open Group Base
142 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
143 Electrical and Electronics Engineers, Inc and The Open Group. In the
144 event of any discrepancy between this version and the original IEEE and
145 The Open Group Standard, the original IEEE and The Open Group Standard
146 is the referee document. The original Standard can be obtained online
147 at http://www.opengroup.org/unix/online.html .
148
149
150
151IEEE/The Open Group 2003 TIMES(3P)