7 routes42 sections324 units

Unit 1.3 · about 4 h

232 / 324

Processes, system calls, fork, exec and scheduling

Assume Programming's shell-processes-environment unit and its exit-status and trap exercises. Here you trace the kernel process model, not shell syntax, and make the launcher own every child it creates.

  • explain
  • implement

You can now

  • Trace fork, exec and wait while identifying kernel-owned state and preserved process identity.
  • Distinguish a privilege-mode transition from a scheduler context switch.
  • Implement child launch and status collection with an explicit exec-failure path.
  • Predict ready, running and blocked transitions under a stated scheduling model.

Covers

  • Programs, processes, system calls and user versus kernel mode
  • Fork, exec, wait, scheduling states and context switches

Study, in this order

5 steps

  1. The process and its API
    Operating Systems: Three Easy Pieces - selected foundations

    Chapter 4, Sections 4.1 to 4.5 (Processes, linked from the book contents); Chapter 5, Sections 5.1 to 5.4, including Figures 5.1 to 5.4

    Depth · Read the redirection example for the fork/exec boundary; leave descriptor details to the next unit.

  2. Protected execution and scheduling
    Operating Systems: Three Easy Pieces - selected foundations

    Chapter 6, Sections 6.1 to 6.3; Chapter 7, Sections 7.1 to 7.2 and 7.6 to 7.8 (CPU Scheduling, linked from the book contents)

    Depth · Treat round robin as a model, not Linux's normal scheduler. A context switch can disturb cache/TLB locality; it does not necessarily flush every cache or every tagged TLB entry.

  3. Linux process launch and switching
    AlgoMaster Operating Systems - selected observation route

    What exec Replaces and What It Preserves; Encoded Wait Status; Handling an Interrupted Wait; Multithreaded fork Considerations; Failure Handling by Operation

    Depth · Then use Context Switching, Mode Switches vs. Context Switches and Blocking, Preemption, and the State Model. Do not benchmark switch cost yet.

  4. Xv6 utilities: the exec task
    MIT 6.1810 - selected xv6 process and memory labs

    exec: adding -exec cmd to find; hints on fork, exec and wait

    Depth · Read and sketch the lifecycle only. Running xv6 needs the RISC-V toolchain and QEMU from the course tools page, which this unit does not require; leave full util-lab completion to a course environment.

  5. One xv6 syscall task: sandbox mask
    MIT 6.1810 - selected xv6 process and memory labs

    Sandbox a command; interpose(mask, path), fork inheritance and grade-lab-syscall sandbox_mask

    Depth · Sketch the stub, syscall number, dispatch and per-process mask ownership. Fall 2025 uses interpose, not the older trace task. Implementing it and running the sandbox_mask test need the xv6 toolchain and are optional here; skip allowed pathnames and Attack xv6.

Focus on

  • Fork creates a child with a different PID; successful exec replaces its address space while retaining its PID. Wait consumes an owned child's termination record.
  • System calls enter controlled kernel code. A call can return in the same task without any switch to another task; blocking or preemption can cause scheduling.
  • Ready is not running, and blocked is not merely low CPU usage. Linux's R status includes running and runnable tasks.
  • Response time and turnaround time answer different questions. A policy comparison needs arrivals, CPU bursts, I/O durations and switch assumptions.

Reproduce closed-book

Write a single-threaded Python launcher using os.fork, os.execl and os.waitpid. In the child, os.write its PID before exec; execute /bin/sh with arguments -c 'echo "after exec pid=$$"; exit 7'. The parent waits for that PID and decodes WIFEXITED/WEXITSTATUS. Repeat with /atlas-missing-executable, catching OSError in the child and calling os._exit(127). Run taskset -c 2 strace -f -e trace=process -- python3 -I launcher.py (CPython 3.14.4). Match the pre/post-exec PID; verify normal exit 7 and launch failure 127. These are program-selected codes, not a universal exec-error encoding. Keep the trace showing process creation, execve and wait4; Linux's fork wrapper may use clone.

Traps

  • Expecting the parent to run first after fork, or treating a sleep as a synchronization guarantee.
  • Returning through normal Python cleanup after a failed child exec. Use os._exit for this post-fork failure path and collect the result in the parent.
  • Extending this single-threaded fork pattern to a multithreaded runtime. Copied locks can remain owned by threads that no longer exist in the child.

Practice

OSTEP `process-run.py`: CPU/I/O state prediction

README Overview and process-run.py; use -s 1 and -s 2 with -l 5:50,5:100 -L 5

