linux-kernel如何读取proc/pid文件?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
/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.您正在寻找正确的地方。
具体来说,
fs/proc/base.c
中的函数proc_pid_readdir()
用于在/proc
读取根目录。您可以看到该函数中所有进程和任务的基本循环:You're looking in the right place.
Specifically, the function
proc_pid_readdir()
infs/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:查看您的 /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