This project extends the xv6 operating system (x86 version) by implementing:
✔ A new system call waitpid()
✔ Kernel-level barrier synchronization
✔ A full user-level threading library
✔ User-space spinlocks using atomic instructions
This README explains setup, installation, modified files, and how to run each part’s testcases.
Adds a new syscall to reap a specific child process by PID.
Implements blocking barriers using kernel sleep() / wakeup().
Adds:
thread_create()thread_exit()thread_join()
Threads share the address space and file descriptors but have individual:
- kernel stacks
- user stacks
- trapframes
Implements spinlocks entirely in userspace using the atomic xchg instruction.
Install required packages:
sudo apt-get update
sudo apt-get install build-essential gcc make python3 python3-pip gitxv6 requires QEMU to run.
git clone https://gitlab.com/qemu-project/qemu.git
cd qemu
./configure
makeIf you get Sphinx errors:
pip install sphinx==6.2.1
pip install sphinx-rtd-theme==1.2.2If ninja is missing:
sudo apt-get install ninja-buildUse the patched xv6 code provided in the OS lab:
https://www.cse.iitb.ac.in/~mythili/os/labs/lab-xv6-threads-sync/xv6-threads-sync-code.tgz
Extract:
tar -zxvf xv6-threads-sync-code.tgz
cd xv6-publicThis project modifies the following xv6 source files:
| File | Purpose |
|---|---|
proc.c |
waitpid + threads logic |
proc.h |
new fields (is_thread, mainthread, ustack) |
defs.h |
added kernel function prototypes |
sysproc.c |
sys_waitpid + sys_thread_* |
ulib.c |
user-space spinlocks + wrappers |
user.h |
user API for threads + locks |
user.c |
stub functions for syscalls |
barrier.c |
kernel barrier implementation |
Replace these files directly in xv6-public/:
Inside xv6-public:
make clean
makeWhen successful, kernel and user programs compile without errors.
GUI mode:
make qemuTerminal-only mode:
make qemu-noxIf successful, you will see:
init: starting sh
$
Four tests are provided:
| Test Program | Description |
|---|---|
t_waitpid |
tests Part A |
t_barrier |
tests Part B |
t_threads |
tests Part C |
t_lock |
tests Part D |
Run any test inside xv6:
$ t_waitpid
$ t_barrier
$ t_threads
$ t_lockreturn value of wrong waitpid -1
return value of correct waitpid <pid>
return value of wait -1
child reaped
Processes block until all arrive; order changes accordingly.
Hello World
Thread 1 created 10
Thread 2 created 10
Value of x = 11
Final value of x: 2000000
- Kernel scans ptable for specific child PID
- Reaps only that child
- Blocks until ZOMBIE or returns -1
- Tracks arrival count using spinlock
- Sleeps early arrivals
- Last arrival wakes up all waiters
thread_create()allocates newprocbut shares parent’s page table- Allocates unique user stack
- Sets trapframe
eipandesp thread_join()reaps only thread resources
- Pure user-space mutual exclusion
- Built using atomic
xchginstruction - Protects shared memory from concurrent access
To add a new user program:
- Create
progname.cinxv6-public/ - Add to Makefile:
UPROGS += _progname- Rebuild:
make clean
makeRun inside xv6:
$ prognameexport PATH=$PATH:/home/<user>/qemu/build/Usually caused by:
- freeing stacks incorrectly
- incorrect thread cleanup
- wrong sleep/wakeup channel
Check your thread_exit() and thread_join().
Check:
- stack alignment in
thread_create() PGROUNDUPofsz- correct stack pointer arithmetic
This repository contains an extended version of xv6 implementing advanced OS concepts:
- system calls
- synchronization
- threads
- user-space concurrency primitives
Ideal for OS lab assignments and educational use.