What happens when I write
cat /proc/cpuinfo. Is that a named pipe (or something else) to the OS which reads the CPU info on the fly and generate that text each time I call it?
Whenever you read a file under
Procfs is present in most non-BSD unices. It started its life in AT&T's Bell Labs in UNIX 8th edition as a way to report information about processes (and
In the past, Linux's
Your first and second entry points for documentation about
Your third entry point, when the documentation doesn't cover it, is reading the source. You can download the source on your machine, but this is a huge program, and LXR, the Linux cross-reference, is a big help. (There are many variants of LXR; the one running on
The core handling of
A big part of
Another important area of
When trying to gain insight into what sort of magic is happening behind the scenes your best friend is
From the above output you can see that
Deeper dive#1 - with ls..
Looking at the file itself it would appear to be "just a file".
But take a closer look. We get our first hint that its special, note the size of the file is 0 bytes.#2 - with stat..
If we now look at the file using
Notice the Access, Modify, & Change times? They keep changing for each access. This is highly unusual that all 3 would change like that. Unless edited a file's timestamp attributes typically stay the same.#3 - with file..
Yet another clue that this file is anything but a regular file:
If it were some manifestation of a named pipe it would show similar to one of these files:
If we touch an
#4 - with mount..
So at this point we need to take a step back and zoom out a bit. We're looking at a particular file but perhaps we should be looking at the filesystem this file resides on. And for this we can use the
OK, so the filesystem type is of type
Taking a look at
And if we take a look at
A little bit further down in that same man page:
At the bottom of the man page is a reference to a kernel document which you can find here, titled: THE /proc FILESYSTEM. Quoting from that document:
So what did we learn here? Well given that
I'll close with this quote which apparently used to be in a prior version of the
Source: The proc pseudo file-system
The answer given by @slm is very comprehensive, but I think a simpler explanation might come from a change in perspective.
In day-to-day usage we can think of files as physical things, ie. chunks of data stored on some device. This makes files like /proc/cpuinfo very mysterious and confusing. However, it all makes perfect sense if we think of files as an interface; a way to send data in and out of some program.
The programs which send and receive data in this way are filesystems or drivers (depending on how you define these terms, that might be too broad or too narrow a definition). The important point is that some of these programs use a hardware device to store and retrieve the data sent via this interface; but not all.
Some examples of filesystems which don't use a storage device (at least directly) are:
The Plan9 OS ( http://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs ) is an extreme example of using files as a general programming interface.