I remember that one of the financial banking software we were using had an internal chat that was basically using "write"(?) underneath to send messages to another users terminal (it was running on Sun Solaris I think).
When I started college, all of our classes would assign a temporary (semester-long) account on one of the various Vax 11/780s supplied by the computing center.
Talk wasn't available yet (pretty sure we were on 4.1 BSD), so we'd use write(1) to communicate to each other (e.g., to figure out where someone was sitting in the lab). To block someone from writing to you (often desired, because write(1) would just spew over whatever you were currently looking at), you'd use the "mesg" command, which our University set as default to 'y'. I figured out that running 'mesg y' effectively just gave open write permission to your tty.
With that knowledge in hand, I started a practical joke where I'd remap someone's keys by redirecting an stty command to their tty, e.g.:
% stty erase e > /dev/tty03
which would make 'e' the backspace key for the duration of their terminal session. Much hilarity ensued.
It always annoyed me that the Unix terminal subsystem is too dumb to handle multiple writers to the same terminal at once without them potentially corrupting each other’s output. There are some techniques to reduce the incidence of this but none of them are foolproof, whereas a better design could be.
Maybe that was too hard when people used to use hardcopy terminals (actual ttys), although even there it possibly could have done better than it actually does.
But certainly by the time people had softcopy terminals with cursor positioning (like the DEC VT series and its emulators), a better experience could have been possible. For example, what if the functionality of curses was actually in the tty driver, so if another process wrote to the terminal it would appear as a new window, which could then be dismissed without altering the output of the underlying program? Or, to avoid putting too much in kernel space, the tty subsystem could live in a user-space daemon (possibly one process per terminal), and then applications would talk to it over IPC
>Or, to avoid putting too much in kernel space, the tty subsystem could live in a user-space daemon
On Unix, that's what "pseudo ttys" are for (i.e. /dev/pty*)!
For example, that's how Emacs lets you run multiple shell sessions in sub-processes, such that they have full job control (i.e. ^Z and ^C works to interrupt or stop sub-processes in the shell, since they're handled by the TTY). That's also how xterm and the Mac Terminal emulator work, providing the shell sub-process with its own pseudo tty with job control, even though there's not a corresponding /dev/tty* serial port driver. It's like a virtual serial port using a device driver with "TTY Line Discipline" but without an actual TTY.
ChatGPT correctly explains it better than I can or anything I can find with google:
Unix pseudo ttys, or pseudo-terminal devices, such as /dev/pty*, are a key mechanism in Unix and Unix-like operating systems for enabling communication between different processes. Pseudo-terminals are designed to provide the same interface and functionality as physical terminals or terminal emulators. They consist of a pair of devices, the master side (e.g., /dev/ptmx) and the slave side (e.g., /dev/pts/N), which are used to establish a bidirectional communication channel.
Pseudo-terminals are useful for running programs that expect to interact with a terminal, even when there is no actual terminal involved. This is particularly important for terminal-based applications like Emacs, which can run sub-shells within their own window or buffer.
Emacs, an extensible and highly customizable text editor, can leverage pseudo-terminals to run sub-shells, like bash or zsh, within its own environment. [Omitted detailed instruction on how Emacs sets up a pty for a sub-shell.]
By utilizing pseudo-terminals, Emacs can provide an integrated environment where users can work with both text files and interactive shell sessions seamlessly. This enhances productivity and enables users to harness the full power of Emacs' editing and navigation features while working with the shell.
> >Or, to avoid putting too much in kernel space, the tty subsystem could live in a user-space daemon
> On Unix, that's what "pseudo ttys" are for (i.e. /dev/pty*)!
No, that’s not what I was talking about. On just about every Unix, even with ptys, the line discipline code (or STREAMS modules for SysV-derived systems) still runs in kernel mode.
On Linux, you could implement something like what I was talking about with CUSE - have a character device which implemented the termios ioctls in a user-space daemon instead of in the kernel tty driver. That’s very different from how most Unix systems implement ptys
But what I was actually thinking about was a daemon which exposed over IPC an API a lot richer than termios. Something closer to curses.