linux-kernel如何读取proc/pid文件?

发布于 2024-08-12 01:06:48 字数 123 浏览 4 评论 0原文

linux-kernel 如何以及在哪里读取显示系统中所有进程的 proc/pid 文件。我找到了 linux-source-2.6.31/fs/proc/ 这里有文件,但是很难理解,因为它真的很复杂。有人可以知道它是如何工作的吗?

How and Where does linux-kernel read proc/pid file which shows all processes in the system. I found linux-source-2.6.31/fs/proc/ Here there are files, but it is hard to understand because it is really complicated. Can someone knows, how it works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-08-19 01:06:48

/proc 是一个伪文件系统,这意味着它的内容不是“真实”文件。相反,内容是内核内部数据结构的表示。因此内核不需要读取它们 - 它已经可以直接访问数据。

/proc 由用户模式(即非内核)程序使用,例如 ps来查找(例如)有关系统上运行的进程的信息。有一个 手册页 描述了大部分可用的内容。

/proc is a pseudo-filesystem, meaning that its contents are not "real" files. Instead, the contents are a representation of the kernel's internal data structures. Therefore the kernel doesn't need to read them - it can already access the data directly.

/proc is used by user-mode (i.e. non-kernel) programs such as ps to find out (for instance) about processes running on the system. There's a man page that describes much of what's available.

何必那么矫情 2024-08-19 01:06:48

您正在寻找正确的地方。

具体来说,fs/proc/base.c中的函数proc_pid_readdir()用于在/proc读取根目录。您可以看到该函数中所有进程和任务的基本循环:

ns = filp->f_dentry->d_sb->s_fs_info;
iter.task = NULL;
iter.tgid = filp->f_pos - TGID_OFFSET;
for (iter = next_tgid(ns, iter);
     iter.task;
     iter.tgid += 1, iter = next_tgid(ns, iter)) {
    filp->f_pos = iter.tgid + TGID_OFFSET;
    if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
        put_task_struct(iter.task);
        goto out;
    }
}

You're looking in the right place.

Specifically, the function proc_pid_readdir() in fs/proc/base.c is used to fill out the list of pid entries when the /proc root directory is read. You can see the basic loop around all processes and tasks in that function:

ns = filp->f_dentry->d_sb->s_fs_info;
iter.task = NULL;
iter.tgid = filp->f_pos - TGID_OFFSET;
for (iter = next_tgid(ns, iter);
     iter.task;
     iter.tgid += 1, iter = next_tgid(ns, iter)) {
    filp->f_pos = iter.tgid + TGID_OFFSET;
    if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
        put_task_struct(iter.task);
        goto out;
    }
}
浅忆流年 2024-08-19 01:06:48

查看您的 /proc 目录,其中有一个虚拟文件列出了系统中运行的所有进程,甚至二进制程序 ps 实际上也在 /proc 目录中打开该文件以输出进程/pid 的列表..

Linux ProcFs 指南
Linux Proc 文件系统作为程序员的工具

Look in your /proc directory, there is a virtual file in there that lists all processes running in the system, even the binary program ps actually opens up that file in the /proc directory to output the listing of processes/pids..

Linux ProcFs Guide
Linux Proc Filesystem as a Programmer's Tool

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文