I'm fairly new to Linux OS programming world. I'm working on a project to control Intel CPU (turbo boost, hyper threading, min & max scaling frequencies to solve problems for other users). Whilst investigating /proc/stat
for CPU utilization I stumbled across this:
$ ll /proc/thread-self
lrwxrwxrwx 1 root root 0 Aug 22 04:26 /proc/thread-self -> 9389/task/9389/
$ ll /proc/self
lrwxrwxrwx 1 root root 0 Aug 22 04:26 /proc/self -> 29420/
$ ll /proc/self
lrwxrwxrwx 1 root root 0 Aug 22 04:26 /proc/self -> 29636/
$ ll /proc/thread-self
lrwxrwxrwx 1 root root 0 Aug 22 04:26 /proc/thread-self -> 30021/task/30021/
thread-self
and self
are constantly changing every time you list the symbolic links with ll
. The date and time remain at the last boot time though.
I did a little digging and found this email chain in 2014:
This patchset implements
/proc/thread-self
a magic symlink that solves a couple of problems.
.... further down the email says:
proc: Have net show up under /proc/<tgid>/task/<tid> proc: Implement /proc/thread-self to point at the directory of the current thread proc: Point /proc/net at /proc/thread-self/net instead of /proc/self/net proc: Point /proc/mounts at /proc/thread-self/mounts instead of /proc/self/mounts
How dp the symbolic link(s) work? Or how do we use them for meaningful analysis/display of what Linux is doing? Or are they simply meaningless and to be ignored as "fluff"?
/proc/self
is a symlink to the current process, the ls
process in your examples. Since each ll
spawns a new ls
process with a different PID the symlink changes each time. It's a magic symlink that changes based on which process accesses it. Different processes will see different destinations.
/proc/thread-self
is the same thing except it points to the current thread instead of the current process. In the output you show, ls
is looking up the symlink on its main (and probably only) thread, so the thread ID matches the process ID.
If you check the output of mount
or cat /proc/mounts
you'll see how this "magic" is implemented. /proc
is typically mounted as a virtual filesystem.
$ mount | grep proc
proc on /proc type proc (rw)
Any time you access an entry under /proc
the proc
filesystem handles the lookup. In contrast to standard filesystems like ext4
, xfs
, or tmpfs
, there is no underlying set of files on disk. Instead, there is a kernel level driver that handles all the filesystem calls. When you access /proc/self
the kernel driver checks what process is making the request and dynamically crafts a symlink pointing to that processes's PID.
What use is it? Well, there's a lot of information under /proc/<PID>
. For example, you can look in /proc/<PID>/fd/*
to see what file descriptors a process has open. /proc/<PID>/cmdline
gives the the contents of argv
. /proc/<PID>/status
has a bunch of information including uid, gid, and memory usage. You might, for instance, insert a call to ls -l /proc/self/fd/
when debugging file descriptor leaks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With