thoughts by ch3ngl0rd ᕦʕ •ᴥ•ʔᕤ

Everything is a file..?

#noticingconfusion

I've started the 1 million sudoku challenge recently, and I wanted to start off with some new theoretical knowledge to help ground the stuff that I'm learning. I realised that file input output would have an impact on the speed of my solution (from reading the one billion row challenge), and hence started to research more about files to understand them better.

One thing that surprised me was the idea that all input output in Unix are modelled as files. My expectation was that normal files could be opened, read to and edited, however things like sockets and (other stuff i haven't researched yet) would be handled different. Yet this is not the case.

In UNIX, a file descriptor is pretty much anything. You could say that sockets aren't remotely file-like, and you'd be right. What's your point? If you can read on it, it's a file. https://yarchive.net/comp/linux/everything_is_file.html

Some questions:


  1. What is a file?

Instead of thinking of things as purely files, we abstract a file into 4 operations:

A traditional file (I'm thinking like text file here) satisfies these operations. I can open a txt file, write "hello world" to it, maybe read what was already in it, and then close it. But with some creativity, I can imagine opening a connection to a server, writing to it (send some requests), read from it (server responds) and then closing it (ending the connection). So they are file-like.

  1. Why is everything a file?

Many things are file-like. Network connections, keyboard inputs and trad txt files. So the APIs are simplified by abstracting everything as a file for a common interface between different tasks. When opening a file-like object, a file descriptor is returned.

"File descriptors typically have non-negative integer values, with negative values being reserved to indicate "no value" or error conditions."

https://en.wikipedia.org/wiki/File_descriptor

They are process-unique identifiers for a file-like entity.

thoughts by ch3ngl0rd ᕦʕ •ᴥ•ʔᕤ