Core · Module 5 · Lesson 5 of 12
Standard I/O Buffering vs System Calls
Contrast stdio user-space buffering modes with raw POSIX system calls, diagnose fork duplication bugs, and control buffer flushes with setvbuf.
By the end you can
- Differentiate unbuffered, line-buffered, and fully buffered streams in `<stdio.h>`
- Diagnose duplicated output bugs caused by calling fork() with dirty user-space buffers
- Configure stream buffering policies programmatically using setvbuf
- Resolve out-of-order execution artifacts when mixing printf and write calls
Builds on File Offsets, Sparse Files, and lseek.
01 · A question first
Why does a program containing printf("Hello") print "Hello" once when run in a terminal, but print "Hello" twice when redirected to a file followed by fork()? The answer reveals the invisible user-space buffer separating stdio from kernel system calls.
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- Start with the problem
- How the idea works
- Why it is correct
- What it costs
- Pitfalls and variations
- Where it is used
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem