[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Base Operating System and Extensions, Volume 2

times Subroutine

Purpose

Gets process and waited-for child process times

Syntax

#include <sys/times.h>

clock_t times (buffer)
struct tms *buffer;

Description

The times subroutine fills the tms structure pointed to by buffer with time-accounting information. The tms structure is defined in <sys/times.h>.

All times are measured in terms of the number of clock ticks used.

The times of a terminated child process is included in the tms_cutime and tms_cstime elements of the parent when the wait or waitpid subroutine returns the process ID of the terminated child. If a child process has not waited for its children, their times are not included in its times.

Applications should use sysconf(_SC_CLK_TCK) to determine the number of clock ticks per second as it may vary from system to system.

Parameters

*buffer Points to the tms structure.

Return Values

Upon successful completion, the times subroutine returns the elapsed real time, in clock ticks, since an arbitrary point in the past (for example, system startup time). This point does not change from one invocation of the times subroutine within the process to another. The return value may overflow the possible range of type clock_t. If the times subroutine fails, (clock_t)-1 is returned, and the errno global variable is set to indicate the error.

Examples

Timing a Database Lookup

The following example defines two functions, start_clock and end_clock, that are used to time a lookup. It also defines variables of type clock_t and tms to measure the duration of transactions. The start_clock function saves the beginning times given by the times subroutine. The end_clock function gets the ending times and prints the difference between the two times.

#include <sys/times.h>
#include <stdio.h>
...
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(&st_cpu);
}

/* 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(&en_cpu);

        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));
 }

Related Information

sysconf Subroutine and wait, waitpid, wait3, or wait364 Subroutine

The gettimer, settimer, restimer, stime, or time Subroutine, getinterval, incinterval, absinterval, resinc, resabs, alarm, ualarm, getitimer or setitimer Subroutine, exec: execl, execle, execlp, execv, execve, execvp, or exect Subroutine, and fork, f_fork, or vfork Subroutine in AIX 5L Version 5.2 Technical Reference: Base Operating System and Extensions Volume 1.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]