How Linux uses /dev/tty and /dev/tty0

I can see the difference between /dev/tty and /dev/tty0 by testing the provided method from this question. But I really wonder about the practical usage of those devices (like situations they will be used).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

/dev/tty is the controlling tty of the current process, for any process that actually opens this special file. It isn’t necessarily a virtual console device (/dev/ttyn), and can be a , a serial port, etc. If the controlling tty isn’t a virtual console, then the process has not to interact with console devices even if its pseudotty is actually implemented on the system console. E. g. for a shell in a terminal emulator under locally-running X server, said programs form such chain of interactions as:

   Unix shell
     ⇕ /dev/pts/2 (≡ /dev/tty for its processes)
 kernel pty driver
     ⇕ /dev/ptmx
 terminal emulator
     ⇕ X Window protocol
   X server
     ⇕ /dev/tty7 (≡ /dev/tty for the server)
 system console
zxc↿⇂[_̈░░]
    user

Use of /dev/tty by userland programs includes:

  • Write something to the controlling terminal, ignoring all redirections and pipes;
  • Make an ioctl() – see tty_ioctl(4);
  • For example, detach from the terminal (TIOCNOTTY).

/dev/tty0 is the currently active (i. e. visible on the monitor) virtual console of the operating system. This special file unlikely is used significantly by system software, but /dev/console is virtually an “alias” for tty0 and /dev/console has much use by syslog daemons and, sometimes, by the kernel itself.

Experiment to show the difference: run a root shell on tty3 (Ctrl+Alt+F3) or in a terminal emulator. Now

# sleep 2; echo test >/dev/tty

then quickly Ctrl+Alt+F2, wait for two seconds, and Ctrl+Alt+whatever back. Where do you see the output?
And now the same test for /dev/tty0.

Method 2

/dev/tty is the controlling tty for any process. This may be your shell.

If your process does not have a controlling tty, /dev/tty is not available, this e.g. is true for daemons.

If your process has a controlling tty, then /dev/tty is a driver alias to the real tty driver your process is using for stdin, stdout or stderr.

See man -s7d tty for more information. Note that the section where this manual is in may differ between different OS and should not be confused with the man page in section 1. So check e.g. man -k tty.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x