Linux.com :: Everything Linux and Open Source

来源:百度文库 编辑:神马文学网 时间:2024/04/27 14:40:12
Overview of How Terminals Work(in Linux)
See also sectionSome Details onHow Terminals Work
Each terminal is connected to a serial port on the host computer(often just a PC). The ports have names/numbers. The first feware: ttyS0, ttyS1, ttyS2, etc.
These are represented by special files found in the /dev (device)directory. ttyS0) corresponds to COM1 in DOS or Windows. ttyS1)is COM2, etc. SeeTerminal Special Filesfor details on these and related "devices".
Login/Logout
When the host computer starts up it runs the program getty. Thegetty program runs the "login" program to log people in. SeeGetty (used in/etc/inittab). A "login:" prompt appears on the screen.People at the terminals log in (after giving their passwords) andthen have access to the computer. When it‘s time to shut theterminal down, one normally logs out and turns the terminal off.SeeLoginRestrictions regarding restricting logins (including allowingthe root user to log in at terminal).
If one watches someone typing at a terminal, the letters onetypes simultaneously appear on the screen. A naive person mightthink that what one types is being sent directly from thekeyboard to the screen with a copy going to the computer(half-duplex like, see next paragraph). What is usually going onis that what is typed at the keyboard is directly sent only tothe host computer which in turn echoes back to the terminal eachcharacter it receives (called full-duplex). In some cases (suchas passwords or terse editor commands) the typed letters are notechoed back.
Full-duplex means that there are two (dual) one-way communicationlinks. Full-duplex is the norm for terminals. Half-duplex is halfof a duplex, meaning that there is only a single one-waycommunication link. This link must be shared by communicationsgoing in both directions and only one direction may be used at atime. In this case the computer would not be able to echo thecharacters you type (and send to it) so the terminal would needto also send each character you type directly to the terminalscreen. Some terminals have a half-duplex mode of operation whichis seldom used.
Terminal Memory
The image on a CRT tube will fade away almost instantly unless itis frequently redrawn on the screen by a beam of electrons shotonto the face of the tube. Since text sent to a terminal needs tostay on the screen, the image on the screen must be stored in thememory chips of the terminal and the electron beam mustrepeatedly scan the screen (say 60 times per second) to maintainthe image. SeeTerminal MemoryDetails for more details.
Commands for the Terminal
The terminal is under the control of the computer. The computernot only sends the terminal text to display on the screen butalso sends the terminal commands which are acted on. These areControlCodes (bytes) andescape sequences. Forexample, the CR (carriage return) control code moves the cursorto the left hand edge of the screen. A certain escape sequence(several bytes where the first byte is the "escape" control code)can move the cursor to the location on the screen specified byparameters placed inside the escape sequence.
Thefirstterminals had only a few such commands but modern terminalshave hundreds of them. The appearance of the display may bechanged for certain regions: such as bright, dim, underline,blink, and reverse video. A speaker in a terminal can "click"when any key is pressed or beep if a mistake has occurred.Function keys may be programmed for special meanings. Variousfonts may exist. The display may be scrolled up or down.Specified parts of the screen may be erased. Various types offlow control may be used to stop the flow of data when bytes arebeing sent to the terminal faster than the terminal can handlethem. There are many more as you will see from looking over anadvanced terminal manual or from the Internet linksEsc Sequence List
Lack of Standardization Solvedby Terminfo
While terminals made for the US all used the same ASCII code forthe alphabet (except for IBM terminals which used EBCDIC), theyunfortunately did not all use the same escape sequences. Thishappened even after various ANSI (and ISO) standards wereestablished since these standards were never quite advancedenough. Furthermore, older terminals often lacked thecapabilities of newer terminals. This might cause problems. Forexample, the computer might send a terminal an escape sequencetelling it to split the screen up into two windows of specifiedsize, not realizing that the terminal was incapable of doingthis.
To overcome these problems a database called "termcap" (meaning"terminal capabilities") was established. Termcap was latersuperceded by "terminfo". This database resides in certain fileson the computer and has a section of it (sometimes a separatefile) for each model of terminal. For each model (such as VT100)a list of capabilities is provided including a list of certainescape sequences available. For example blink=\E5m means that tomake the cursor start blinking the terminal must be sent: Escape5 m. See SectionTermcap and Terminfo(detailed) for more details. Application programs may utilizethis database by calling certain C-Library functions. One largeset of such programs (over 200) is named "ncurses" and are listedin the manual page for "ncurses" which comes with a developer‘sncurses package. There is also a NCURSES-programming-HOWTO.
The Interface
The environment variable TERM is the type of terminal Linuxthinks you are using. Most application programs use this to lookup the capabilities in the terminfo database so TERM needs to beset correctly. But there is more to a correct interface than thecomputer knowing about the capabilities of the terminal.
For bytes to flow from the computer to the terminal the terminalmust be set to receive the bytes at the same baud rate (bits persecond) as they are sent out from the terminal. If the terminalis set to receive at 19,200 baud and the computer sends outcharacters at 9600 baud, only garbage (or perhaps nothing) willbe seen on the screen. One selects the baud rate for a terminal(as well as many other features) from the terminals "set-up"menus at the terminal. Most terminals have a large number ofoptions in their "set-up" menus (seeTerminal Set-Up(Configure) Details). The computer serial port has optionsalso and these options must be set up in a compatible way (seeComputerSet-Up (Configure) Details.
Emulation
Most terminals today have more than one emulation (personality or"terminal mode"). The terminal model numbers of terminalsformerly made by DEC (Digital Equipment Corporation now Compaq)start with VT (e.g. VT100). Many other terminals which are notVT100 may be set up to emulate a VT100. Wyse is a major terminalmanufacturer and most of their terminals can emulate various DECterminals such at VT100 and VT220. Thus if you want to, say, usea VT320 terminal you may either use a real VT320 in "native"personality or possibly use some other terminal capable ofemulating a VT320.
The "native" personalities usually have more capabilities so,other things being equal, "native" is usually the best to use.But other things may not be equal. Since the Linux consoleemulates a VT102 it you may want to have a terminal emulate this(or something close to it such as VT100). This will help insurethat some programs that may not handle terminals properly willstill work OK on your terminal. Some programs will assume thatyou are using a VT102 if the program can‘t find a terminfo foryour terminal (or can‘t find a certain capability). Thus thefailure of a program to work correctly with your non-vt102terminal may well be your fault if you don‘t provide a goodterminfo file for your terminal. Using "native" and thenreporting any bugs will help discover and fix bugs which mightnot otherwise get detected.
The most common type of emulation is to use a PC like it was avt100 terminal (or the like). Programs loaded into the PC‘smemory do the emulation. In Linux (unless you‘re in X Window) thePC monitor (called the console) emulates a terminal of type"Linux" (close to vt100). Even certain windows within X Windowemulate terminals. SeeTerminalEmulation.
The Console
On a PC, the monitor is normally the console. It emulates aterminal of type "Linux". One logs on to it as a virtualterminal. SeeThe Console. Itreceives messages from the kernel regarding booting and shutdownprogress. One may have the messages that normally go to theconsole, go to the terminal. To get this you must manually patchthe kernel, except that for kernel 2.2 (or higher) it is a "makeconfig" option. SeeMake a SerialTerminal the Console.