Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/times.html
2024-02-19 00:21:47 -05:00

195 lines
7.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>times</title>
</head>
<body bgcolor="white">
<basefont size="3"> <a name="times"></a> <a name="tag_03_779"></a><!-- times -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_779_01"></a>NAME</h4>
<blockquote>times - get process and waited-for child process times</blockquote>
<h4><a name="tag_03_779_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/sys/times.h.html">sys/times.h</a>&gt;<br>
<br>
clock_t times(struct tms *</tt><i>buffer</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_779_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>times</i>() function shall fill the <b>tms</b> structure pointed to by <i>buffer</i> with time-accounting information.
The <b>tms</b> structure is defined in <a href="../basedefs/sys/times.h.html"><i>&lt;sys/times.h&gt;</i></a>.</p>
<p>All times are measured in terms of the number of clock ticks used.</p>
<p>The times of a terminated child process shall be included in the <i>tms_cutime</i> and <i>tms_cstime</i> elements of the parent
when <a href="../functions/wait.html"><i>wait</i>()</a> or <a href="../functions/waitpid.html"><i>waitpid</i>()</a> 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.</p>
<ul>
<li>
<p>The <i>tms_utime</i> structure member is the CPU time charged for the execution of user instructions of the calling process.</p>
</li>
<li>
<p>The <i>tms_stime</i> structure member is the CPU time charged for execution by the system on behalf of the calling process.</p>
</li>
<li>
<p>The <i>tms_cutime</i> structure member is the sum of the <i>tms_utime</i> and <i>tms_cutime</i> times of the child
processes.</p>
</li>
<li>
<p>The <i>tms_cstime</i> structure member is the sum of the <i>tms_stime</i> and <i>tms_cstime</i> times of the child
processes.</p>
</li>
</ul>
</blockquote>
<h4><a name="tag_03_779_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>times</i>() shall return the elapsed real time, in clock ticks, since an arbitrary point in the
past (for example, system start-up time). This point does not change from one invocation of <i>times</i>() within the process to
another. The return value may overflow the possible range of type <b>clock_t</b>. If <i>times</i>() fails, (<b>clock_t</b>)-1 shall
be returned and <i>errno</i> set to indicate the error.</p>
</blockquote>
<h4><a name="tag_03_779_05"></a>ERRORS</h4>
<blockquote>
<p>No errors are defined.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_779_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_779_06_01"></a>Timing a Database Lookup</h5>
<p>The following example defines two functions, <i>start_clock</i>() and <i>end_clock</i>(), that are used to time a lookup. It
also defines variables of type <b>clock_t</b> and <b>tms</b> to measure the duration of transactions. The <i>start_clock</i>()
function saves the beginning times given by the <i>times</i>() function. The <i>end_clock</i>() function gets the ending times and
prints the difference between the two times.</p>
<pre>
<tt>#include &lt;sys/times.h&gt;
#include &lt;stdio.h&gt;
...
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(&amp;st_cpu);
}
<br>
/* 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(&amp;en_cpu);
<br>
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));
}
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_779_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>Applications should use <i>sysconf</i>(_SC_CLK_TCK) to determine the number of clock ticks per second as it may vary from system
to system.</p>
</blockquote>
<h4><a name="tag_03_779_08"></a>RATIONALE</h4>
<blockquote>
<p>The accuracy of the times reported is intentionally left unspecified to allow implementations flexibility in design, from
uniprocessor to multi-processor networks.</p>
<p>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 <a href="alarm.html"><i>alarm</i>()</a> .</p>
<p>If the type <b>clock_t</b> 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 IEEE&nbsp;Std&nbsp;1003.1-2001 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.</p>
<p>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.</p>
</blockquote>
<h4><a name="tag_03_779_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_779_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="alarm.html"><i>alarm</i>()</a> , <a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a> , <a href=
"fork.html"><i>fork</i>()</a> , <a href="sysconf.html"><i>sysconf</i>()</a> , <a href="time.html"><i>time</i>()</a> , <a href=
"wait.html"><i>wait</i>()</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/sys/times.h.html"><i>&lt;sys/times.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_779_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>