Project 6: Userspace Thread Library (Green Threads)

A cooperative threading library that implements thread creation, yielding, and a round-robin scheduler, entirely in userspace without kernel thread support.

Quick Reference

Attribute Value
Primary Language See main guide
Alternative Languages N/A
Difficulty Level 5: Master
Time Estimate 2-3 weeks
Knowledge Area OS Architecture / Concurrency
Tooling Context Switching
Prerequisites Strong C, assembly basics, understanding of calling conventions

What You Will Build

A cooperative threading library that implements thread creation, yielding, and a round-robin scheduler, entirely in userspace without kernel thread support.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Saving and restoring CPU state (registers, stack pointer) (maps to context switching)
  • Allocating and managing per-thread stacks (maps to memory management)
  • Implementing a scheduler and run queue (maps to scheduling)
  • Making it work with blocking I/O (maps to understanding blocking vs non-blocking)
  • Signal handling with custom stacks (maps to interrupts, signals)

Key Concepts

  • Context switching: “Operating Systems: Three Easy Pieces” Ch. 6 - Arpaci-Dusseau
  • setjmp/longjmp: “C Programming: A Modern Approach” Ch. 24 - K.N. King
  • Stack management: “Computer Systems: A Programmer’s Perspective” Ch. 3 - Bryant & O’Hallaron
  • Scheduling algorithms: “Operating Systems: Three Easy Pieces” Ch. 7-9 - Arpaci-Dusseau

Real-World Outcome

thread_create(worker_function, arg);
thread_create(another_worker, arg2);
scheduler_run();  // Runs both, switching on yield()

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: TRACK_A_OS_KERNEL_PROJECTS.md
  • “Operating Systems: Three Easy Pieces” by Arpaci-Dusseau