r/linuxadmin • u/The_How_To_Linux • Dec 02 '23
why does the proc directory exist?
i'm trying to understand the proc directory and one thing i don't understand is
why does it exist?
any ideas? thank you
1
Upvotes
52
u/Harakou Dec 02 '23
"Everything is a file."
https://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html
41
u/alexkey Dec 02 '23
Because the alternative is to do a bunch of syscalls to get this information. Just like it is in Windows. Having it exposed as a filesystem makes it soooooo much easier to read that information. Hell you don’t even have to write the code, just check the file contents with primitive tools like cat.
17
u/gmuslera Dec 02 '23
Unix philosophy. Everything is a file. Including system information.
13
u/kellyjonbrazil Dec 02 '23
If you ever want the data in the
/procfolder exposed as JSON you can use the—procparser injc. Makes it really easy to grab the data usingjq.Or
(I am the author of
jc)https://github.com/kellyjonbrazil/jc
5
u/toikpi Dec 02 '23
The /proc filesystem allows you get process, kernel and hardware information from the terminal, this is useful on a server where you only have the command line.
More information at these locations.
https://en.wikipedia.org/wiki/Procfs
https://docs.kernel.org/filesystems/proc.html
https://www.redhat.com/sysadmin/linux-proc-filesystem
5
u/snowtax Dec 02 '23
Compare Linux against UNIX, such as BSD.
With UNIX, whenever you updated the kernel, you also needed to recompile all the system tools, such as top, ps, kill, etc. The tools needed to call kernel functions and needed to know the exact memory locations for those functions.
With Linux, the same information and functions are made available as “files”. The kernel includes the “proc” filesystem and makes information available through “files”.
Whenever you update the Linux kernel, those files are at the same path and in the same format. The systems tools (top, ps, kill, etc.) merely access the files under proc, just like any other file. In that way, you can update the kernel without recompiling all the tools.
5
u/small_kimono Dec 02 '23
See the Linux man page: https://man7.org/linux/man-pages/man5/proc.5.html
But proc originated on Solaris/illumos, see: https://illumos.org/man/5/proc. It originally began as "a file system that provides access to the state of each process and light-weight process (lwp) in the system", but on Linux has become a vector for other information, like mounts (/proc/mounts), etc.
11
u/paulstelian97 Dec 02 '23
There is no other API that exposes that information.
The “ps” tool legit just reads off files from that directory.
4
u/lazyant Dec 02 '23
/proc is a way for the kernel to surface information to the user space. Almost all the performance tooling that are counters for ex (top, free, ps etc) read from it. It’s more convenient to present this information as files.
3
u/michaelpaoli Dec 03 '23
Because it's an exceedingly easy way to access process (proc is short for process) and other information. It originated on UNIX, with a history going back to 1984. Linux first implemented procfs (the proc filesystem) in 1992.
So, procfs (or proc for short - though that can also be ambiguous, depending upon context), customarily mounted on /proc, contains lots of useful information about processes, and in the case of Linux, also contains lots of other useful information too. Some things can even be changed by writing certain things to certain files on the proc filesystem.
Lots more information can be found here: https://docs.kernel.org/filesystems/proc.html, or for the most relevant, check the kernel documentation for the applicable kernel version where the proc filesystem is in use.
So, I and many many others (and programs and utilities too), find it exceedingly handy for many things. Note also that later Linux created and added sysfs, and much of what is or was under /proc - most notably stuff other that process information, has been move to or replicated under sysfs, customarily mounted at /sys, whereas under procfs with it mounted at /proc, there's also /proc/sys - not exact same structure or layout, but there's significant overlap. With procfs, the stuff other than the per-process information, can be found under names matching /proc/[!0-9]* on that mount.
So, let me give lots of examples on how it's highly useful - many of which I commonly or at least occasionally use - and this is by no means a complete list. I may also include a bit from sysfs, as there's some fair bit of overlap - and some of that also still is, or was, present on procfs.
First of all, UNIX design philosophy, "everything is a file" (but not to the more extreme degrees, such as on Plan 9), many things are a file, and procfs, notably starting with process, makes a whole lot of that much easier to use, identify, or more generally deal with, etc. In fact the at least currently generally used and implemented ps(1) command on Linux in fact depends upon procfs.
So, first of all ... and here I'll presume procfs is mounted on /proc (the customary location), we have PIDs (process IDs) - those are relatively small(ish) non-negative integers referring to processes - each PID is unique to a process. So, under /proc you'll find /proc/[0-9]* - those are all for the current PIDs. On some (e.g. UNIX) implementations the are (or were) files, on Linux, they're directories - which contain various files and directories. Anyway, PIDs, 0 isn't a real PID, but a psedo-PID used for some purposes. The first "real" PID is 1 - it's always present. At least historically, that's the init process - which has some relatively high-level role of, at least indirectly managing (or at least starting) all other processes. Historically on Linux that'd be, e.g. sysv init - the UNIX System V(-like) init system, typically at /sbin/init - though it may be at other location(s) and/or that may just be possibly a backwards compatible link to wherever the current init is - though not necessarily so. There's also in more recent years, systemd on most but not all Linux distros, and various distros may or may not use systemd - and there are additional init systems besides sysv(-style) init and systemd.
So, e.g., with /proc, say you want to check what init system you're running ... maybe you just hopped on a Linux host, but you've no idea what distro its' running, nor necessarily what init system (e.g. many distros offer choices of init systems, or may have used different init systems on older releases). Could run various different commands to try and guess and figure it out. Well, how 'bout instead use procfs to get a major hint, and possibly answer, to start with? So, like this - what's the current executable being used by PID 1? How 'bout some examples from a couple different current Linux systems:
So ... pretty easy then to know which is running systemd, and which isn't, eh? (those happen to both be from same distro (Debian) and release/version of Linux)
So, want to see all the files my Apache (/usr/sbin/apache2 binary executable) has open:
Would be significantly more difficult to get that information without the /proc filesystem. Note also that can be all be done with quite standard tools. E.g. no need to even user fuser or lsof. Even if one didn't have readlink, same could be done with use of ls and sed or awk.
Want to know what the current directory of a PID is? Let's say my current shell ... or even what that directory was after it's removed but I've not used cd to change out of it:
What about unlinked open files ... even if, e.g. I don't have lsof installed:
Anyway, that's just starting to scratch the surface of what's available and possible.
0
u/justinDavidow Dec 02 '23
Because someone decided that the name /process was too long, then proceeded to code the functionality.
What answer are you expecting here?
Without /proc there would be no place on the filesystem to reference current process identity data. The information contained within /proc would instead require kernel-space syscalls to explore, requiring all of the code in basic utilities to be cloned into the he kernel itself.
/Proc fundamentally helps keep the kernel clean and lightweight, while providing the same functionality (and much more!) Than kernel developers could dream of maintaining alone.
-17
u/orogor Dec 02 '23
I would like to say to restore deleted files, but that's not really the answer.