nx_kernel_sched_test.nx
buildroot/runtime/nx_kernel_sched_test.nx
about
nx_kernel_sched_test.nx -- x86 ladder R-kernel rung-1: a SCHEDULER (the heart of an OS).
A boot+loader gets code running; what makes it an OPERATING SYSTEM is multitasking: more than one
task, with a real CONTEXT SWITCH between them. This builds two tiny x86 tasks and a cooperative
round-robin scheduler. The emu is made RESUMABLE: emu_resume runs a task from its saved state until
it executes YIELD (0xF1), saving its registers + pc into a per-task control block, so the scheduler
can switch to the other task and come back exactly where it left off.
The proof of a REAL context switch: each task sets rdx=0x3F8 (the UART port) ONCE before its loop;
on every resume it re-enters mid-loop WITHOUT re-setting rdx, so each of its prints only works if the
scheduler RESTORED rdx across the switch. Task A prints 'A' then yields; task B prints 'B' then
yields. Round-robin for 4 rounds must produce exactly "ABABABAB" -- fair, no starvation, context
preserved.
KAT: console == "ABABABAB", both tasks alive (not halted), equal progress (4 each).
HONEST SCOPE: COOPERATIVE (yield-based) on one core. PREEMPTIVE scheduling (timer IRQ0 via the PIC +
IDT) is the next rung; then memory manager, syscalls, userspace. No hw writes (Rule 26).
expect_exit: 0 license_tier: ORIGINAL
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| none |
functions
| 18 | func sc_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 19 | func sc_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } |
| 20 | func sc_beq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } called by 1: main |
| 21 | func sc_i32(c: *u8, o: i64) -> i64 { return (c[o] as i64) | ((c[o+1] as i64)<<8) | ((c[o+2] as i64)<<16) | ((c[o+3] as i64)<<24) } called by 1: emu_resume |
| 22 | func sc_b(c: *u8, o: i64, b: i64) -> i64 { c[o]=(b & 0xff) as u8; return o+1 } called by 1: build_task |
| 27 | func emu_resume(code: *u8, len: i64, st: *i64, console: *u8, clen: *i64) -> i64 |
| 48 | func build_task(c: *u8, ch: i64) -> i64 |
| 59 | func main() -> i64 |