Add Book to My BookshelfPurchase This Book Online

Chapter 11 - Processes

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Timing Process Execution
If you want to be able to determine how much processor time a process has consumed, use the times function. You may need this value for accounting purposes, or to attempt to optimize a program. In UNIX, processor time is divided into two parts, user time and system time. User time is the amount of time the processor spends executing in user mode; that is, time spent executing the parts of the program written by the user such as loops and local functions. System time is the amount of time the processor spends executing operating system code on the user's behalf; that is, time spent in system calls such as read and write.
The times function is defined as:
    #include <sys/times.h>
    #include <limits.h>
    clock_t times(struct tms *buffer);
The struct tms structure is defined as follows:
    struct tms {
        clock_t    tms_utime;
        clock_t    tms_stime;
        clock_t    tms_cutime;
        clock_t    tms_cstime;
    }
The information reported by times applies to the calling process and all of the terminated child processes for which it has called a wait function. (It is not possible to obtain information about processes that are still running.)
The tms_utime and tms_stime elements of the structure report the amount of user and system time, respectively, used by the calling process. The tms_cutime element represents the sum of the tms_utime and tms_cutime of the calling process' children (thus, a process inherits the times of its children). The tms_cstime element represents the sum of the tms_stime and tms_cstime of the calling process' children.
All times are reported in units called clock ticks. The value of a clock tick is defined by the CLK_TCK constant in the include file limits.h. To obtain a value in seconds, the element of interest in the structure should be divided by CLK_TCK.
On success, times returns the elapsed real time in clock ticks from some arbitrary point in the past (usually system boot time). This point does not change between calls to times, so by making two calls (say, before a call to fork and after a call to wait), it is possible to determine how long a process took to execute.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback