Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.