Why is "Everything is a file" unique to the Unix operating systems?

22,346

Solution 1

So, why is this unique to Unix?

Typical operating systems, prior to Unix, treated files one way and treated each peripheral device according to the characteristics of that device. That is, if the output of a program was written to a file on disk, that was the only place the output could go; you could not send it to the printer or the tape drive. Each program had to be aware of each device used for input and output, and have command options to deal with alternate I/O devices.

Unix treats all devices as files, but with special attributes. To simplify programs, standard input and standard output are the default input and output devices of a program. So program output normally intended for the console screen could go anywhere, to a disk file or a printer or a serial port. This is called I/O redirection.

Does other operating systems such as Windows and Macs not operate on files?

Of course all modern OSes support various filesystems and can "operate on files", but the distinction is how are devices handled? Don't know about Mac, but Windows does offer some I/O redirection.

And, compared to what other operating systems is it unique?

Not really any more. Linux has the same feature. Of course, if an OS adopts I/O redirection, then it tends to use other Unix features and ends up Unix-like in the end.

Solution 2

The idea that "everything is a file" came from Multics. The designers of Unix based a lot of their work on predecessors, especially Multics. Indeed, a lot of things in computing are based on predecessors.

You can read up on the late Dennis Ritchie's work in the design of Unix for more. He referenced things that they "copied" from Multics, such as the tree-like file system, the command shell, and non-structuring of files. I'm not implying that the Unix folks stole from the Multics folks. For all intents and purposes, it was the same folks.

Solution 3

Unique? No. Defining? Absolutely.

Having everything as a file or a device in a known hierarchy means you can use the same set of tools for everything. Plan 9 from Bell Labs takes this further with even hardware devices as files.

More importantly, this allows for two very simple and powerful concepts. Basic utilities that do One Thing Well (tm), which can be strung together with pipes as needed. Want to find something in a text file? Use cat to show it, pass it through grep, and you're cooking with gas. That's the real power of the 'Unix' way - specialised applications working together for massive amounts of flexibility.

Mac OS X also follows the Unix philosophy, but it's better hidden (an 'application' bundle is really a directory full of files), and in fact is a proper, certified Unix, descended from NeXT, which used bits of FreeBSD.

With Windows, there are some binary components such as event viewer and registry since, and there's some speed advantages there, in that particular scenario.

Solution 4

Because of the special files. When people say "everything is a file in Unix", common files and directories are not what they have in mind. Special files are unique to Unix-like OSes, of which there are many. So it is not unique to the Unix.

Special files serve many purposes. There are e.g. pipes, sockets and, most notably, device files. Pipes and sockets are communication streams between processes. Much of the functionality of the subsystems is made available to the user space through device files.

Pipes and Sockets

Programs use them just as they'd use ordinary files. In fact, most of the time they don't even care what type of file they use. That's why Unix commands can be so manifoldly combined to form powerful new systems. (See I/O redirection in sawdust's answer)

Device Files

As previously mentioned these act like interfaces for the user space. For example, in order to eject the cd tray, a programmer would at first open the corresponding device file. Another example: you want your program switch the virtual terminal. Open /dev/console first.

What happens next is not sending mere characters to those files, but issuing ioctl()'s on them. The individual ioctl's you can issue depend on the device. E.g. the console is documented in console_ioctl(4)

Solution 5

I am probably going to get reamed for saying this, but I think that saying that everything is a file in Unix is in fact a fallacy. What it is really is two things.

  1. Files and devices (and a lot of other stuff) are objects that can be modeled by an interface that comprises of open, close, read, write, and control (ioctl) functions.
  2. The namespace for these objects is hierarchical i.e. these objects are organized in a hierarchy.

A filesystem implements this namespace, and implements the framework that allows the dispatch of interface functions to these objects. A filesystem was first conceptualized to house files, but was then co-opted to organize other objects in the namespace hierarchy. An example of polymorphism from before object oriented was a thing.

There is no harm in just calling everything files. But in reality, they are these more generic objects (a file being one such object). From this perspective, this idea is not unique to Unix at all. A lot of other OSes implement such hierarchies of polymorphic objects.

Share:
22,346

Related videos on Youtube

Some Noob Student
Author by

Some Noob Student

Just Some Noob Student. ( ̄(工) ̄) I ask questions that are dumb and can't distinguish a good answer. ╮(╯▽╰)╭ So please forgive me if I didn't choose your answer as the best answer. o(〒﹏〒)o But I'll make it up to you with upvotes. ‧★,:*:‧\ ( ̄▽ ̄) /‧:‧°★

Updated on September 18, 2022

