0

I have a question and I'm not sure it's a pertinent one (maybe I miss something).

saying that on linux everything is a file means that :

1 - communication with modules and processes is writing to files : is that correct

2 - if it's correct, those files are stored on hard drive ?

3 - if it's correct, doest it take time to R/W hard drive ?

  • Related: https://unix.stackexchange.com/questions/225537/everything-is-a-file – Kusalananda Apr 20 '17 at 15:37
  • Related: https://unix.stackexchange.com/questions/141016/a-laymans-explanation-for-everything-is-a-file-what-differs-from-windows – Kusalananda Apr 20 '17 at 15:39
  • Linux and most Unixes don't really go all the way, and make everything a file. Plan 9 took it all the way so would have files such as /net/https://unix.stackexchange.com/questions/360212/is-linux-everything-is-a-file-reducing-performance that could contain this page. This page is not on your hard-disk, so not a file in that sense. So in what sense, everything looks like a file. Everything has a file interface. In the same way that (nearly) all motor vehicles have the same controls: steering wheel, peddles in the same place, same-ish gear stick. – ctrl-alt-delor Apr 20 '17 at 16:12
  • "everything is a file" is a meaningless gimmick – curiousguy Nov 29 '18 at 12:28

2 Answers2

7

Everything may be a file, but not everything is real.

Consider the contents of /proc. On my Linux system, there is a file /proc/uptime, whose current contents are:

831801.89 1241295.64

If I were to cat the file again, it would contain different numbers. My hard drive is mounted read-only, so it can't possibly be the case that something is writing these numbers to disk every fraction of a second. In fact, nothing under /proc is on disk. Each interaction with a file in that directory simply runs kernel code, due to the nature of procfs.

Then there are temporary files. Chances are, your /tmp is mounted tmpfs, meaning its contents are stored in RAM instead of on disk.

Another interesting place is /dev/tcp, for communicating with the network. On some systems, this only even exists under bash but not other shells, so it can't possibly be on-disk in those systems.

These examples all show that the filesystem and the hard drive are separate, and the "Everything is a file" philosophy does not impact performance on account of I/O speed.

Fox
  • 8,193
1

No I wouldn't say there's a penalty in terms of i/o.

Maybe the phrase is an over simplification. But within the kernel devices, files, everything can be referred to with a file descriptor, and all the descriptors are mapped onto a virtual files system, and as with a file system one can refer to a particular file with a name (a handle).

So the kernel wouldn't necessarily pass for example the data of /proc/vmstat through the disk file system, however you don't have to do anything special to read the statistics, you can read the data using the cat command.

try

watch head /proc/vmstat

You will see the numbers change every interval.

Obviously some files have special characteristics and might require further operations to manipulate than the simple open/close/read/write/seek eg setting the baud rate on a serial device makes sense, but not on a file on disk.

X Tian
  • 10,463