If you fire up a terminal and call an executable (assuming one thats line oriented for simplicity) you get a reply to the command from the  executable. How does this get printed to you (the user) does the terminal do something like pexpect? (poll waiting for output ) or what? How does it get notified of output to be printed out? And how does a terminal start a program? (is it something akin to python's os.fork()? ) I'm puzzled how a terminal works, I've been playing with some terminal emulator and I still don't get how all this magic works. I'm looking at the source of konsole (kde) and yakuake (possibly uses konsole) an I can't get where all that magic happens.
How does a Linux terminal work?
terminal;tty;terminal emulator;architecture;pty
Originally you had just dumb terminals - at first actually teletypewriters (similar to an electric typewriter, but with a roll of paper) (hence /dev/tty - TeleTYpers), but later screen+keyboard-combos - which just sent a key-code to the computer and the computer sent back a command that wrote the letter on the terminal (i.e. the terminal was without local echo, the computer had to order the terminal to write what the user typed on the terminal) - this is one of the reason why so many important Unix-commands are so short. Most terminals were connected by serial-lines, but (at least) one was directly connected to the computer (often the same room) - this was the console. Only a select few users were trusted to work on the console  (this was often the only terminal available in single-user mode).Later there also were some graphical terminals (so-called xterminals, not to be confused with the xterm-program) with screen & graphical screen-card, keyboard, mouse and a simple processor; which could just run an X-server.  They did not do any computations themselves, so the X-clients ran on the computer they were connected to.  Some had harddisks, but they could also boot over the network.  They were popular in the early 1990s, before PCs became so cheap and powerful.A terminal emulator - the terminal-window you open with programs like xterm or konsole - tries to mimic the functionality of such dumb terminals.  Also programs like PuTTY (Windows) emulate terminals.With the PC, where the console (keyboard+screen) and the computer is more of a single unit, you got virtual terminals (on Linux, keys Alt+F1 through Alt+F6) instead, but these too mimic old-style terminals. Of course, with Unix/Linux becoming more of a desktop operating system often used by a singe user, you now do most of your work at the console, where users before used terminals connected by serial-lines.It's of course the shell that starts programs.  And it uses the fork-systemcall (C language) to make a copy of itself with a environment-settings, then the exec-systemcall is used to turn this copy into the command you wanted to run.  The shell suspends (unless the command is run in the background) until the command completes.  As the command inherits the settings for stdin, stdout and stderr from the shell, the command will write to the terminal's screen and receive input from the terminal's keyboard.