很有趣的考证:Linux的终端下,键入Ctrl+S后如何恢复

在终端操作linux时候,由于一些可悲的习惯,会不自觉的键入Ctrl+S。终端就会锁死……只能键入Ctrl+Q来重新激活终端。

心血来潮,查查看为何如此~得到结论在此

Ctrl-Q is indeed the answer. I thought I’d toss in a little history of this that is too long to fit in the margins of ak2’s correct answer.

Back in the dark ages, a terminal was a large piece of equipment that connected to a remote device (originally another terminal because teletypes were so much easier to learn to operate than a telegraph key) over a long wire or via phone lines with modems. By the time Unix was developing, the ASCII code was already well established (although the competing EBCDIC code from IBM was still a force to be reckoned with).

The earliest terminals kept a printed record of every character received. As long as the characters arrived no faster than the print head could type them, at least. But as soon as CRT based terminals were possible, the problem arose that only about 25 lines fit on the CRT, and 25 lines of 80 characters represented enough RAM that no one thought seriously about providing more RAM for characters that had scrolled off the top of the screen.

So some convention was needed to signal that the sending end should pause to let the reader catch up.

The 7-bit ASCII code has 33 code points devoted to control characters (0 to 31 and 127). Some of those had really well established purposes, such as NUL (blank paper tape leader for threading, gaps, and splices), DEL (“crossed out” characters on paper tape indicated by punching all seven holes), BEL (ding!), CR, LF, and TAB. But four were defined explicitly for controlling the terminal device itself (DC1 to DC4 aka Ctrl+Q, Ctrl+R, Ctrl+S and Ctrl+T).

My best guess is that some engineer thought that (as mnemonics go), “S” for “Stop” and “Q” for “Continue” weren’t too bad, and assigned DC3 to mean “please stop sending” and DC1 to mean “ok, continue sending now”.

Even that convention was already well established by the time Unix was leaving nest at Bell Labs to go out into the world.

The convention is known as software flow control, and is extremely common in real serial devices. It is not easy to implement correctly, as it prevents the use of either of those characters for any other purpose in the communications channel, and the Stop signal has to be handled ahead of any pending received characters to avoid sending more than the receiving end can handle.

If practical, using additional signals out of band from the serial data stream for flow control is vastly preferred. On directly wired connections that can afford the additional signal wires, you will find hardware handshake in use, which frees up those characters for other uses.

Of course, today’s terminal window is not using an actual physical serial port, has scroll bars, and doesn’t really need software handshaking at all. But the convention persists.

I recall the claim that Richard Stallman received complaints about his mapping Ctrl+S to incremental-search in the first releases of emacs, and that he was rather unsympathetic to any user that had to depend on a 7-bit, software flow controlled connection.

蛮神奇的。哥接触最早的键盘也没这货(88年的IBM)……

看来,目前还没有讲解计算机历史掌故的资历哦~