Next: Exercises Up: UNIX and C Previous: Receiving signals - signal()

Times Up!!

The last topic we will at in this course is how we can access the clock time with UNIX system calls.

There are many more time functions - see man pages and handouts.

Uses of time functions include:

time_ttime(time_t*tloc) - returns the time since 00:00:00 GMT, Jan. 1, 1970, measured in seconds.

If tloc is not NULL, the return value is also stored in the location to which tloc points.

time() returns the value of time on success.

On failure, it returns (time_t) -1. time_t is typedefed to a long (int) in <sys/types.h> and <sys/time.h> header files.

int ftime(struct timeb *tp) - fills in a structure pointed to by tp, as defined in <sys/timeb.h>:

The structure contains the time since the epoch in seconds, up to 1000 milliseconds of more precise interval, the local time zone (measured in minutes of time westward from Greenwich), and a flag that, if nonzero, indicates that Day light Saving time applies locally during the appropriate part of the year.

On success, ftime() returns no useful value. On failure, it returns -1.

Two other functions defined etc. in #include <time.h>

char *ctime(time_t*clock), char *asctime(struct tm *tm)

ctime() converts a long integer, pointed to by clock, to a 26-character string of the form produced by asctime(). It first breaks down clock to a tm structure by calling localtime(), and then calls asctime() to convert that tm structure to a string.

asctime() converts a time value contained in a tm structure to a 26-character string of the form:

Sun Sep 16 01:03:52 1973

asctime() returns a pointer to the string.

Example 1: Time (in seconds) to perform some computation:

Example 2: Set a random number seed

lrand48() returns non-negative long integers uniformly distributed over the interval (0, 2**31).

A similar function drand48() returns double precision numbers in the range [0.0,1.0).

srand48() sets the seed for these random number generators. It is important to have different seeds when we call the functions otherwise the same set of pseudo-random numbers will generated. time() always provides a unique seed.



Next: Exercises Up: UNIX and C Previous: Receiving signals - signal()


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994