Lab · Lecture 4
fork() terminal
Write a tiny C program, press Run, and see exactly which process printed which line. No compiler needed: the browser simulates fork, exec, wait, exit, sleep and kill.
- 1
Choose an example from the dropdown on the left, or type your own program. Predict the output in your head first.
- 2
Press Run (or Ctrl+Enter). Each output line is tagged with the pid that printed it, and the process tree below shows parents, children, zombies and orphans.
- 3
Open the Challenges tab. Answer how many lines or processes a program produces, then run it to see why.
Try this first: load "Three forks in a row" and check that 3 forks give 8 processes and 7 children, the 2^n rule from the slides.
Supported
- int, pid_t and char* variables, arrays, #define, functions with int parameters
- if / else, while, do while, for, break, continue, return
- + - * / %, comparisons, && and || with real short-circuiting, ++ -- += -= and the ternary operator
- fork(), getpid(), getppid(), wait(&status), waitpid(pid, &status, 0), exit(n), sleep(n)
- printf with %d %i %u %x %c %s %%, puts, putchar
- exec family (stops the old image), kill(pid, SIGKILL), WIFEXITED, WEXITSTATUS, WIFSIGNALED
Not supported
- pipes, files, signals handlers, threads, malloc, structs, switch, strings beyond literals
- stdin: scanf and getchar are not available
- Real timing. sleep(n) advances a virtual clock, and the scheduler always runs the process with the smallest clock
Output order between processes is one valid interleaving, not the only one. A real kernel may schedule differently every run. Line counts and process counts are exact.