在 Linux 中从 /proc 获取 PID 列表

rab*_*bit 2 c linux pid procfs

我做节目,如果页面错误发生在一些流程,可以看到,我的方法做,这是获得所有进程的PID和看到rssmaj_flt寻求在每一个等/proc/[PID],检查是否有在总的变化maj_flt

但为了得到所有正在运行的进程的PID的,我需要直接从我的C程序得到这些,而无需使用像现有的shell命令pstop等等。

有谁知道正在运行的 PID 数据存在于/proc何处或其他地方?或者如果有另一种方法可以做到这一点,比如通过我的 C 程序中的系统调用函数来获取它?

Mar*_*lli 5

不幸的是,没有公开 PID 列表的系统调用。在 Linux 中您应该通过/proc虚拟文件系统获取这些信息。

如果您想要当前正在运行的进程的 PID 列表,您可以使用opendir()readdir()打开/proc并迭代其中的文件/文件夹列表。然后,您可以检查文件名是数字的文件夹。检查后,您可以直接打开/proc/<PID>/stat获取您想要的信息(特别是您想要第12个字段majflt)。

这是一个简单的工作示例(可能需要进行更多错误检查和调整):

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <ctype.h>

// Helper function to check if a struct dirent from /proc is a PID folder.
int is_pid_folder(const struct dirent *entry) {
    const char *p;

    for (p = entry->d_name; *p; p++) {
        if (!isdigit(*p))
            return 0;
    }

    return 1;
}

int main(void) {
    DIR *procdir;
    FILE *fp;
    struct dirent *entry;
    char path[256 + 5 + 5]; // d_name + /proc + /stat
    int pid;
    unsigned long maj_faults;

    // Open /proc directory.
    procdir = opendir("/proc");
    if (!procdir) {
        perror("opendir failed");
        return 1;
    }

    // Iterate through all files and folders of /proc.
    while ((entry = readdir(procdir))) {
        // Skip anything that is not a PID folder.
        if (!is_pid_folder(entry))
            continue;

        // Try to open /proc/<PID>/stat.
        snprintf(path, sizeof(path), "/proc/%s/stat", entry->d_name);
        fp = fopen(path, "r");

        if (!fp) {
            perror(path);
            continue;
        }

        // Get PID, process name and number of faults.
        fscanf(fp, "%d %s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %lu",
            &pid, &path, &maj_faults
        );

        // Pretty print.
        printf("%5d %-20s: %lu\n", pid, path, maj_faults);
        fclose(fp);
    }

    closedir(procdir);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

示例输出:

    1 (systemd)           : 37
   35 (systemd-journal)   : 1
   66 (systemd-udevd)     : 2
   91 (dbus-daemon)       : 4
   95 (systemd-logind)    : 1
  113 (dhclient)          : 2
  143 (unattended-upgr)   : 10
  148 (containerd)        : 11
  151 (agetty)            : 1
  ...
Run Code Online (Sandbox Code Playgroud)

  • 完成后不应该调用“closedir(procdir);”吗? (3认同)