Skip to content

Process Management

kazah-png edited this page Jul 11, 2026 · 3 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 (via spawn_user_path or execve) → run → exit → reap.

  • execve(): replaces the calling process's image in place (same pid, same fds, new address space). ELF is loaded into a fresh page directory via elf_load_image, then the old one is freed (COW-refcount aware). argv is threaded onto the new stack in SysV format.
  • spawn (background): spawn_user_path() loads an ELF as a sched_managed ring-3 process and returns immediately. The scheduler time-slices it — the shell stays responsive.
  • exec (foreground): runs the child preemptively while the desktop recomposites at ~16 fps — TUIs render live in the GUI window. kwait() reaps after the child becomes a zombie.

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

Signals

A POSIX-style subset in kernel/signal.c:

  • Posted (bit in sig_pending) and delivered at return-to-ring-3 by signal_dispatch (called from the syscall entry stub)
  • SIG_DFL: terminates (exit code 128+signo)
  • SIG_IGN: dropped silently
  • User handler: interrupted context saved, trampoline pushed to user stack, handler entered → returns through SYS_SIGRETURN to restore context
  • Ctrl-C: keyboard IRQ posts SIGINT to the foreground process; blocks in read() are interrupted with -EINTR

Preemption safety

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

SMP

See SMP for multi-core scheduling. Currently APs park in cli;hlt; per-CPU run queues, reschedule IPIs, and TLB shootdown are future work.

Clone this wiki locally