Comments

  • Some Noob Student
    Some Noob Student almost 2 years

    I often hear people say "Unix's unique philosophy is that it treats everything as a file" or "In Unix, everything is a file". But I've never heard anyone explain why it is unique to Unix.

    So, why is this unique to Unix? Does other operating systems such as Windows and Macs not operate on files?

    And, is it unique compared to other operating systems?

    • akira
      akira over 12 years
      sidenote: macosx runs ontop of a unix.
    • user1686
      user1686 over 12 years
      Compared to ... VMS, perhaps?
    • Mark
      Mark over 12 years
      fwiw, while higher-level stuff doesn't use files (just like dbus or X in *nix) low-level Windows is built around handles. You can do a lot of the same with them as you can files on *nix (open, close, control, and usually send/receive), but you need to be a programmer to see that interface. Whether that's a good thing is a matter of preference.
    • alvin
      alvin over 12 years
      everything is a file, except network interfaces.
    • Rob
      Rob over 12 years
      @akira - OSX is certified Unix and doesn't run "on top of" it.
    • Wermerb
      Wermerb over 12 years
      @akira What is mac? Private joke
    • ewanm89
      ewanm89 over 12 years
      @alvin well, sockets themselves can indeed be files. The whole interface is a little more complicated so is often several files behind the scenes
    • alvin
      alvin over 12 years
      @ewanm89, i was thinking of eth0 which does not appear as a file in linux. i am not sure about this behaviour in unix though. if not, my bad.
    • Šimon Tóth
      Šimon Tóth over 12 years
      @akira Not on top of a Unix. OSX is one of the few certified UNIX OS.
    • ewanm89
      ewanm89 over 12 years
      @Let_Me_Be it depends precisely what one counts as Unix, if it's just kernel and coreutils in OSX that darwin or FreeBSD and gnu coreutils. That is the only part that is POSIX compliant and the rest. Technically anything else is not UNIX but running on top, we could throw in sysv init at a stretch. But this is pretty pedantic kind of reasoning.
    • Šimon Tóth
      Šimon Tóth over 12 years
      @ewanm89 Technically only operating systems that have UNIX certification are UNIX.
    • ewanm89
      ewanm89 over 12 years
      No, certified POSIX compliant parts only, all the fluff on top (X Windows/Quartz, DE, WM... is not part of UNIX).
  • slhck
    slhck over 12 years
    OS X basically is Unix, so there's not much of a difference.
  • Danny Staple
    Danny Staple over 12 years
    Plus 1 for mentioning plan 9. IMHO Fuse brings plan 9 closer to an implementation through ad-hoc and iteration.
  • Thalys
    Thalys over 12 years
    Well, plan 9 is plan 9, it sorta works already. Fuse just allows userspace filesystem expansion in.. interesting ways. I like the one where IRC is treated as a FS.
  • Lie Ryan
    Lie Ryan over 12 years
    this shouldn't be the highest voted answer, it does not answer the question, it just begs people to ask another question: "Why is 'Everything is a File' in Multics?"
  • Goran Jovic
    Goran Jovic over 12 years
    @LieRyan: Actually it does answer the question why is it unique: it isn't.
  • Danny Staple
    Danny Staple over 12 years
    Hmm - you know, I've been meaning to look it up for ages since finding out through an Eric Raymond book years ago... You've actually finally got me over the "get round to it" hump, I've downloaded the ISO and trying it out.
  • Thalys
    Thalys over 12 years
    I believe there's a newer version too, called inferno
  • Fiasco Labs
    Fiasco Labs over 12 years
    The Unix folks just took their ideas with them when the Multics project was discontinued and renamed it. Unix just being a play on words - one VS many.
  • Patrick Seymour
    Patrick Seymour over 12 years
    @Lie Ryan - The question was not why is everything a file, it's why is it unique. As Goran and I pointed out, it's not.
  • Patrick Seymour
    Patrick Seymour over 12 years
    The opening paragraph simply isn't true. There were operating systems before Unix, and they had features later found in Unix, including I/O redirection.
  • William
    William over 12 years
    Plan 9 has the same concept Everything is a file concept and has even been called "more unix then unix"
  • calum_b
    calum_b over 12 years
    +1 for noting that OS X's Darwin is a 'real Unix' too.
  • MSalters
    MSalters over 12 years
    Windows (since NT) goes a bit futher. There are lots of devices that behave like files, such as serial ports and named pipes. You can write to all using WriteFile. But in addition, there's an even bigger category of "synchronization objects" that includes not just files but also mutexes.
  • MSalters
    MSalters over 12 years
    The question really refers to the "Unix operating systems", i.e. the family. That family reasonably includes Multics and Linux.
  • bgvaughan
    bgvaughan over 12 years
    You didn't provide a link. On some searching, I found yarchive.net/comp/linux/everything_is_file.html, quoting Torvald's responses in an email thread, in which he implicitly affirms that the model should be "everything is a file."
  • Šimon Tóth
    Šimon Tóth over 12 years
    @slhck Unlike Linux, OS X is a certified UNIX OS.
  • Greg A. Woods
    Greg A. Woods over 8 years
    In Multics everything is just memory (RAM), except I/O (though I/O can also be (usually) transparently mapped onto memory too). In Unix everything is a file descriptor (not necessarily a file!), i.e. everything (except memory) is I/O. In Multics every file is directly addressable with full random access no different from memory, but in Unix every file (and every device and every network connection) is an I/O stream.
  • Greg A. Woods
    Greg A. Woods over 8 years
    Exactly! In Unix everything is a file descriptor (not necessarily a file!), i.e. everything (except memory) is I/O, even files. Files must be accessed as if they are I/O streams (though they are seek-able, so somewhat randomly accessible, but that the extreme cost of extra system calls). (with the exception of what little can be done with mmap() of course....)
  • Greg A. Woods
    Greg A. Woods over 8 years
    Well, in Multics everything is memory, except I/O (though that can be mapped onto memory too). In Unix everything (except memory) is a file descriptor, including files. I.e. in Unix everything is I/O.
  • Greg A. Woods
    Greg A. Woods over 8 years
    BTW, an emulator now exists that will boot and run Multics.
  • ninjalj
    ninjalj almost 7 years
    @LIttleAncientForestKami: and Unix traditionally treats files as simply streams of bytes, as opposed to systems that treated files as streams of records (or had both streams of bytes and streams of records), as collections of streams (or resource forks), or had more complex models, where there were several different ways to access several different kinds of objects.