-1

I am a newbie to Linux and while exploring around file system - I quite often encounter the phrase "Everything is a file". I do see an answer to this question here but I am still failing to grasp the concept. In the answer it is mentioned that precisely "Everything is a stream of bytes"

I am not getting what it means by saying monitor/keyboard etc. is represented as "stream of bytes". Can someone help me to visualize this?

2 Answers2

2

"Everything is a file" is slogan, so to say. A meme, if you will. It's not exactly true.

Obviously hardware devices are not actual files in the same way as a hello.txt on an ext4 filesystem would be. Many of them don't even have anything to do with storage devices.

But even though not everything is a file, a number of things can be accessed as if they were files, at least somewhat. That is, they have a name in the filesystem tree, and the read() and write() system calls can be used for them. The latter is especially important if we consider the sense that "everything is a byte stream", since those calls access byte streams.

Since the same system calls work for regular files, raw disk devices, terminals, network sockets and pipes, the same set of tools can be used with each. E.g. echo foo works the same regardless of where the output is connected. That can simplify the utility implementations, and makes it easier to apply them for new purposes. A shell or another command line utility does not need to do anything special to be connected through a pipe or a socket to an SSH server and accessed over the network. (Though for interactive sessions, SSH creates a pseudo-terminal, so it's not that different from a local session.)

Also, when a number of things have presence in the filesystem, they can be listed with ls and accessed with cat and shell redirections (echo foo > ...). This can be useful for accessing special files in /proc or /sys from scripts, without needing a special binary to make a less-common system call.

Still not everything is a file, or a stream of bytes, and for things that are, not everything can be done with read() and write(). UDP sockets are not byte streams, as they transmit multi-byte datagrams with fixed sizes. (Though read() and write() still work on them, just a bit differently.) Some actions still need to be done with the ioctl() system call, and the ioctl operations are device-specific. On Linux, network devices don't have names in the filesystem.

See also e.g.

ilkkachu
  • 138,973
1

Everything in a UNIX / Linux system can be visualized as some sort of input or output device.

A disk is both - you either write a stream of bytes to it, or you read a stream of bytes from it. A keyboard is an input device - you read a stream of bytes from the keyboard.

Interpreting this stream of bytes is up to the program currently using the keyboard - whether it's a shell, a user program, a system program, or something else. The terminal is simply a stream of bytes being written to - STDOUT or STDERR.

The analogy breaks down somewhat when you're talking about graphical consoles, but within each terminal window it holds up. That terminal window is just something that waits for bytes to be sent to it by the shell / program, and displays those bytes in the form of text or other ASCII characters to the user.

As has been pointed out in comments to this answer, the analogy breaks down even further as you get deeper into the system. The initial phrase "Everything is a file" and the related phrase "Everything is a stream of bytes" seems to have been coined to simplify the view of the system for those who are new to it or who have limited experience with it. As you gain experience and get more involved with Linux, you'll realize that this simplification is sometimes incorrect, but it still holds value for those just becoming familiar with the environment.

John
  • 17,011
  • 1
    "The monitor is simply a stream of bytes being written to - STDOUT or STDERR." -- no, it isn't. And you said that yourself in the very next sentence. The terminal (either one emulated by GUI software, or one provided by e.g. the kernel for the text-based interface) can be accessed through a byte stream, but that's not the same as the monitor. – ilkkachu Nov 01 '21 at 13:21
  • 1
    This is wrong when applied to far too many things. Some things in UNIX/Linux can be regarded as bytes of streams but there are tons of exceptions. – Artem S. Tashkinov Nov 01 '21 at 13:34
  • @ArtemS.Tashkinov, streams of bytes... – ilkkachu Nov 01 '21 at 13:37
  • “Everything is accessible through a file descriptor” is about as good as it gets, rather than just “a file” or “a stream of bytes”. – Stephen Kitt Nov 01 '21 at 15:15
  • 1
    On UNIX most of interaction between processes are done with bytes streams (read, write syscall) : TTY between a shell and its terminal, some X11 programs with their X11 server, etc.

    However, there are other interfaces, typically shared memory pages which makes a program write some bytes in some pages and use some other synchronisation object (semaphore…) to tell something is new in the shared pages. X11 uses now some shared memory pages to make buffers exchanges more efficient.

    – Frédéric Loyer Nov 01 '21 at 15:48