Expert · Module 10 · Lesson 3 of 12
Historical & Standard Primitives: setjmp/longjmp, sigaltstack, and ucontext_t
Examine POSIX context-switching mechanisms, diagnose why setjmp cannot safely switch stacks, benchmark the hidden syscall cost of ucontext_t, and handle stack overflows via sigaltstack.
By the end you can
- Deconstruct jmp_buf internals and non-local jump mechanics in setjmp and longjmp
- Identify why longjmp across independent stack boundaries causes undefined behavior
- Measure the kernel syscall overhead of signal mask preservation in swapcontext
- Configure emergency alternate execution stacks using sigaltstack and SA_ONSTACK
Builds on Stack Allocation, Alignment, and Guard Pages with mmap and mprotect.
01 · A question first
Why did POSIX formally deprecate makecontext and swapcontext in POSIX.1-2008, why does using setjmp for coroutines invoke undefined behavior, and how does sigaltstack rescue processes from fatal stack exhaustion?
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
- Benchmarking swapcontext vs. the Syscall Trap
- Production Implementation: Alternate Signal Stack Interceptor
- 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