Chapter 1. General Unix and Advanced C

1  Everything is a file!

An often-quoted tenet of UNIX-like systems such as Linux or BSD is everything is a file.

Imagine a file in the context of something familiar like a word processor. There are two fundamental operations we could use on this imaginary word processing file:

  1. Read it (existing saved data from the word processor).

  2. Write to it (new data from the user).

Consider some of the common things attached to a computer and how they relate to our fundamental file operations:

  1. The screen

  2. The keyboard

  3. A printer

  4. A CD-ROM

The screen and printer are both like a write-only file, but instead of being stored as bits on a disk the information is displayed as dots on a screen or lines on a page. The keyboard is like a read only file, with the data coming from keystrokes provided by the user. The CD-ROM is similar, but rather than randomly coming from the user the data is stored directly on the disk.

Thus the concept of a file is a good abstraction of either a sink for, or source of, data. As such it is an excellent abstraction of all the devices one might attach to the computer. This realisation is the great power of UNIX and is evident across the design of the entire platform. It is one of the fundamental roles of the operating system to provide this abstraction of the hardware to the programmer.

It is probably not too much of a stretch to say abstraction is the primary concept that underpins all modern computing. No one person can understand everything from designing a modern user-interface to the internal workings of a modern CPU, much less build it all themselves. To programmers, abstractions are the common language that allows us to collaborate and invent.

Learning to navigate across abstractions gives one greater insight into how to use the abstractions in the best and most innovative ways. In this book, we are concerned with abstractions at the lowest layers; between applications and the operating system and the operating system and hardware. Many more layers lie above this, each worthy of their own books. As these chapters progress, you will hopefully gain some insight into the abstractions presented by a modern operating system.

Can you spot the difference between a CD-ROM and a keyboard? As an application programmer, you should not be able to.
Figure 1.1 Abstraction