Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <sys/times.h>
clock_t times(struct tms *buffer);
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.
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 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.
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.
⁄* 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