Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It was explained in this Stack Overflow thread that each logical terminal has a "pseudo-terminal", and that writing to one:

$ cat some-file.txt > /dev/ttys002

will cause the data to appear in that terminal window. What's the reason for providing a file-like API to terminal windows? Is there any use case where this is helpful?

Till here copied verbatim.

Not limited to pseudo-terminals, it is available to /dev/tty* as well.

share|improve this question
1  
I think it's not meant to be an API to the console, it's the other way round: a console's acess to ... the kernel, I suppose. But in *nix everything is a file so if you have the necessary permissions you can access it from any process. –  Bananguin Oct 13 '13 at 21:03

1 Answer 1

up vote 4 down vote accepted

Most hardware devices offer a file-like API. This is done because it makes both the design of the operating system and the design of applications simpler. The OS only has to have a file API and not a separate terminal API and a separate disk API and a separate sound API and so on. Applications that are not using features specific to a particular kind of hardware can use the file API without caring whether they are accessing a regular file or a hardware device.

A lot of hardware has capabilities that are specific to a type of device. Applications can invoke these capabilities via ioctl. Some hardware doesn't appear as files because you can't simply read or write a stream of bytes to it. For example Linux doesn't expose network interfaces as device files, because network interfaces work on packets, not on individual bytes.

Historically, terminals were hardware devices. Nowadays most terminals are provided by emulators, either in a graphical environment or over the network. Nonetheless even pseudo-terminals appear like hardware devices, because the kernel contains special handling to track which processes are running on which terminal.

On every unix variant, /dev/tty means “the current terminal for this process”. In other words, whenever a process opens that file, it designates the process's controlling terminal). This allows a process to interact via its terminal even when its standard input and output descriptors have been redirected.

Each terminal has an associated device file, which is either a hardware terminal (tty, for example /dev/tty1, /dev/tty2, … for the text mode virtual consoles on Linux, or /dev/ttyS0, … for serial ports on Linux) or an emulated terminal (pty, short for pseudo-terminal; /dev/pts/NUMBER on Linux). This is the file through which processes exchange data with the terminal driver or emulator.

It's because terminals are files that you can run applications and display their output to the terminal. When you run a program at the command line, by default, its output goes to the terminal, but you can redirect it to a file.

share|improve this answer
    
the part where you mention "the current terminal for this process". Could you elaborate "this" with "its terminal" in context? –  Bleeding Fingers Oct 16 '13 at 18:19
    
@hus787 I'm not sure I understand your question. See my edit, is that what you're asking? –  Gilles Oct 16 '13 at 19:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.