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