Sikau

Operating systems · Day 1 · about 30 min

What an operating system does

Kernel vs user mode, system calls, and the life of a process.

The program that runs the programs

Your laptop runs hundreds of programs on a handful of CPU cores, each believing it has the machine to itself. The kernel makes that illusion: it shares the CPU (scheduling), memory (virtual memory), files and devices — and protects programs from each other.

Programs can't touch hardware directly. They ask the kernel with a system call — exactly the ecall you used in Assembly. The CPU switches into kernel mode, does the privileged work, and switches back.

A process's life

A process is a running program: its code, memory and registers. It moves between states — *ready* to run, *running* on a core, *blocked* waiting for disk or network, and finally *terminated*.

const MOVES = {
  new: { admit: "ready" },
  ready: { dispatch: "running" },
  running: { timeout: "ready", wait: "blocked", exit: "terminated" },
  blocked: { wake: "ready" },
  terminated: {},
};

function transition(state, event) {
  const next = MOVES[state]?.[event];
  if (!next) throw new Error(`Can't ${event} from ${state}`);
  return next;
}

function lifecycle(events) {
  let state = "new";
  const path = [state];
  for (const e of events) {
    state = transition(state, e);
    path.push(state);
  }
  return path;
}

The rest of this lesson

  • System calls you already useexplain
  • Protection between processesexplain
  • Who does what?match
  • A process's typical lifeorder
  • The state machinewrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 12 lessons of Operating systems