Вы не определить, какую версию Ubuntu вы используете, но этот код (скомпилирован с опцией -std=c11
или -std=c99
) показывает, что times()
хорошо работает на Ubuntu 14.04 LTS при запуске в виртуальной машине на Mac OS X 10.10.2 Йосемити ,
Файл ubuntu.times.c
:
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <unistd.h>
#define PRI_clock_t "lu" /* Correct on Mac OS X */
static void report_times(void)
{
struct tms t;
clock_t t0 = times(&t);
printf("%" PRI_clock_t ": %" PRI_clock_t " %" PRI_clock_t "; %" PRI_clock_t " %" PRI_clock_t "\n",
t0, t.tms_utime, t.tms_stime, t.tms_cutime, t.tms_cstime);
}
static char *cmd0[] = { "sleep", "1", (char *)0 };
static char *cmd1[] = { "dd", "if=/dev/zero", "of=/dev/null", "bs=1024", "count=1000000", (char *)0 };
static void exec_cmd(char **args)
{
for (int i = 0; i < 10; i++)
{
if (fork() == 0)
{
execvp(args[0], args);
exit(1);
}
int status;
int corpse = wait(&status);
printf("PID %d: 0x%.4X\n", corpse, status);
}
report_times();
}
int main(void)
{
report_times();
FILE *fp = fopen("/dev/null", "w");
for (int i = 0; i < 1000000; i++)
{
fprintf(fp, "Row %d\n", i);
}
report_times();
exec_cmd(cmd0);
exec_cmd(cmd1);
return 0;
}
Пример вывода на Ubuntu:
$ ./ubuntu.times
1718230233: 0 0; 0 0
1718230242: 7 0; 0 0
PID 67448: 0x0000
PID 67449: 0x0000
PID 67450: 0x0000
PID 67451: 0x0000
PID 67452: 0x0000
PID 67453: 0x0000
PID 67455: 0x0000
PID 67456: 0x0000
PID 67457: 0x0000
PID 67458: 0x0000
1718231249: 8 0; 0 1
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.295776 s, 3.5 GB/s
PID 67459: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.291788 s, 3.5 GB/s
PID 67460: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.303734 s, 3.4 GB/s
PID 67461: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.289385 s, 3.5 GB/s
PID 67462: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.292731 s, 3.5 GB/s
PID 67463: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.290734 s, 3.5 GB/s
PID 67464: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.292078 s, 3.5 GB/s
PID 67465: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.289427 s, 3.5 GB/s
PID 67466: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.29415 s, 3.5 GB/s
PID 67467: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.295052 s, 3.5 GB/s
PID 67468: 0x0000
1718231548: 8 0; 56 234
$
Регулируя в коде ошибки минимальна на несуществующие; он не должен считаться хорошим стилем.
Документация, к которой вы обращаетесь, предназначена для QNX, полностью отдельной операционной системы от Ubuntu Linux. Документация для ['times()'] (http://linux.die.net/man/2/times) из http://linux.die.net/ не упоминает такие проблемы. Возможно, вам нужно найти документы для Ubuntu, но при отсутствии информации об обратном предположите, что это работает. –
Вполне возможно (например, использовать 128 мс общего процессорного времени (8 мс на каждом из 16 процессоров) и заканчивать «реальным временем» 8 мс на ОС, которая имеет размерность таймера по таймеру 10 мс и заканчивается сообщение 0 мс. – Brendan