diff --git a/docs/autopilot.md b/docs/autopilot.md index 4e0460e..aecbdb1 100644 --- a/docs/autopilot.md +++ b/docs/autopilot.md @@ -65,7 +65,7 @@ The owner's priority since 2026-11-03: proper exam practice. The design, decisio - [x] Computational Mathematics 83 tagged (1 with `lesson` only). (PR #108, stacked on #105) Every migrated question now names its lesson. Questions added to a quiz later still need a `lesson` by hand; no test enforces it, so authors' quiz rounds aren't blocked. - [ ] 5. Generated questions (subnets, scheduling, Big-O first). Split into runs: - [x] 5a. The `QuestionGenerator` plumbing (`instantiate`, `buildOptions`, `generators.ts` per course, `loadBank` adding instances, a fresh seed per exam attempt) and three Computer Networks generators: usable hosts, network address and broadcast address of a random subnet. (PR #109, stacked on #108) - - [ ] 5b. Operating Systems: CPU scheduling (waiting and turnaround times from `src/lib` scheduling logic). + - [x] 5b. Operating Systems: average waiting time under FCFS and under SJF, and a process's finish time under Round Robin, all from the lesson's `schedule()`. (PR #111, stacked on #109) - [ ] 5c. Algorithm Design: Big-O and step counts. The owner's course, so ask before adding. - [ ] 6. Account sync for review cards and exam attempts. @@ -100,6 +100,7 @@ Nothing. The exam-stack block (since 2026-11-06, over #68's inline practice ques Newest first. One line per run: date and run, what was done, PR link, merged or open. +- 2026-11-21 06:10 (run A): exam phase 5b: three generated CPU-scheduling questions for Operating Systems (FCFS and SJF average waiting time, Round Robin finish time with a random quantum), answers from `schedule()` and checked over 500 seeds against an independent simulation. PR #111, open, stacked on #109. - 2026-11-20 10:40 (run B): exam phase 5a: generated questions. `QuestionGenerator` plumbing plus three subnet generators for Computer Networks (usable hosts, network address, broadcast address), answers from the lesson's own `parseCidr` and checked over 500 seeds against an independent bit-mask calculation. They appear in the course exam (fresh numbers every attempt) and in review (fresh numbers every session). PR #109, open, stacked on #108. - 2026-11-20 06:10 (run A): exam phase 4d finished: Computational Mathematics' 83 quiz questions tagged with lesson and section, so all 262 migrated questions now link back to their lesson. Next exam phase: 5, generated questions (subnets, scheduling, Big-O first). PR #108, open, stacked on #105. - 2026-11-19 10:40 (run B): exam phase 4d, second batch: tagged 98 quiz questions in Discrete Mathematics, Logic Design and Algorithm Design with the lesson and section that teach each (lesson only for 5 where no one heading does). Algorithm Design is the owner's course: only tags were added, no wording changed. Left: Computational Mathematics' 83. PR #105, open, stacked on #104. diff --git a/src/content/operating-systems/generators.ts b/src/content/operating-systems/generators.ts new file mode 100644 index 0000000..fffbc63 --- /dev/null +++ b/src/content/operating-systems/generators.ts @@ -0,0 +1,83 @@ +/* Generated questions for Operating Systems: fresh processes every time, answers from the + same schedule() the lessons' CPU scheduler runs. Format: docs/exams.md. */ + +import { buildOptions, randomInt, type QuestionGenerator } from "@/lib/questions"; +import { schedule, type Proc } from "@/lib/operatingSystems"; + +const BASICS = "cpu-scheduling-basics"; + +/** 3 or 4 processes, all arriving at time 0, with different burst times. */ +function randomProcs(random: () => number): Proc[] { + const n = randomInt(random, 3, 4); + const bursts = new Set(); + while (bursts.size < n) bursts.add(randomInt(random, 1, 12)); + return [...bursts].map((burst, i) => ({ name: `P${i + 1}`, arrival: 0, burst })); +} + +/** An average, written the way the lessons write it: 17, 5.5 or 7.33. */ +const fmt = (x: number) => (Number.isInteger(x) ? String(x) : String(Math.round(x * 100) / 100)); + +/** The average waiting time if the longest burst ran first, the worst order: a tempting backwards SJF. */ +const longestFirst = (procs: Proc[]) => schedule([...procs].sort((a, b) => b.burst - a.burst), "fcfs").avgWaiting; + +const table = (procs: Proc[]) => procs.map((p) => `${p.name} = ${p.burst}`).join(", "); + +export const generators: QuestionGenerator[] = [ + { + id: "operating-systems/cpu-scheduling-basics/gen-fcfs-waiting", + lesson: BASICS, + section: "first-come-first-served-fcfs", + generate(random) { + const procs = randomProcs(random); + const fcfs = schedule(procs, "fcfs"); + const sjf = schedule(procs, "sjf"); + const total = fcfs.stats.reduce((s, x) => s + x.waiting, 0); + return { + prompt: `Processes arrive at time 0 in the order ${procs.map((p) => p.name).join(", ")}, with burst times ${table(procs)}. What is the average waiting time under FCFS?`, + ...buildOptions(fmt(fcfs.avgWaiting), [fmt(fcfs.avgTurnaround), fmt(sjf.avgWaiting), String(total), fmt(total / (procs.length - 1)), fmt(longestFirst(procs)), fmt(fcfs.avgWaiting + 1), fmt(fcfs.avgWaiting + 2), fmt(fcfs.avgWaiting + 0.5)], random), + explanation: `FCFS runs them in arrival order, so each waits for everyone ahead of it: ${fcfs.stats.map((s) => `${s.name} waits ${s.waiting}`).join(", ")}. Average: (${fcfs.stats.map((s) => s.waiting).join(" + ")}) / ${procs.length} = ${fmt(fcfs.avgWaiting)}. ${fmt(fcfs.avgTurnaround)} is the average turnaround, which also counts each process's own burst, and ${fmt(sjf.avgWaiting)} is what SJF would give.`, + difficulty: 1, + }; + }, + }, + { + id: "operating-systems/cpu-scheduling-basics/gen-sjf-waiting", + lesson: BASICS, + section: "shortest-job-first-sjf", + generate(random) { + const procs = randomProcs(random); + const sjf = schedule(procs, "sjf"); + const fcfs = schedule(procs, "fcfs"); + const order = [...procs].sort((a, b) => a.burst - b.burst); + const total = sjf.stats.reduce((s, x) => s + x.waiting, 0); + return { + prompt: `Processes ${procs.map((p) => p.name).join(", ")} all arrive at time 0, with burst times ${table(procs)}. What is the average waiting time under non-preemptive SJF?`, + ...buildOptions(fmt(sjf.avgWaiting), [fmt(fcfs.avgWaiting), fmt(sjf.avgTurnaround), fmt(longestFirst(procs)), String(total), fmt(total / (procs.length - 1)), fmt(sjf.avgWaiting + 1), fmt(sjf.avgWaiting + 2), fmt(sjf.avgWaiting + 0.5)], random), + explanation: `SJF runs the shortest burst first: ${order.map((p) => p.name).join(", ")}. Waiting times: ${order.map((p) => `${p.name} ${sjf.stats.find((s) => s.name === p.name)!.waiting}`).join(", ")}. Average: ${total} / ${procs.length} = ${fmt(sjf.avgWaiting)}, the smallest any order can give. ${fmt(fcfs.avgWaiting)} is FCFS in the listed order, and ${fmt(sjf.avgTurnaround)} is SJF's average turnaround.`, + difficulty: 1, + }; + }, + }, + { + id: "operating-systems/round-robin-and-beyond/gen-rr-completion", + lesson: "round-robin-and-beyond", + section: "round-robin-rr", + generate(random) { + const procs = randomProcs(random); + const q = randomInt(random, 2, 4); + const rr = schedule(procs, "rr", q); + const fcfs = schedule(procs, "fcfs"); + // ask about a process that gets preempted at least once, so the quantum matters + const target = procs.find((p) => p.burst > q) ?? procs[procs.length - 1]; + const done = rr.stats.find((s) => s.name === target.name)!.completion; + const fcfsDone = fcfs.stats.find((s) => s.name === target.name)!.completion; + const slices = rr.slices.filter((s) => s.name).map((s) => `${s.name} ${s.from}-${s.to}`).join(", "); + return { + prompt: `Processes arrive at time 0 in the order ${procs.map((p) => p.name).join(", ")}, with burst times ${table(procs)}. Under Round Robin with quantum ${q}, at what time does ${target.name} finish?`, + ...buildOptions(String(done), [String(fcfsDone), String(done - target.burst), String(done + q), String(done - q), String(done + 1), String(done + 2)], random), + explanation: `Each process runs for at most ${q}, then goes to the back of the queue. The Gantt chart: ${slices}. ${target.name}'s last slice ends at ${done}. ${fcfsDone} is when it would finish under FCFS, and ${done - target.burst} is its waiting time, not its finish time.`, + difficulty: 2, + }; + }, + }, +]; diff --git a/src/test/generated-questions.test.ts b/src/test/generated-questions.test.ts index aa6ac46..8c46f82 100644 --- a/src/test/generated-questions.test.ts +++ b/src/test/generated-questions.test.ts @@ -91,6 +91,36 @@ describe("generated questions", () => { } }); + it("compute scheduling answers that match an independent simulation", () => { + const gen = (name: string) => byCourse["operating-systems"].find((g) => g.id.endsWith(name))!; + const answer = (q: Question) => q.options.find((o) => o.id === q.correctOptionId)!.text; + const bursts = (prompt: string) => [...prompt.matchAll(/P\d+ = (\d+)/g)].map((m) => Number(m[1])); + const avgWait = (order: number[]) => order.reduce((acc, b, i) => acc + order.slice(0, i).reduce((x, y) => x + y, 0), 0) / order.length; + const round = (x: number) => String(Math.round(x * 100) / 100); + for (const seed of SEEDS) { + const f = instantiate(gen("gen-fcfs-waiting"), seed); + expect(answer(f), f.prompt).toBe(round(avgWait(bursts(f.prompt)))); + const s = instantiate(gen("gen-sjf-waiting"), seed); + expect(answer(s), s.prompt).toBe(round(avgWait([...bursts(s.prompt)].sort((a, b) => a - b)))); + const r = instantiate(gen("gen-rr-completion"), seed); + const q = Number(/quantum (\d+)/.exec(r.prompt)![1]); + const target = Number(/does P(\d+) finish/.exec(r.prompt)![1]) - 1; + const left = bursts(r.prompt); + const queue = left.map((_, i) => i); + let t = 0; + let finish = -1; + while (queue.length) { + const i = queue.shift()!; + const run = Math.min(q, left[i]); + t += run; + left[i] -= run; + if (left[i] > 0) queue.push(i); + else if (i === target) finish = t; + } + expect(Number(answer(r)), r.prompt).toBe(finish); + } + }); + it("are added to a course's bank by loadBank, with numbers from the seed", async () => { const a = await loadBank("computer-networks", 1); const b = await loadBank("computer-networks", 2);