To access PCB of process in C

2019-08-02 16:39发布

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 .....

2条回答
Fickle 薄情
2楼-- · 2019-08-02 17:00

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:

$ cd /proc/1
$ ls
...a bunch of files...

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:

$ cat status
Name:   init
State:  S (sleeping)
Tgid:   1
Pid:    1
PPid:   0
...
$

Good luck.

查看更多
forever°为你锁心
3楼-- · 2019-08-02 17:08

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 for proc(5) for details. This information is generally available in plain text form and can be easily displayed - try, for example:

cat /proc/self/status

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!

查看更多
登录 后发表回答