code wiki / (root) / nx_kernel_sched_test.nx

nx_kernel_sched_test.nx

buildroot/runtime/nx_kernel_sched_test.nx

7117 B102 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind gate/prooftopic kernel
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_kernel_sched_test.nx

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

main sc_puts sys_write sys_mmap build_task sc_b emu_resume sys_mmap ↻ sc_i32 sys_write ↻ sc_num sys_mmap ↻ sys_write ↻ sc_beq sys_exit

structs

none

consts

none

functions

18func 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 }
called by 1: main calls 1: sys_write
19func 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 }
called by 1: main calls 2: sys_mmapsys_write
20func 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
21func 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
22func sc_b(c: *u8, o: i64, b: i64) -> i64 { c[o]=(b & 0xff) as u8; return o+1 }
called by 1: build_task
27func emu_resume(code: *u8, len: i64, st: *i64, console: *u8, clen: *i64) -> i64
called by 1: main calls 2: sys_mmapsc_i32
48func build_task(c: *u8, ch: i64) -> i64
called by 1: main calls 1: sc_b
59func main() -> i64