"Everything is a File"

From COMP15212 Wiki
Depends on UserFilesConcepts

This is sometimes said of Unix resources. Whilst this is a simple (and sometimes useful) paradigm, it is not entirely true. However it is often useful to direct data to/from/between processes and devices in a standardised way and think of these as “files”. Characters can be picked up from a keyboard or a genuine disk file or some other new device and treated identically.

“Everything is a file descriptor.” [e.g. Wikipedia, quoting Linus Torvalds] is a bit closer although the term “file descriptor” is more usually used to refer to a particular opening of a file rather than being unique to the file.

Perhaps “Lots of things in Unix use i-nodes and appear in the file system.” is even closer, if not as catchy.

In practice there is a lot of commonality of functionality in use. An I/O device may be able to supply or consume a stream of bytes and may require particular access permissions – just like a file.

Here are the standard i-node types:

Examples

In most parts of a Unix file system you are likely to find only ‘regular’ files and directories – maybe with occasional links scattered here and there. There is an exercise to help you create your own pipe.

To see a selection of devices-looking-like-files, try the /dev directory. Here are some examples (which will have been set up by the last boot) taken from the ‘kilburn’ server.

Link

0 lrwxrwxrwx   1 root root            13 Nov 20 03:18 fd -> /proc/self/fd

Character device

Here are some sample character devices, masquerading as “Teletypes” – i.e. computer terminals.

 0 crw--w----   1 root tty         4,   0 Nov 20 03:18 tty0
 0 crw-rw-rw-   1 root dialout     4,  64 Nov 20 03:18 ttyS0
 0 crw-rw-rw-   1 root root        1,   3 Nov 20 03:18 null
 0 crw-rw-rw-   1 root root        1,   8 Nov 20 03:18 random
 0 crw-rw-rw-   1 root root        1,   9 Nov 20 03:18 urandom

In this case tty0 will be a virtual device corresponding to a notional keyboard/screen window pair; there will be many ‘Teletypes’ created on a server with multiple users.

ttyS0 has a similar behaviour but is actually a hardware serial port on the machine. This particular interface has been given read/write permission by any user, which means it would be accessible directly by a user application. (We use this feature in various labs. where the desktop workstations connect to other equipment.)

/dev/null is not a device at all: it is a “black hole” which consumes anything entering without trace. This is quite useful if something is generating output which you don’t want; it gives it somewhere to go from which it will not return. Anyone can write to (or read from!) it.

random and urandom are sources of notionally random data. The latter is a ‘straightforward’ pseudo-random number generator whereas the former can try to pick up genuinely random (unpredictable) noise from some hardware source in the machine (which could not be duplicated in pure software).

Block device

Disks are block devices in that it is only useful to move data in sizeable blocks. Linux uses “sd” for “SCSI Device” (even though modern disks are unlikely to use a SCSI interface) and a letter to indicate individual identities. Thus sda is “Disk A”; this is somewhat like Windows “C:”, etc. The subsequent numeric suffix indicates a further logical partition on that device.

 0 brw-rw----   1 root disk        8,   0 Nov 20 03:18 sda
 0 brw-rw----   1 root disk        8,   1 Nov 20 03:18 sda1

Windows

Windows (and other operating systems) have adopted a similar convention by supporting device files using keywords rather than implanting the ‘virtual files’ in a unified system.



Articles on User
"Everything is a File" • Application Binary Interface (ABI) • Arrays • Boot • Buffer Overflow • Containers • Daemons • Disk Partition • Dynamic Memory Allocation • Emulator traps • Environment Variables • Errors • Exceptions • File Attributes • File Locking • File Permissions • Introduction to Operating Systems • Journalling File System • Links • Locks • Man(ual pages in Unix) • Memory Mapped Files • Monitoring • Network File System (NFS) • PATH • Pipes • Pointers • Relocatable Code • Reset • SETUID • Shell • Sockets • Spooling and Buffering • Streams • Structures • Superuser • System Calls • Unix Signals • User • Using Peripherals