Я пытался получить% загрузку процессора с C. Я видел это решение: How to get the CPU Usage in C?процессора в C
Поэтому я попытался с помогает при условии:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <unistd.h>
#define NANO2SEC 1000000000
struct timespec gettimenow;
double getWtime;
double getCtick;
int ncore;
double cpu_util;
double get_wall_time() {
if (clock_gettime(CLOCK_REALTIME,&gettimenow)){
//error handle
return 0;
}
return ((double)gettimenow.tv_sec + ((double)gettimenow.tv_nsec/NANO2SEC));
}
double get_cpu_time() {
return ((double)clock()/sysconf (_SC_CLK_TCK));
}
int core_logical() {
return (sysconf(_SC_NPROCESSORS_ONLN));
}
void main() {
getWtime = get_wall_time();
printf("\nWall time : %f \n", getWtime);
getCtick = get_cpu_time();
printf("\nCPU time : %f \n", getCtick);
ncore = core_logical();
printf("\nNo of cores : %d \n", ncore);
cpu_util = (getCtick/ncore/getWtime);
printf("\nCPU Utilization : %f %% \n", cpu_util);
}
о/р время
Стена: 1439132892,054816
процессорное время: 17,280000
Нет ядер: 2
Использование ЦП: 0,000000%
Но с высшим командным я обнаружил, что использование процессора не совсем 0%, то есть более, что это. то есть. 6.2% Я хочу знать% от используемого процессора в настоящее время.
@ Anton.P такой же результат – Zim
Для чего вы хотите использовать cpu? Система? Процесс? –
@thatotherguy total cpu использование системы в% – Zim