Predict the full trace before adding -c -p. Compare SWITCH_ON_IO with SWITCH_ON_END and IO_RUN_LATER with IO_RUN_IMMEDIATE. The answer flag is the simulator's oracle, not a Linux scheduler test. Save one matching launcher trace for the section evidence pack.

Self-test

  1. Does successful exec return zero to the old program?

  2. What must happen after a blocked child's I/O completes before it executes again?

  3. Is every system call a context switch?

  4. Can the raw wait status be compared directly with exit code 7?

Completion criterion

Stop when

Your launcher reaps both success and exec-failure children, and your two-seed simulator predictions distinguish blocked time from ready time and a mode switch from a task switch.

Mark passed, then open 1.4 Only mark this when you can do it without notes.

Leads to

Resources for this unit

  • Operating Systems: Three Easy Pieces - selected foundations
    Remzi and Andrea Arpaci-Dusseauprimarytheory
    Read Chapters 4-7, 13-15, 18-21, 36, 39-40, 42 and 45. Work from process state to address translation, devices, files, journaling and integrity. For every chapter, draw the owned state and one failure boundary instead of collecting summaries.
  • MIT 6.1810 - selected xv6 process and memory labs
    MIT PDOSrequiredcourse
    Use Fall 2025 Lectures 1, 3-6, 8 and 10-11. Complete the util lab, one syscall task, and the page-table inspection plus ugetpid task from pgtbl. Add cow only if copy-on-write remains unclear. Keep all work in your own course checkout and stop before the full xv6 sequence.
  • AlgoMaster Operating Systems - selected observation route
    AlgoMastersupportrevision
    After OSTEP, use the matching lessons as a second explanation and recall check: fork, exec, wait, and exit; Zombies and Orphans; Signals; Context Switching; The Process Address Space; Paging; The TLB: Hits, Misses, and Invalidation; Page Faults; Copy-on-Write; mmap and Memory-Mapped Files; How malloc Works; VSZ, RSS, PSS, and What They Mean; File Descriptors; The Page Cache and Write-Back; Durability and fsync; Crash Consistency; Linux Namespaces; Control Groups; PID 1 and Signal Handling; Container Resource Failures; and the Investigating lessons. Treat quizzes as recall checks and keep your own traces as evidence.

Also in this section

  • OSTEP homework - process, paging, disk and integrity simulators
    OSTEP authorsrequiredpractice
    Solve process-run.py, paging-linear-translate.py, paging-policy.py, disk.py, fsck.py and checksum.py for at least two seeds each before using the answer flag. Then reproduce one prediction using strace, /proc or a small local program.
  • Linux proc and strace references
    man7.org and strace projectsupportreference
    Use proc(5), getrusage(2), mmap(2), fsync(2), rename(2), wait(2) and signal(7) while tracing a parent-child pipe, page faults, file publication and a resource-limit failure. Record exact commands and raw observations.
  • Nand2Tetris - Projects 1 and 4 only
    Nand2Tetrisoptionalrepair
    Use Project 1 for Boolean logic and Project 4 for machine-language execution only if the binary or instruction-level entry check fails. Do not take the full course as a second systems curriculum.
  • Algorithmica - Algorithms for Modern Hardware
    Sergey Slotinrequiredtheory
    Use the Instruction-Level Parallelism, SIMD Parallelism and RAM & CPU Caches chapters for measured pipeline, branch, vectorization, bandwidth, latency and cache-line effects. The figures are the author's Zen 2 measurements at a fixed 2 GHz: predict and measure your own.
  • MIT 6.S894 Accelerated Computing - Throughput Processors
    MITsupportcourse
    Use Lecture 2 for the processor model behind this section: superscalar issue, out-of-order execution, speculation and SIMD's need for coherent control. The labs run on the course's remote machines and target AVX-512 and NVIDIA GPUs, so they belong to the GPU track.
  • Auto-Vectorization in LLVM
    LLVM Projectsupportreference
    Use The Loop Vectorizer Usage and Diagnostics sections for the Clang remarks the pipelines unit asks for; the Features list is optional depth.
  • AlgoMaster: hardware and container storage companions
    AlgoMastersupportrevision
    Use Memory Ordering, Barriers, and False Sharing (coherence and false-sharing headings only); Huge Pages, NUMA, and ASLR (NUMA headings only); Storage Hardware; RAID Fundamentals (comparison and backup boundaries); and Container File Systems. These fill hardware and image-layer gaps outside the pool's selected OS revision lessons.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels