Process Table

medium · os, process, lifecycle

Process Table

Implement a minimal process table that models fork/exit/wait and reparenting.

Types and functions

type ProcessTable struct { /* internal fields */ }

func NewProcessTable() *ProcessTable
func (pt *ProcessTable) Fork(parent int) int
func (pt *ProcessTable) Exit(pid int)
func (pt *ProcessTable) Wait(parent int) int
func (pt *ProcessTable) Parent(pid int) int
func (pt *ProcessTable) IsZombie(pid int) bool

Rules

  • The table starts with PID 1 (init). Its parent is 0.
  • Fork(parent) creates a new child of parent, returns its PID.
    • PIDs start at 2 and increase by 1.
    • If parent does not exist or is a zombie, return -1.
  • Exit(pid) marks the process as a zombie.
    • PID 1 never exits; ignore Exit(1).
    • When a process exits, its living children are reparented to PID 1.
  • Wait(parent) reaps one zombie child of parent.
    • If multiple zombie children exist, reap the smallest PID.
    • Reaping removes the process from the table entirely.
    • Return the reaped PID, or -1 if none exist.
  • Parent(pid) returns the parent PID, or -1 if the process does not exist.
  • IsZombie(pid) returns true if the process exists and is a zombie.

Notes

  • A zombie remains in the table until reaped.
  • A living child is one that exists and is not a zombie.
Run tests to see results
No issues detected