PintOS is a teaching operating system for x86 architecture, originally developed at Stanford University. This repository contains a working implementation with three of the four major project milestones completed: Threads, User Programs, and File Systems. The Virtual Memory project is stubbed but not implemented.
src/
├── threads/ Core kernel: threading, scheduling, synchronization, memory allocation
├── userprog/ User program loading, system call handling, process management
├── vm/ Virtual memory (stubs only — not implemented)
├── filesys/ File system with indexed inodes and subdirectory support
├── devices/ Hardware drivers (timer, keyboard, disk, serial, VGA)
├── lib/ Shared C library used by both kernel and user programs
├── examples/ Example user programs (cat, cp, echo, ls, mkdir, shell, etc.)
├── tests/ Perl-based test suite with expected-output checks
├── utils/ Build utilities (pintos runner, mkdisk, etc.)
└── misc/ Miscellaneous helper scripts
The kernel threading system in src/threads/ provides:
- Thread lifecycle: creation, scheduling, context switching, and destruction.
- Priority-based scheduling with round-robin time slicing (4-tick quantum).
- Multi-level feedback queue scheduler (MLFQS) support as an alternative scheduler.
- Synchronization primitives: semaphores, locks, and condition variables (
synch.c). - Memory allocators: a page allocator (
palloc.c) for 4 KB pages and a sub-page malloc allocator (malloc.c).
User program support lives in src/userprog/ and includes:
-
ELF binary loading with full command-line argument passing. Arguments are placed on the user stack following x86 calling conventions (
process.c). -
15 system calls implemented in
syscall.c:Syscall Description haltShut down the machine exitTerminate the current process with a status code execStart a new process from an executable path waitBlock until a child process exits createCreate a new file of a given size removeDelete a file by name openOpen a file and return a file descriptor filesizeReturn the size of an open file readRead bytes from a file descriptor (or stdin) writeWrite bytes to a file descriptor (or stdout) seekSet the read/write position of a file tellReturn the current position of a file closeClose a file descriptor chdirChange the current working directory mkdirCreate a new directory Additional directory-related syscalls (
readdir,isdir,inumber) are also present. -
Per-process file descriptor table with descriptors 0, 1, and 2 reserved for stdin, stdout, and stderr.
-
Parent-child process management: the parent tracks each child via a
child_processstruct and synchronizes on load and exit using semaphores. -
User memory validation: every pointer passed from user space is checked before access; invalid pointers cause the process to exit with status
-1.
The src/vm/ directory contains stub files (page.c, frame.c, swap.c). The thread struct has a page_hash field for a supplemental page table, but no demand paging, swapping, or memory-mapped files are implemented.
The file system in src/filesys/ goes well beyond the default PintOS flat file system:
- Multi-level indexed inodes (
inode.c): each inode has 12 direct blocks, 1 single-indirect block (128 pointers), and 1 double-indirect block (128 x 128 pointers), giving a maximum file size of roughly 8 MB. - Subdirectory support (
directory.c,filesys.c): full path resolution for both absolute (/a/b/c) and relative paths, resolved from the process's current working directory. - Per-process current working directory: inherited from parent to child on
exec. - Directory operations: create, remove, list entries, and traverse.
- Write-deny semantics: running executables cannot be modified while in use, enforced through deny-write counters and condition variables on inodes.
- A global
file_lockserializes all file system operations from user space. This is simple but limits concurrency. - Per-inode locks protect inode metadata during read/write and extension.
- Semaphores are used for parent-child signaling (load success/failure and exit status).
- Condition variables coordinate deny-write tracking on inodes.
PintOS requires an i386-elf cross-compiler toolchain (i386-elf-gcc, i386-elf-ld, etc.) and standard Unix build tools.
To build a specific project:
cd src/userprog # or src/threads, src/filesys
makeThis produces a build/ directory containing kernel.bin, kernel.o, and the bootable os.dsk disk image.
Example user programs in src/examples/ (cat, cp, echo, ls, mkdir, shell, etc.) are compiled automatically during the userprog or filesys build.
PintOS can run on QEMU or Bochs. The pintos utility script in src/utils/ wraps the emulator invocation:
cd src/userprog/build
pintos -- run 'echo hello world'To run with a file system disk:
pintos -p ../../examples/echo -a echo -- -f -q run 'echo hello world'-p <file> -a <name>: copies a file into the PintOS file system under the given name.-f: formats the file system before running.-q: powers off after the command finishes.
Each project has a test suite under src/tests/. Tests are Perl scripts that compare actual kernel output against expected output (.ck files).
cd src/userprog/build
make check # run all tests for this project
make tests/userprog/args-none.result # run a single testTest categories include:
- threads: alarm, priority donation, MLFQS scheduling
- userprog: argument passing, system calls, bad pointer handling, multi-process scenarios
- filesys: basic file operations, extended indexed-inode tests, directory tests
┌──────────────────────────────────────────────┐
│ User Programs │
│ (examples/: cat, ls, shell) │
├──────────────────────────────────────────────┤
│ System Call Interface │
│ (userprog/syscall.c) │
├────────────┬─────────────┬───────────────────┤
│ Process │ File │ Directory │
│ Mgmt │ Operations │ Operations │
│ (process.c)│ (file.c) │ (directory.c) │
├────────────┴─────────────┴───────────────────┤
│ File System Layer │
│ (filesys.c, inode.c, free-map.c) │
├──────────────────────────────────────────────┤
│ Block Device Layer │
│ (devices/block.c, ide.c) │
├──────────────────────────────────────────────┤
│ Kernel Threading & Memory │
│ (threads/thread.c, synch.c, palloc.c) │
├──────────────────────────────────────────────┤
│ x86 Hardware (via QEMU/Bochs) │
└──────────────────────────────────────────────┘
- User space:
0x00000000—0xC0000000(lower 3 GB) - Kernel space:
0xC0000000—0xFFFFFFFF(upper 1 GB) - Page size: 4096 bytes
- Disk sector size: 512 bytes
| File | Purpose |
|---|---|
threads/thread.c |
Thread creation, scheduling, context switching |
threads/synch.c |
Semaphores, locks, condition variables |
userprog/process.c |
Process loading, argument setup, wait/exit |
userprog/syscall.c |
System call dispatch and implementation |
userprog/pagedir.c |
Page directory management for user processes |
filesys/inode.c |
On-disk inode with multi-level indexing |
filesys/filesys.c |
High-level file system operations and path resolution |
filesys/directory.c |
Directory entry management |
devices/timer.c |
System timer and tick counting |
devices/ide.c |
IDE disk controller driver |
- No virtual memory: pages are never swapped to disk; processes are limited to physical memory.
- Single-threaded processes: each process has exactly one kernel thread.
- Coarse-grained file system locking: a single global lock serializes all file operations.
- Max file size ~8 MB: constrained by the double-indirect block indexing scheme.
- Max filename length: 14 characters per path component.