乐闻世界logo
搜索文章和话题

How to calculate the CPU usage of a process by PID in Linux from C?

5 个月前提问
4 个月前修改
浏览次数56

2个答案

1
2

在Linux系统中,要从C中通过进程的PID来计算CPU使用率,可以通过解析/proc文件系统中的特定文件来实现。/proc文件系统包含了关于系统进程及其他系统信息的详细数据。特别是,每个进程的具体信息都存储在它的目录/proc/[pid]中,其中[pid]是进程的ID。

对于计算CPU使用率,我们主要关注/proc/[pid]/stat文件。这个文件包含了进程的各种统计信息,包括进程的CPU时间。CPU时间的数据可以帮助我们计算CPU使用率。

下面是具体的步骤和一个简单的示例代码:

1. 读取 /proc/[pid]/stat 文件

这个文件中的第14和第15字段分别是该进程用户态的CPU时间utime和核心态的CPU时间stime。这两个值的总和可以表示该进程占用CPU的总时间。

2. 计算总CPU时间

我们需要获取系统的总CPU时间来计算进程的CPU使用率。这可以通过读取 /proc/stat 文件的第一行来获得,其中包含了所有CPU时间的总和。

3. 计算CPU使用率

CPU使用率可以通过下面的公式计算:

[ \text{CPU 使用率} = \left( \frac{\text{进程的 CPU 时间变化}}{\text{系统的 CPU 时间变化}} \right) \times 100% ]

这需要在两个不同的时间点测量,然后计算差值。

示例代码

下面是一个简单的C程序示例,用于计算特定PID的CPU使用率:

c
#include <stdio.h> #include <unistd.h> #include <string.h> // 函数来读取CPU时间 long get_cpu_time() { FILE *fp; long total_time = 0; long value; fp = fopen("/proc/stat", "r"); fscanf(fp, "cpu %ld %ld %ld %ld %ld %ld %ld", &value, &value, &value, &value, &value, &value, &value); total_time = value; fclose(fp); return total_time; } // 函数来读取进程的CPU时间 long get_process_time(int pid) { FILE *fp; char buffer[1024]; long utime, stime; sprintf(buffer, "/proc/%d/stat", pid); fp = fopen(buffer, "r"); fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %ld %ld", &utime, &stime); fclose(fp); return utime + stime; } int main() { int pid = 1234; // 替换为目标进程的PID long initial_cpu_time = get_cpu_time(); long initial_process_time = get_process_time(pid); sleep(1); // 等待一段时间 long final_cpu_time = get_cpu_time(); long final_process_time = get_process_time(pid); double cpu_usage = 100.0 * (final_process_time - initial_process_time) / (final_cpu_time - initial_cpu_time); printf("CPU Usage: %.2f%%\n", cpu_usage); return 0; }

这段代码首先获取初始的CPU和进程时间,等待一秒钟,然后再次获取这些时间,最后计算这段期间的CPU使用率。注意要替换示例中的PID为实际的目标进程PID。

2024年6月29日 12:07 回复

在Linux中,从C语言程序中计算特定进程的CPU使用率可以通过读取 /proc/[pid]/stat 文件来实现,其中 [pid] 是进程的ID。这个文件包含了关于进程的各种统计信息,包括CPU时间统计。

以下是具体步骤和示例代码:

步骤:

  1. 打开 /proc/[pid]/stat 文件: 首先需要以读模式打开 /proc/[pid]/stat 文件,这里的 [pid] 是你想要查询的进程ID。

  2. 解析文件内容: 从这个文件中解析出需要的CPU时间信息。主要关注的字段是 utime(该进程在用户模式下花费的时间)和 stime(在核心模式下花费的时间)。

  3. 计算CPU使用率: 使用从 /proc/stat 文件中读取的总CPU时间,可以计算出进程的CPU使用率。需要记录两个时间点的数据,以计算在这段时间内CPU的使用情况。

  4. 重复测量: 对于动态计算CPU使用情况,需要定期重复上述步骤,并计算前后两次测量之间的差异。

示例代码:

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int get_cpu_usage_pct(int pid) { char path[40], line[100]; FILE *stat_file; long long int utime, stime, total_time; sprintf(path, "/proc/%d/stat", pid); stat_file = fopen(path, "r"); if (!stat_file) { perror("Failed to open stat file"); return -1; } /* 读取并解析第14和15个字段(utime和stime) */ for (int i = 1; i <= 13; i++) fscanf(stat_file, "%*s"); fscanf(stat_file, "%lld %lld", &utime, &stime); fclose(stat_file); total_time = utime + stime; /* 以某种方式转换为百分比 */ // 示例中略过了从总CPU时间到百分比的转换步骤,因为需要系统总CPU时间信息 return total_time; } int main() { int pid = 1234; // 请替换为实际的PID printf("CPU time used by process %d: %d\n", pid, get_cpu_usage_pct(pid)); return 0; }

注意: 这个示例没有将时间转换为百分比,因为我们还需要读取系统的总CPU时间,这通常是从 /proc/stat 文件中读取。在实际应用中,你可能需要存储时间点的值,然后比较两个时间点之间的差异来计算CPU的使用率。

2024年6月29日 12:07 回复

你的答案