Skip to content

Process Management

kazah-png edited this page Jul 6, 2026 · 8 revisions

Process Management

Preemptive weighted round-robin scheduler. Fixed process table (64 entries).

See also: Architecture, Syscalls, Userspace, SMP, Memory Management, Shell

Process states

PROC_RUNPROC_BLOCKEDPROC_ZOMBIEPROC_EMPTY

Scheduler

  • Each process has sched_weight (1–64) and sched_quantum countdown
  • PIT fires at 1000 Hz; ticks decrement quantum, switch when quantum hits 0
  • Compositor gets weight 4 for responsive GUI
  • nice/renice set job weights from the Shell

Kernel threads

Created via create_task(func, stack). Run in ring 0 with kernel PML4.

User processes

Ring 3 with own PML4. Lifecycle: spawn → schedule → exit → reap. See Userspace.

fork()

SYS_FORK (syscall #10) clones the calling ring-3 process via copy-on-write:

  • Refcounted frames: page_incref() tracks share count; free_page() only frees at last reference
  • COW clone: clone_page_directory_cow() walks user PML4[0..510]; writable leaves downgraded to read-only + COW in both parent & child (refcount bumped), read-only pages (code) shared as-is
  • First writer: vm_handle_fault detects COW → allocates private copy, drops shared reference
  • Return: child gets fork()==0, parent gets child's PID — both resume in round-robin

Blocking

  • kwait(pid): block until child exits
  • sleep(ms): block with wake_tick, scheduler wakes on timer tick
  • cli;hlt yield pattern ensures atomic check-then-sleep

Preemption safety

preempt_disable/enable guards kmalloc, kfree, EXT2 ops, VFS node pool.

SMP

See SMP for multi-core scheduling (future: per-CPU run queues).

Clone this wiki locally