The ‘pseudo’ file system

In the magic Linux world 🐧 there is one specific entity with the label ‘pseudo file system’. It sits tight and altough is a little bit jealous that it’s not part of the /etc/fstab it’s the kernel best sharing friend and exposes all of its secrets.

After this introduction it’s time to give a down to earth explanation. In Linux you have a directory with the path and name /proc, which for the most part exposes kernel information in user land. Many internal commands that you use like top are actually getting their data from there.

Also for each process that is currently in place you have a lot of information shared. That gives you valuable internal if you are debugging or simply want to understand more.

If you wake up one day really curious about the number of open files a process is currently handling this is the best place to go.


Let’s visit this land with our best below buddy.

import threading
import time
import os
print("PID: {0}".format(os.getpid()))
def write(filename, n):
with open(filename, "w") as f:
f.write("vip msg")
f.flush()
time.sleep(100)
threads = []
for i in range(0, 10):
filename = "file-{0}".format(str(i))
t = threading.Thread(target=write, args=(filename, i,))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()


Long story short we are opening 10 files at once, write very important message to each one of them and wait for some time each worker.

python3 write.py
PID: 16899

Now let’s go to our visit place and see how it’s doing on one of its addresses 16899.

cd /proc/16899/
/proc/16899$ ls
arch_status exe mem personality status
attr fd mountinfo projid_map syscall
autogroup fdinfo mounts root task
auxv gid_map mountstats sched timens_offsets
cgroup io net schedstat timers
clear_refs ksm_merging_pages ns sessionid timerslack_ns
cmdline ksm_stat numa_maps setgroups uid_map
comm latency oom_adj smaps wchan
coredump_filter limits oom_score smaps_rollup
cpu_resctrl_groups loginuid oom_score_adj stack
cwd map_files pagemap stat
environ maps patch_state statm

That’s a lot of things to handle. Let’s summarize a few of those that I consider most important, it all really depends on what kind of process you are looking at.

environ - the environment variables initially passed to the process. There are also environment variables which could be later on passed and you would not find them here.

fd – all the file descriptors that the process has opened.

fdinfo – for each opened file descriptors above there is a few fields here, including the ino field which reflects the inode number of the target file.

limits – here you would find how much maximum number of open file descriptors your process might have, as well as the max stack size.

Let’s open the fd and see what is the current state there.

ls -l
total 0
lrwx------ 1 gosho gosho 64 Mar 14 19:27 0 -> /dev/pts/3
lrwx------ 1 gosho gosho 64 Mar 14 19:27 1 -> /dev/pts/3
l-wx------ 1 gosho gosho 64 Mar 14 19:27 10 -> /home/gosho/Blog/02.Python/file-7
l-wx------ 1 gosho gosho 64 Mar 14 19:27 11 -> /home/gosho/Blog/02.Python/file-8
l-wx------ 1 gosho gosho 64 Mar 14 19:27 12 -> /home/gosho/Blog/02.Python/file-9
lrwx------ 1 gosho gosho 64 Mar 14 19:27 2 -> /dev/pts/3
l-wx------ 1 gosho gosho 64 Mar 14 19:27 3 -> /home/gosho/Blog/02.Python/file-0
l-wx------ 1 gosho gosho 64 Mar 14 19:27 4 -> /home/gosho/Blog/02.Python/file-1
l-wx------ 1 gosho gosho 64 Mar 14 19:27 5 -> /home/gosho/Blog/02.Python/file-2
l-wx------ 1 gosho gosho 64 Mar 14 19:27 6 -> /home/gosho/Blog/02.Python/file-3
l-wx------ 1 gosho gosho 64 Mar 14 19:27 7 -> /home/gosho/Blog/02.Python/file-4
l-wx------ 1 gosho gosho 64 Mar 14 19:27 8 -> /home/gosho/Blog/02.Python/file-5
l-wx------ 1 gosho gosho 64 Mar 14 19:27 9 -> /home/gosho/Blog/02.Python/file-6

You could see a set of symbolic links towards those files. We also have started counting from 3, since 0, 1 and 2 are reserved for each process to present the standard input, output and error.

Leave a comment