我制作了一个程序,以便在我的C ++程序中获得CPU使用率。但是除了
,我什么都得不到0.000000 sec user
0.000000 sec system
如何获得可用的价值?
我试过那段代码:
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
void print_cpu_time() {
struct rusage usage;
getrusage (RUSAGE_SELF, &usage);
printf ("CPU time: %ld.%06ld sec user, %ld.%06ld sec system\n",
usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
}