April 19, 2006
Coverage: [USP] Chapter 9
Back to Times
- asctime, ctime, gmtime, and
localtime are not thread-safe
- use asctime_r, ctime_r, gmtime_r, and
localtime_r from POSIX:TSF Thread Safe Extension instead
- struct timeval
- provides a finer level of granularity than seconds
- (seconds, microseconds) pair
- used by functions that support a timeout option such as select
- gettimeofday: returns system time in seconds and microseconds
since Epoch
- gettimeofdaytimimg.c: measures running
time of a function in microseconds
- realtime clocks
- a clock is a counter that increments at fixed intervals
called the clock resolution
- clockid_t type
- CLOCK_REALTIME: written by privileged users; read by anyone
- struct timespec: specifies time for POSIX:TMR clocks
and timers (and timeout values for POSIX thread functions supporting timeouts)
- (seconds, nanoseconds) pair
- functions for manipulating clocks
- clock_settime: sets the clock time
- clock_gettime: returns the clock time
- clock_getres: returns the clock resolution
- clockrealtimetiming.c: analog of gettimeofdaytimimg.c
using POSIX:TMR clocks
- clockrealtimetest.c: tests the resolution of
CLOCK_REALTIME by measuring the average of 20 changes in the
clock reading
- real time vs. virtual time
- real time is sometimes called elapsed time or
wall-clock time
- the time function measures real time
- virtual time is the amount of time that a process
resides in the running state
- times function populates a struct tms
struct with various time-accounting information
- cpufraction.c: estimates the total CPU time used
by a function in seconds and as a percentage of the real time used
- calls sysconf; recall showtimes program from Ch2?
- returns the number of clock ticks in a second
- does not include time for children
- may be inaccurate if a context switch happens during
function execution
- timechild.c: acts like the time shell command
- subtracts the process time from the total time
- works because the process will never have more than one child
- why don't we get the same result
when we pass it ls as we do with the time shell command?
- here we always get 0 clock ticks
References
[USP] K. A. Robbins and S. Robbins. UNIX Systems Programming: Concurrency,
Communication, and Threads. Prentice Hall, 2003