使用c语言实时获取linux系统CPU各个核心的使用率
时间: 2024-03-27 17:39:19 浏览: 107
linux cpu 使用率
可以使用Linux系统提供的/proc/stat文件来获取CPU各个核心的使用率。
具体步骤如下:
1. 打开/proc/stat文件,读取CPU总的使用时间和空闲时间
```
FILE *fp = fopen("/proc/stat", "r");
char buf[1024];
fgets(buf, sizeof(buf), fp);
```
2. 解析第一行数据,获取总的CPU使用时间和空闲时间
```
unsigned long long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
sscanf(buf, "%*s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
unsigned long long total = user + nice + system + idle + iowait + irq + softirq + steal;
unsigned long long idle_time = idle + iowait;
```
3. 等待一段时间(比如1秒),再次读取/proc/stat文件,计算CPU各个核心的使用率
```
sleep(1);
fseek(fp, 0, SEEK_SET);
fgets(buf, sizeof(buf), fp);
unsigned long long user2, nice2, system2, idle2, iowait2, irq2, softirq2, steal2, guest2, guest_nice2;
sscanf(buf, "%*s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", &user2, &nice2, &system2, &idle2, &iowait2, &irq2, &softirq2, &steal2, &guest2, &guest_nice2);
unsigned long long total2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + steal2;
unsigned long long idle_time2 = idle2 + iowait2;
unsigned long long diff_total = total2 - total;
unsigned long long diff_idle = idle_time2 - idle_time;
double usage = (diff_total - diff_idle) * 100.0 / diff_total;
```
4. 根据CPU核心数,重复步骤2和步骤3,计算每个核心的使用率
```
int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
unsigned long long user_cpu[cpu_num], nice_cpu[cpu_num], system_cpu[cpu_num], idle_cpu[cpu_num], iowait_cpu[cpu_num], irq_cpu[cpu_num], softirq_cpu[cpu_num], steal_cpu[cpu_num], guest_cpu[cpu_num], guest_nice_cpu[cpu_num];
memset(user_cpu, 0, sizeof(user_cpu));
memset(nice_cpu, 0, sizeof(nice_cpu));
memset(system_cpu, 0, sizeof(system_cpu));
memset(idle_cpu, 0, sizeof(idle_cpu));
memset(iowait_cpu, 0, sizeof(iowait_cpu));
memset(irq_cpu, 0, sizeof(irq_cpu));
memset(softirq_cpu, 0, sizeof(softirq_cpu));
memset(steal_cpu, 0, sizeof(steal_cpu));
memset(guest_cpu, 0, sizeof(guest_cpu));
memset(guest_nice_cpu, 0, sizeof(guest_nice_cpu));
fseek(fp, 0, SEEK_SET);
fgets(buf, sizeof(buf), fp);
for (int i = 0; i < cpu_num; i++) {
fgets(buf, sizeof(buf), fp);
sscanf(buf, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", &i, &user_cpu[i], &nice_cpu[i], &system_cpu[i], &idle_cpu[i], &iowait_cpu[i], &irq_cpu[i], &softirq_cpu[i], &steal_cpu[i], &guest_cpu[i], &guest_nice_cpu[i]);
}
sleep(1);
fseek(fp, 0, SEEK_SET);
fgets(buf, sizeof(buf), fp);
for (int i = 0; i < cpu_num; i++) {
fgets(buf, sizeof(buf), fp);
unsigned long long user2_cpu, nice2_cpu, system2_cpu, idle2_cpu, iowait2_cpu, irq2_cpu, softirq2_cpu, steal2_cpu, guest2_cpu, guest_nice2_cpu;
sscanf(buf, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", &i, &user2_cpu, &nice2_cpu, &system2_cpu, &idle2_cpu, &iowait2_cpu, &irq2_cpu, &softirq2_cpu, &steal2_cpu, &guest2_cpu, &guest_nice2_cpu);
unsigned long long total_cpu = user2_cpu + nice2_cpu + system2_cpu + idle2_cpu + iowait2_cpu + irq2_cpu + softirq2_cpu + steal2_cpu;
unsigned long long idle_time_cpu = idle2_cpu + iowait2_cpu;
unsigned long long diff_total_cpu = total_cpu - (user_cpu[i] + nice_cpu[i] + system_cpu[i] + idle_cpu[i] + iowait_cpu[i] + irq_cpu[i] + softirq_cpu[i] + steal_cpu[i] + guest_cpu[i] + guest_nice_cpu[i]);
unsigned long long diff_idle_cpu = idle_time_cpu - idle_cpu[i];
double usage_cpu = (diff_total_cpu - diff_idle_cpu) * 100.0 / diff_total_cpu;
}
```
5. 关闭文件指针,输出CPU各个核心的使用率
```
fclose(fp);
for (int i = 0; i < cpu_num; i++) {
printf("CPU%d Usage: %.2f%%\n", i, usage_cpu[i]);
}
```
阅读全文