← Back

Ghost Hunting Simulation

C POSIX Threads Semaphores Linux
View on GitHub ↗

What it is

A ghost hunting game in C. A team of hunters explores Willow House, a 13-room building, while a ghost moves around dropping evidence. Hunters collect it; the program uses bitwise ops to match evidence against ghost types.

Everything runs concurrently. Each hunter is a thread. The ghost is a thread. They all share the same memory.

Screenshots

Simulation setup
Simulation running
Simulation output
Simulation results and cleanup

How it works

Each hunter and the ghost run on their own POSIX thread. Every room has a semaphore that a thread must acquire before entering. The shared evidence file and stdin have their own semaphores too.

Deadlock prevention was the hard part. When a thread needs to lock multiple rooms, it sorts them by memory address first, then locks in that order. Every thread follows the same sequence, so circular waits can't form.

Hunters navigate with a breadcrumb stack, pushing rooms as they go and popping back when they need to return to the van. The ghost wanders randomly and speeds up when hunters are close.

Ran 1,000+ simulated ticks with zero deadlocks or memory leaks, verified with Valgrind.