33

Does this concept only apply to terminal drivers (which is what most sites cover) or to any driver in general?

soc
  • 440

2 Answers2

41

The terms raw and cooked only apply to terminal drivers. "Cooked" is called canonical and "raw" is called non-canonical mode.

The terminal driver is, by default a line-based system: characters are buffered internally until a carriage return (Enter or Return) before it is passed to the program - this is called "cooked". This allows certain characters to be processed (see stty(1)), such as CtrlD, CtrlS, CtrlU, Backspace); essentially rudimentary line-editing. The terminal driver "cooks" the characters before serving them up.

The terminal can be placed into "raw" mode where the characters are not processed by the terminal driver, but are sent straight through (it can be set that INTR and QUIT characters are still processed). This allows programs like emacs and vi to use the entire screen more easily.

You can read more about this in the "Canonical mode" section of the termios(3) manpage.


Stephen Kitt
  • 434,908
Arcege
  • 22,536
  • A question: say the first process sets the keyboard driver to raw mode, the second process uses the default cooked mode. If both processes read keyboard presses as characters from stdin, does the keyboard driver continuously switch between raw and cooked mode to serve both processes? – mercury0114 Aug 09 '20 at 16:10
  • @mercury0114 I haven't tested it, but there would be one driver for the one device; whichever setting is last would persist until changed again. The two processes would have to coordinate. This isn't that different from vim/emacs switching to canonical mode just before forking a shell. Also, it could be non-deterministic which process would get the character (race condition) when both are waiting, but only once should return. – Arcege Aug 09 '20 at 19:59
  • @mercury0114 - on a trad TTY device, only one process is supposed to access the terminal as stdin/stdout at a time (not exactly, but sort of). In theory, the process is supposed to tidy up after itself and set the terminal mode correctly, but it could crash out without doing so, leaving the terminal set to raw mode. You can use the stty command from the shell to fix this with commands like stty sane. Multiple processes active use different terminal devices via xterm or suchlike, or a switcher like screens or shell layer manager, with each having their own virtual terminal device. – ConcernedOfTunbridgeWells Feb 29 '24 at 12:13
12

The terms get used to describe terminal and disk I/O, but mean different things in each context.

Raw and cooked modes on a terminal device cover processing (character-at-a-time vs. line-at-a-time, there are also a few other differences).

A 'raw' disk device (/dev/rdsk/*) is a character device, and the /dev/dsk/ is a block device. IIRC the main difference is that the block device has kernel buffering, and fine grained control over I/O modes such as direct/mapped I/O can be specified on a raw defice.

A description of raw and cooked tty modes can be found here. A posting about raw vs cooked disk devices can be found here.

Praxeolitic
  • 1,668
  • 1
    The second link is no longer valid. http://web.archive.org/web/20190905204530/http://www.linuxsa.org.au/pipermail/linuxsa/2002-February/038793.html – Andy Mar 28 '22 at 04:44