19

I was tinkering around with the /proc filesystem in Linux, and I came across /proc/self, which is a symbolic link to the process directory of the current process. I would like to know how it is implemented. One solution would be to change that symlink on every context switch, but that's obviously very expensive as it involves a disk access.

2 Answers2

20

http://lxr.linux.no/linux+v3.2.9/fs/proc/base.c#L2482 is the current implementation.

The proc filesystem is entirely virtual, and is implemented so the internal VFS readlink delegates to the right place for special symlinks. So, it calculates what self points to when it is read / traversed, not every context switch.

13

Files in /proc are not stored on a disk, they are generated on the fly by the kernel. See What happens when I open and read from /proc?

If you're programmatically inclined, you can read the implementation of /proc in the kernel source code. The contents of the /proc/self symbolic link is generated by a function that fills a buffer with the pid of the calling process.