
時間
|
399
EFAULT
ts
是一個無效的指標。
EINVAL
clock_id
在此系統上是一個無效的時間來源。
下面的範例程式碼可以從四種標準的時間來源取得當前時間:
clockid_t clocks[] = {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID,
CLOCK_MONOTONIC_RAW,
(clockid_t)
−
1 };
int i;
for (i = 0; clocks[i] != (clockid_t)
−
1; i++) {
struct timespec ts;
int ret;
ret = clock_gettime (clocks[i], &ts);
if (ret)
perror ("clock_gettime");
else
printf ("clock=%d sec=%ld nsec=%ld\n",
clocks[i], ts.tv_sec, ts.tv_nsec);
}
取得行程時間
times()
系統呼叫可用於取得正在運行之行程與它的子行程的行程時間,以時鐘週期
(clock tick)計:
#include <sys/times.h>
struct tms {
clock_t tms_utime; /* user time consumed */
clock_t tms_stime; /* system time consumed */