Notes about /proc/self/
转载自:/proc/self/目录的意义和Which process is /proc/self/
for?。
可以通过/proc/$pid/来获取指定进程的信息,例如内存映射、CPU绑定信息等等。如果某个进程想要获取本进程的系统信息,就可以通过进程的pid来访问/proc/$pid/目录。但是这个方法还需要获取进程pid,在fork、daemon等情况下pid还可能发生变化。为了更方便的获取本进程的信息,linux提供了/proc/self/目录,这个目录比较独特,不同的进程访问该目录时获得的信息是不同的,内容等价于/proc/本进程pid/。进程可以通过访问/proc/self/目录来获取自己的系统信息,而不用每次都获取pid。
When the kernel has to answer the question “What does /proc/self
point to?”, it simply picks the currently-scheduled pid, i.e. the currently running process (on the current logical CPU). The effect is that /proc/self
always points to the asking program’s pid; if you run1
ls -l /proc/self
you’ll see ls
‘s pid; if you write code which uses /proc/self
then code will see its own pid, etc.