Main index

Introducing UNIX and Linux


Processes and devices

Overview
Processes
      Process status
      Foreground and background
      Process control
      Signals
Environment
      Environment variables
      Global and local variables
      Executable scripts
Program control
      Job control
      Command history list
      Running a job at a specific time
      Running programs periodically
      Big programs
      Timing a program
      Running programs in order
Quotes and escapes
Devices
Backquotes
Summary
Exercises

Devices

A device is any piece of equipment connected to a computer system which performs communication between 'the outside world' and the system, such as a printer or a terminal. Although normally hardware, a device might be software that behaves, from the perspective of UNIX, in the same way as hardware. When you have run commands that use input and output streams, their behaviour as 'streams of characters' does not depend upon where they originate or are directed to. They are simply streams of characters. It does not matter whether input comes from a terminal or from a file, nor whether output is piped to a printer, sent to the terminal, or redirected to a file. In fact, UNIX treats devices exactly the same as files.

From the perspective of a UNIX programmer, every device is a file.

Type tty to discover what the name of your current terminal (or window) is (say, ttyp9). Now change directory to /dev ('device') and use ls to examine which files are in it. You will find a very large number of files. Look closely at file ttyp9 (or the name of the terminal you are using instead of ttyp9):

ls -l ttyp9
crw--w--- 1 chris tty 20, 2 Mar 26 12:02 ttyp9

This looks very much like an ordinary file, except that the first character in the output of ls -l is the character c, indicating that the file is a character special file, the technical jargon used to describe a device such as a terminal. You own the file, and can write to it. Try:

date >ttyp9

and the date will appear on your screen just as if you had typed date on its own.

Every device has a filename in the directory /dev.

If you attempt to write to a device owned by another user, it won't work. If you have several windows on your terminal you will normally be allowed to write to other windows - use tty to discover their names, and then try the above example with one of them. There is also a file in /dev called tty which is always a synonym for the current terminal's filename - so

date >tty

would produce the same output as above.

Standard input is received from, and standard output and standard error are sent to, the file that is your terminal, unless you redirect them elsewhere. They are not files, they are simply concepts to enable redirection of streams to take place. At the ends of pipelines, unless these streams are redirected, they are automatically directed at /dev/tty. Thus

sh myprogram

is equivalent to

sh myprogram 0/dev/tty 2>/dev/tty

Note that you can overload a device and direct input and/or more than one output at it

Other devices you may encounter include /dev/audio, if your terminal has a loudspeaker and microphone, /dev/console if you are using a workstation, and devices with names similar to /dev/rst8 if you ever need to use a magnetic tape drive. There is, however, one device you will need, and will have to use by name. Suppose you have written a program that outputs diagnostic messages as well as its output, and you wish to view only the output. You could send the standard error stream to a file:

sh myprogram 2>test.errors

but this would be wasteful of filespace. You can discard this stream by redirecting it to a file (device) known as /dev/null:

sh myprogram 2>/dev/null

This file behaves in the same way as any other file or device, but it simply junks any output sent to it, and if you try to read from it it is always at end-of-file. Use /dev/null with care, and only when you know that you want output discarded.

Worked example

Write a script to read in the name of a file and display a message only if it cannot be read.
Solution: Use cat to read the contents of the file. If cat fails, the file is unreadable, and the error message, sent to standard error, should be displayed. However, we do not actually wish to see the file's contents, so junk them by directing the standard output to /dev/null:

echo Type the name of a file:
read FILENAME
cat $FILENAME >/dev/null


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck