Need the source code for top and find the connection between top and /proc/ directory in linux

StackOverflow https://stackoverflow.com/questions/3371131

  •  05-10-2020
  •  | 
  •  

Question

I wanted the source code for top which I could not find anywhere also, i wanted a little more information on what exactly does the /proc directory contain.y I have seen it have a series folders labeled 1, 2, 3 4, .... and in those folders there seem to be a consistent set of files. I was wondering if these are the directories for the processes running on the machine at the moment.

Also I wanted to know, how exactly TOP linked to this folder because I have been told that the processes are monitored by TOP, by fetching data from these directories. I would like to know which file exactly is TOP getting the CPU usage of the particular process from with the directory. If it's too complicated it would be great if you could just point me to the portion of the code where I could actually understand this from!

Thanks for your help Shouvik

OTHER TIPS

top is part of procps, and yes, those numbers are process id's.

The procfs is basically an file abstraction of system and process information.

The numbered folders are currently running processes with the folder name related PID.


You can trace which files where read out by top or any other process with

strace -e open top

or on raspberry

strace -e openat top

or more general with grep

strace top | grep open

for example, U get the ouput

...
openat(AT_FDCWD, "/proc/7353/stat", O_RDONLY|O_LARGEFILE) = 8
openat(AT_FDCWD, "/proc/7353/statm", O_RDONLY|O_LARGEFILE) = 8
...

Here you can see, that top opened the file

/proc/[pid]/stat

which contains certain information about the process

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top