Sikau

Project: A whole computer · Day 4 · about 50 min

Final: A kernel for your CPU

Time-slice two machine-code programs, with a yield system call.

Two programs, one CPU

The finale: an operating system for the CPU you built. Each program gets its own registers and program counter (a process!); your kernel runs each for at most quantum instructions, switches on a SYS 1 (yield), and logs what each program prints — round robin, on real machine code you assembled.

A process is a machine state

Each program gets its own newMachine(): its own registers, program counter and output. That object *is* the process's context. Switching processes means choosing which machine object to pass to step next — the CPU code doesn't change at all, exactly as a real kernel reuses the same hardware with saved and restored register sets.

  • Process = { name, words, machine }.
  • Ready queue = array of processes in turn order.
  • Context switch = pick a different machine object.
  • Terminated = machine.halted is true; drop it from the queue.

Assemble every program once at the start; the kernel only ever deals with machine words.

The rest of this lesson

  • Quantum and yieldexplain
  • The whole stackexplain
  • One kernel turnorder
  • Piece to coursematch
  • runOSchallenge
  • Badge quizquiz
  • The machine: complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 4 lessons of Project: A whole computer