在C中访问进程的PCB
我在 Linux 中工作,我有点困惑,我是否可以访问进程的 PCB?如果是,那么我们可以访问它的哪些内容并将其打印到终端上,如果不是,那么为什么不呢?
感谢您的回答......
I am working in Linux and i have a little bit confusion that whether i can access the PCB of process or not? if yes then what content of it we can access it and print them on to the terminal and if not then why not?
thanks for answering .....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果通过 PCB,您指的是进程控制块,是或否...
否,因为它位于内核地址空间中,不能被用户进程直接访问。内核在
/proc
下提供 PCB 中的一些信息 - 请参阅proc 的联机帮助页(5)
了解详情。此信息通常以纯文本形式提供,并且可以轻松显示 - 尝试,例如:是,因为使用内核调试工具
struct task_struct
(以及其他相关结构)。这不是一件容易的事;您需要充分了解内核源代码。基本思想是尝试在/proc/kcore
或/proc/kmem
中找到该结构。您将需要管理(root)权限并且对内核内存布局有很好的了解。这应该仅用于内核调试或探索 - 请不要在生产代码中执行此操作,特别是当内部内核结构的布局在内核版本之间发生变化而没有警告时!If by PCB, you mean the Process Control Block, yes and no...
No, because it's in the kernel address space and cannot be accessed directly by user processes. The kernel makes some information from the PCB available under
/proc
- see the manpage forproc(5)
for details. This information is generally available in plain text form and can be easily displayed - try, for example:Yes, because using kernel debugging facilities the
struct task_struct
(and other related structures) for a process can be accessed. This is not an easy task; you'll need a good understanding of the kernel source code. The basic idea will be to try to locate the structure in/proc/kcore
or/proc/kmem
. You will need administrative (root) rights and a very good understanding of the kernel memory layout. This should be done only for kernel debugging or exploration - please don't do this in production code, particularly as the layout of internal kernel structures changes without warning between kernel versions!大多数信息可以通过 proc 文件系统访问,通常安装在 /proc。例如,如果我想查看系统上进程 1 的信息:
您可以通过“man proc”或“man 5 proc”了解其中大部分的含义。有关这些文件的大量信息也存在于 Linux 源代码树“${LINUX_SRC}/Documentation/filesystems/proc.txt”中。这些文件可以像任何其他文件一样打开和读取。例如:
祝你好运。
Most of that information can be accessed via the proc filesystem, usually mounted at /proc. For example, if I want to see the info for process 1 on my system:
You can find out what most of these mean via "man proc" or "man 5 proc". A great deal of information on these files also exists in the Linux source tree at "${LINUX_SRC}/Documentation/filesystems/proc.txt". These files can be opened and read just like any other file. For example:
Good luck.