clock_gettime()
QNX SDP8.0C Library ReferenceAPIDeveloper
Get the current time of a clock
Synopsis:
#include <time.h>
int clock_gettime( clockid_t clock_id,
struct timespec * tp );
Arguments:
- clock_id
- The ID of the clock whose time you want to get; one of the following:
- CLOCK_REALTIME — the standard POSIX-defined clock. Timers based on this clock wake up the processor if it's in a power-saving mode.
- CLOCK_MONOTONIC — this clock always increases at a constant rate and can't be adjusted.
- CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID,
or a clock ID returned by
clock_getcpuclockid(),
pthread_getcpuclockid(),
or
ClockId(),
representing the amount of time the process or thread has spent running.
CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID are
special clock IDs that refer to the CPU time of the calling process and thread, respectively.
See
Monitoring execution times
in theUnderstanding the Microkernel's Concept of Time
chapter of the QNX OS Programmer's Guide.
- tp
- A pointer to a timespec structure where
clock_gettime() can store the time. For CPU-time clocks (if
clock_id is CLOCK_PROCESS_CPUTIME_ID or
CLOCK_THREAD_CPUTIME_ID, or if the clock ID is the result of a
ClockId() call), this time is the number of seconds and nanoseconds
of execution time (for a thread or all threads); for other clocks, this function sets
the members as follows:
- tv_sec — if clock_id is CLOCK_REALTIME, the number of seconds since 1970; if clock_id is CLOCK_MONOTONIC, the number of seconds since system boot.
- tv_nsec — the partial second beyond tv_sec, in nanoseconds.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The clock_gettime() function gets the current time of the clock specified by clock_id, and puts it into the buffer pointed to by tp.
Note:
In order to get the time for a CPU-time clock associated with another process,
your process must have the PROCMGR_AID_XPROCESS_QUERY ability enabled.
For more information, see
procmgr_ability().
Returns:
- 0
- Success.
- -1
- An error occurred (errno is set).
Errors:
- EFAULT
- A fault occurred trying to access the buffers provided.
- EINVAL
- Invalid clock_id.
- EPERM
- The calling process doesn't have the required permission; see procmgr_ability().
- ESRCH
- The process associated with this request doesn't exist.
Examples:
/*
* This program calculates the time required to
* execute the program specified as its first argument.
* The time is printed in seconds, on standard out.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#define BILLION 1000000000L;
int main( int argc, char** argv )
{
struct timespec start, stop;
double accum;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
return EXIT_FAILURE;
}
system( argv[1] );
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
perror( "clock gettime" );
return EXIT_FAILURE;
}
accum = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "%lf\n", accum );
return EXIT_SUCCESS;
}
Classification:
Safety: | |
---|---|
Cancellation point | No |
Signal handler | Yes |
Thread | Yes |
Page updated: