fosscat-site/content/tils/why-we-cant-forget-to-flush.md

47 lines
1.7 KiB
Markdown

---
date: 2025-10-01T22:32:18-06:00
description: ""
lastmod: 2025-10-01T22:32:18-06:00
showTableOfContents: true
type: "tils"
title: "TIL: Why We Cant Forget to Flush"
image: ""
image_credit: ""
image_alt: ""
tags: ["zig", "programming"]
---
# Context
I just learned about this book [Systems Programming with Zig](https://www.manning.com/books/systems-programming-with-zig).
I'm reading through the free introductory chapters to familiarize myself with things I thought I'd already know. But, as
a college drop-out, I think there are a handful of low level concepts I missed, and buffer flushing was one of them!
# Reflection
Zig is all about removing implicit behavior. Its number one of the `zig zen` after all:
```shell
nate in ~/source/zig-systems on main λ zig zen
* Communicate intent precisely.
* Edge cases matter.
* Favor reading code over writing code.
* Only one obvious way to do things.
* Runtime crashes are better than bugs.
* Compile errors are better than runtime crashes.
* Incremental improvements.
* Avoid local maximums.
* Reduce the amount one must remember.
* Focus on code rather than style.
* Resource allocation may fail; resource deallocation must succeed.
* Memory is a resource.
* Together we serve the users.
```
When you want to write some text to the console, the program needs to make a syscall to actually send that data to
the OS, to then print out those characters. By default, most languages flush buffers for you, because when you call
`writeOut("some text")`, you expect it to write it! But, to save on syscalls, zig will buffer those bytes, and any more
bytes, until you tell it to `flush()`, which then tells your program to make the syscall so we can see those beautiful
bytes.