Winding Up the UNIX Clock

Originally published in Computer Jagat magazine in January ,1995

Dr. Khan Monzoor-E-Khoda  and Professor D.J. Evans


Due to computer word length incompatibility the UNIX clock subroutine can give incorrect times for computations exceeding 2147 sees.

In this short note a reliable UNIX clock subroutine is proposed that will return the correct time for longer running processes.

KEYWORDS : C function clock, UNIX, wrap around.

online pharmacy cialis


The C function clock (see man clock, appendix 1) in the UNIX operating system usually returns the execution time of a process since its last call. However the value returned by clock( ) is defined in microseconds for compatibility with systems that have CPU clocks with much higher resolution.’ Because of this, the value returned will wrap around after accumulating only 2147 seconds of CPU time. Thus the clock( ) is only reliable up to approximately 36 minutes. To rectify this situation, an optimized subroutine is proposed in this note for an efficient reliable clock to read the CPU time used. The reliability of this new clock subroutine is also observed by comparison with the usual clock () subroutine.


The new modified clock subroutine is used to read the computer’s clock to provide the amount of processor time in seconds used by a program or algorithm when it is being executed. In fact, it returns a single precision float number representing the number of seconds that process takes. Notice that the time reported is the sum of the total user times and the total system times of the calling process. Child process times are not considered as most of the users do not spawn any child process / es. When called from PASCAL or FORTRAN language, float is equivalent to data type ‘real’. An error is indicated when-1 is returned.

The structure of the subroutine depends totally on times (see man times) and is developed in the C language under the UNIX environment. Its resolution varies depending on the hardware and on the software configuration.


To measure the execution time of a full or portion of a program, call the modified clock subroutine at the beginning of the program segment and then call it once again at the end of the segment, and then subtract the two values. The standard calling statement is defined by,

X <— mod_clock()

Where <— denotes the usual assignment statement depending on the programming language being considered. The subroutine mod_clock() must be declared as an external subroutine at the beginning of the program which calls it.

The corresponding function declaration

extern float mod_clock();

function mod_clock: real; external C;

External mod_clock

need to be used when the calling program is written

using the C, PASCAL and FORTRAN language respectively.


# include <sys/types.h>

# include <sys/times.h>

# define HP_HZ    100 /”For AT &T HP UNIX*/

# define SUN_HZ 60  /*FOR MOST OTHER UNIX’/ float mod_clock()


struct tms zl; long 1, error_signal; float d,x;

error_signal = times (&zl); if (error_signal !=-l)|

1 = zl.tms_utim + zl.tms_stime;

x = HP_HZ (orSUN_HZ)

d = (float)l/x



d = -1.0;


We have used a 32-bit arithmetic, floating point, HP 9000/ 870 computer system at Loughborough University of Technology to compare the efficiency and reliability of the proposed new modified clock with those of the original clock( ), (see Appendix 2).

The new modified clock subroutine is found to be considerably more reliable than the original clock subroutine when the execution time exceeds 2147 seconds (approx. 36 minutes). In fact both clock subroutines performed equivalently up to 2147 seconds.




clock-report CPU time used


# include <time.h>

clock_t clock(void);


clock returns the amount of CPU time (in microseconds) used since the first call to clock. The time reported is the sum of the user and system times of the calling process and its terminated child processes for which it has executed wait(2) or system OS). To determine the time in seconds, the value returned by the clock function should be devided by the value of the macro CLOCKS_PER_SEC.

The resolution of the clock varies, depending on the hardware and on the software configuration.

If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)-1.


The value returned by clock is denned in microseconds for compatibility with systems that have CPU clocks with much higher resolution. Because of this, the value returned will wrap around after accumulating only 2147 seconds of CPU time (about 36 minutes).


series 300 / 400

the   clock   resolution   is   20 milliseconds.

Series 700 / 800

the default clock resolution is 10 milliseconds.


times(2), wait(2), system(3S).


clock: SVID2, XPG2, XPG3, ANSI C.


Table 1: Comparison of original clock and modified clock

Clock() mod_clock()
0.000 0.050
0.450 0.500
0.900 0.950
1.450 1.500
2.000 2.050
2.500 2.550
2.900 2.950
4.000 4.050
9.000 0.050
127.000 127.500
1937.00 1937.550
1951.00 1951.050
2062.000 2062.050
2147.010 2147.060
2147.400 2147.450
2147.460 2147.510
2147.480 2147.530
-2147.467 2147.550
-2147.007 2148.010
-2144.967 2150.050
-2109.867 2185.150

* Present address: Analyst, Management Information System, The Hospitality Group Ltd., 980 Young Street, Suit 200, Ontario, L5A 1H4, Canada (

* * Present address: Director, Parallel Algorithms Research Centre, Dept. of Computer Studies, University of Technology, Loughborough, Leicestershire, LEI 1 3TU, U.K.

Subscribe / Share

Article by nuzhat akhter

Authors bio is coming up shortly.
nuzhat akhter tagged this post with: , , , Read 913 articles by