times() — Get process and child process times
Standards
| Standards / Extensions | C or C++ | Dependencies |
|---|---|---|
POSIX.1
XPG4 XPG4.2 Single UNIX Specification, Version 3 |
both |
Format
#define _POSIX_SOURCE
#include <sys/times.h>
clock_t times(struct tms *buffer);General description
struct tms*buffer- Points to a memory location where times() can store a structure of information describing processor time used by the current process and other related processes.
tms structure,
which has the following elements: clock_t tms_utime- Amount of processor time used by instructions in the calling process.
Under z/OS® UNIX, this does not include processor time spent running in the kernel. It does include any processor time accumulated for the address space before it became a z/OS UNIX process.
clock_t tms_stime- Amount of processor time used by the system.
Under z/OS UNIX, this value represents kernel busy time running on behalf of the calling process. It does not include processor time performing other MVS™ system functions on behalf of the process.
clock_t tms_cutime- The sum of tms_utime and tms_cutime values for all waited-for child processes which have terminated.
clock_t tms_cstime- The sum of tms_stime and tms_cstime values for all terminated child processes of the calling process.
clock_t is an integral type
determined in the time.h header file. It measures times in terms of clock
ticks. The
number of clock ticks in a second (for your installation) can be found
in sysconf(_SC_CLK_TCK).
Times for a terminated child can be determined once wait() or waitpid() have reported the child's termination.
Pthreads can not be separately clocked by the times() function because they do not run in a separate process like forked children do.
Returned value
If successful, times() returns
a value giving the elapsed time since the process was last invoked
(for example, at system startup). If this time value cannot be determined,
times() returns (clock_t)-1.
- Error Code
- Description
- ERANGE
- An overflow having occurred computing time values.
Example
/* CELEBT12
This example provides the amount of processor time
used by instructions and the system for the parent and child
processes.
*/
#define _POSIX_SOURCE
#include <sys/times.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
main() {
int status;
long i, j;
struct tms t;
clock_t dub;
int tics_per_second;
tics_per_second = sysconf(_SC_CLK_TCK);
if (fork() == 0) {
for (i=0, j=0; i<1000000; i++)
j += i;
exit(0);
}
if (wait(&status) == -1)
perror("wait() error");
else if (!WIFEXITED(status))
puts("Child did not exit successfully");
else if ((dub = times(&t)) == -1)
perror("times() error");
else {
printf("process was dubbed %f seconds ago.\n\n",
((double) dub)/tics_per_second);
printf(" utime stime\n");
printf("parent: %f %f\n",
((double) t.tms_utime)/tics_per_second,
((double) t.tms_stime)/tics_per_second);
printf("child: %f %f\n",
((double) t.tms_cutime)/tics_per_second,
((double) t.tms_cstime)/tics_per_second);
}
}
process was dubbed 1.600000 seconds ago.
utime stime
parent: 0.000000 0.020000
child: 0.320000 0.000000