TTY and Buffering

(mattrighetti.com)

40 points | by mattrighetti 5 days ago

4 comments

  • lapsed_lisper 1 hour ago
    A while ago I stumbled across a technique for improving stream buffering that I wish more I/O library implementors knew about. Korn and Vo's sfio library (circa 1991) had a feature called "pooling", whereby distinct streams could be linked together. Any read or write operation to any stream in a pool implicitly synchronized all the other streams in the pool first. This way, when stdio and stderr were pooled, which was the default when both went to ttys, a write on stderr implicitly flushed stdout. I've implemented this feature for myself a couple times; it's fairly easy to do and basically eliminates the need to explicitly flush streams in client code.

    Citation: https://archive.org/details/1991-proceedings-tech-conference... but note that the explanation of stream pools there is a little less precise and more general than really necessary. I believe that later versions of sfio simplified things somewhat, though I could be wrong. (I find their code fairly hard to read.)

    Anyhow, ISTM a missed opportunity when new languages that don't actually use libc's routines for something reinvent POSIX's clunkier aspects.

  • teddyh 4 hours ago
    > Surprisingly, Rust, as of now, uses line buffering for both TTYs and non-TTYs.

    > The FIXME comment shows the Rust team acknowledges that ideally they should check if something is executed in TTYs or not and use LineWriter or BufWriter accordingly, but I guess this was not on their priority list.

    This does not inspire confidence.

    • dwattttt 1 hour ago
      That's not forced behaviour. If you want to do something more interesting, you'd use the raw/unsynchronised handles:

        /// The returned handle has no external synchronization or buffering layered on top.
        const fn stdout_raw() -> StdoutRaw;
  • nanolith 5 hours ago
    In libc, you can use setvbuf to change the buffering mode.
  • amelius 5 hours ago
    How would a modern OS implement this?
    • geocar 2 hours ago
      > How would a modern OS implement this?

      fwrite only buffers because write is slow.

      make it so write isn't slow and you don't need userspace buffering!

    • Veserv 1 hour ago
      You do not need any OS changes, you just need a print library that does buffering correctly.

      Buffering should basically always be: “Work or Time” based, either you buffered enough or enough time has passed. This is because you buffer when per-element latency starts bottlenecking your throughput.

      If you have so little data that your throughput is not getting limited, then you should be flushing.

    • pocksuppet 4 hours ago
      Probably by not assuming terminals and byte streams any more. Terminal-by-default is a 20th-century-ism. Now you have screens with pixels. Without stdout, no need to know if stdout is a terminal.
      • zbentley 3 hours ago
        This is an interesting idea--that in a reimagined OS, programs could have their output connected to all sorts of sinks (terminal, file, GUI, web content) without carrying baggage related to those sinks' behaviors.

        I think the core question is whether some middle layer of output processing between program and sink/display could be created that knows enough about (using terminals as an example sink) raw mode/console dimensions/buffering to make most programs display correctly enough for most users without knowing specifics about the program writing the output's internals. If that can be done, then programs that need more specifics (e.g. complex animated/ncurses GUIs) could either propose overrides/settings to the output middleware or configure it directly, and programs that don't wouldn't.

        That's possible to implement, sure, but can that be done without just reinventing the POSIX terminal API, or any one of the bad multiplatform-simple-GUI APIs, badly?

        • Joker_vD 2 hours ago
          > programs could have their output connected to all sorts of sinks (terminal, file, GUI, web content) without carrying baggage related to those sinks' behaviors.

          We already have this. The TTY itself is not very special at all. It's just that the applications, traditionally, decide that they should special-case the writing to TTYs (because those, presumably, are human-oriented and should have as little batching as possible). But you, as an application developer, can simply not do this, you know.

          • wahern 46 minutes ago
            Automatically changing behavior by testing if the output sink is a TTY is traditionally considered an anti-pattern by those with enough time and hair loss spent at the terminal. It's one of those things where there are definitely occasions where it's useful, but it's overused and can end up frustrating people more than it helps, like when they're attempting to replicate a work flow in a script.[1] A classic example of "just because you can do something doesn't mean you should do it".

            I don't know how it works today, but IIRC colorization by GNU ls(1) used to require an explicit option, --color, typically added through an alias in default interactive shell configs, rather than ls automatically enabling it by default when detecting a TTY.

            Explicit is generally better than implicit unless you're reasonably sure you're the last layer in the software stack interacting with the user. For shell utilities this is almost never the case, even when 99% of usage is from interactive shells. For example, `git` automatically invokes a pager when it detects output is to a TTY; this is endlessly frustrating to me because most of the time I'd prefer it dumped everything to the screen so I could more easily scroll using my GUI terminal window, as well as retain the output in the scroll buffer for later reference. Git does have the -P option to disable this behavior, but IMHO it has proper defaults reversed; usually I just end up pipe'ing to cat because that's easier to remember than bespoke option arguments for frilly anti-features.

            [1] Often times it forces people to use a framework like expect(1) to run programs with another pseudo TTY for child programs just to replicate the behavior.

        • pocksuppet 3 hours ago
          We use them today - terminal emulators. They intermediate between bytes and pixels.

          If colours were delivered via a sideband, you wouldn't have to know whether the other side was a terminal to disable colours. You could send colours to a file and they wouldn't be stored - or would be stored in RTF format, if you were sending to an RTF file.

          The design we use on Linux is very "worse is better". Some mechanisms were developed because they could be developed, and those mechanisms, because they were the ones available, were made to fulfil every purpose they could fulfil, and now we're locked into this design for better or worse.

          Windows used to have APIs to directly set text colour. You could set the colour to blue and print some text and it would be blue. You could call a function on a console window object to ask how big the console window was, or to change it. This obviously doesn't compose through pipes or ssh, but Windows doesn't have a pipe culture or ssh culture so that was never a design criterion. They've since deprecated that and moved to the worse-is-better escape-code design, in order to increase compatibility with Linux.

        • direwolf20 52 minutes ago
          Why is the sibling comment about Windows [dead]?
          • zbentley 30 minutes ago
            I don't know. I vouched for it (as I did for GP to start this thread) just now. It's from a sockpuppet-rhyming username with 0 feedback. I hope I'm not enabling some bad behavior that they're being marked dead for elsewhere.