Skip to content

Mope

A multitasking kernel, proven by five hungry philosophers

C++DOSBorland C++Completed

Part 7 of 9 · Early Work

Mope (also called Mtask) is the kernel I wrote for my Operating Systems course in 2000, with Borland C/C++ 3.1. It is preemptive: it hooks the PC’s timer interrupt, INT 8, and on every tick of the quantum it saves the running task’s stack segment and pointer and loads the next task’s, which is the whole trick of a context switch in 625 lines. Tasks move between six states (delayed, running, suspended, blocked, ready and dead), and synchronize through semaphores or through mailboxes of up to 1,024 messages.

The semaphore build running in DOSBox: five philosophers around the table, four thinking under floating equations and Zs, one in red reaching for the noodles.

The dining table, rendered in 3D Studio Max: five bowls on a marble table over a wooden floor, under a legend reading pensando, hambriento, comiendo (thinking, hungry, eating).

To prove it works, it seats the Dining Philosophers at this table: five philosophers, five chopsticks, and the classic risk that everyone grabs one and starves. I rendered them in 3D Studio Max in three poses, colored by state: white while thinking, yellow when hungry, red while eating. It ships twice, once solved with semaphores and once with messages, both from the textbook, both running on the kernel.

Three renders of a philosopher: thinking, in white, with e equals m c squared floating overhead; hungry, in yellow; and eating, in red.

Officially, it was a team of four’s project. In practice I wrote it alone, again; the other three sat on the living room couch, watching satellite TV courtesy of a smart card unlooper I had built from schematics I found somewhere on the Web.

My professor, Hugo Fernando Hernández, told me it was the best kernel anyone had handed him in all his years teaching Operating Systems, and the course used it as the model for final projects afterward.

Reading it again

Of my school projects this is the one I’m proudest of, and reading it again, parts of it hold up as kernel code.

Every task starts life with a forged interrupt frame on its own stack, laid out exactly the way Borland’s interrupt handlers push registers. The first time the scheduler switches to it, the task “returns” from an interrupt it never took, straight into its entry point, and when its function returns, it lands in its own kill routine. A task that gives up the processor calls the timer handler directly, which builds the same frame the hardware would, so preemption and yielding share one path. Semaphores re-check their count after every wakeup, which is the correct way to do it. And no task ever calls DOS or the BIOS: everything draws straight into video memory and reads its own keyboard buffer, which sidesteps DOS’s worst problem for multitasking, that it can’t be re-entered.

There is a comment where you can watch me learn something. After swapping the stack segment and pointer, my local variables were gone, because locals live on the stack I had just swapped away, and the comment says so: “this variable has to be global… otherwise once SS or SP change, if it’s a local, we can no longer reach it!!”

The mistakes are the ones you make before C’s integer rules and the PC’s interrupt controller are second nature. Each semaphore is identified by its 32-bit address, built as FP_SEG(variable)<<16, but in a 16-bit compiler that shift happens in 16 bits, the segment falls off the end, and only the offset is left. Two semaphores at the same offset in different segments share an identity, and waking one can wake the other’s waiter instead. The fix is a cast, (DWORD), before the shift.

The timer handler never acknowledges the interrupt controller itself; it counts on the BIOS handler it chains to. At the 18 ticks per second the demos run, every tick is chained, so it works. Speed the clock up and the first tick that isn’t chained is never acknowledged, and the machine freezes. When no task is ready, the scheduler waits inside the interrupt handler, with interrupts off, for a time that only that same interrupt can advance; it survives because the demo’s main loop never sleeps. And a dying task frees its own stack while still running on it. Today it would have an idle task, a reaper for dead tasks, and an acknowledgment on every path. I never hit any of these because the philosophers never asked for them, and the philosophers were my whole test suite.

For 2000, the part that holds up is that it is real. Operating systems courses usually settled for a simulation, processes as records in a list and a scheduler moving them between queues, with nothing ever interrupted. Mope switches real stacks on the real timer interrupt, and its forged frame is the same idea real kernels use to start a task, worked out alone. The rule I broke was not obscure: every reference on the PC’s interrupt controller, Ralf Brown’s list among them, says a timer handler that doesn’t chain must acknowledge the interrupt. Mine just counted on chaining every time.

They do eat well, though. The call that makes one eat is commented “Delicioso espagueti” (“delicious spaghetti”).

Downloads: both builds with source.

Source and history on GitHub: Kronuz/Mope, with the kernel and both demos.