ECE344 Fall 2026 (Sec 1) Lec 4 - Process Creation
Watch on YouTube →
Overview
Jon Eyolfson explains Unix process creation through the process control block, process states, and the complementary system calls fork and execve. He traces how fork duplicates a running process, how its parent and child continue independently with different return values, and how shells use fork followed by execve to launch commands; POSIX spawn offers a safer alternative for common cases.
Key takeaways
- fork returns different values in its two processes: zero in the child and the child's PID in the parent, allowing both to choose separate control-flow paths.
- Copy-on-write makes fork initially memory-efficient, but parent and child still have independent process state and can diverge when either modifies memory.
- Repeated fork calls multiply processes: two consecutive forks can produce four, and a fork in each loop iteration can double the total again.
- Process output order across parent and child is nondeterministic, but each process's own instruction order remains consistent.
- execve replaces the current program image and does not return on success; shells combine it with fork to launch commands while retaining the shell process.
- The fork-to-exec interval enables setup such as redirecting file descriptor 1, while POSIX spawn offers a higher-level way to configure such actions.
Chapters
0:00
Process Control Blocks Track Linux and xv6 Processes
- The kernel manages each user-mode process, including its registers, virtual memory, and file descriptors.
- Linux stores process bookkeeping in the task_struct; xv6 uses struct proc in proc.h.
- A process control block includes saved registers, scheduling and memory information, open file descriptors, accounting data, and a unique PID.
2:52
The Five Process States: Ready, Running, Blocked, and Terminated
- A process moves among ready, running, blocked, and terminated states after it is created and its executable is loaded.
- Ready means waiting for CPU time; blocked means waiting for another event, such as a disk read.
- The kernel can preempt a running process back to ready, unblock it when an event finishes, or terminate it when execution ends.
- Linux exposes process information through /proc/PID/status; tools such as top inspect the /proc tree.
7:20
Unix Splits Process Creation Between fork and execve
- Unlike Windows' CreateProcess API, which takes many arguments, Unix separates cloning a process from loading a new program.
- fork creates a child process based on the currently running process; execve replaces a process image with a specified executable.
- The fork call returns -1 on failure, 0 in the child, and the child's positive PID in the parent.
- getpid and getppid report a process's own PID and its parent's PID.
12:00
What fork Copies: Memory, Registers, and File Descriptors
- After fork, parent and child resume at the same point with independent virtual memory and register state.
- Copy-on-write lets the processes initially share memory pages, copying a page only when one process modifies it.
- The child inherits file descriptors, so parent and child can both write to the same terminal; either process can later redirect its own descriptor.
- Use man 2 fork for system-call documentation and man 3 printf for C library documentation.
16:45
Reading fork's Two Return Values in a Parent-Child Example
- A fork example branches on the return value: the parent receives the child's PID, while the child receives zero.
- getpid and getppid confirm that the parent's fork result matches the child's PID and that the child's parent PID matches the original process.
- Both processes execute the code after fork, so output from both branches appears even though each individual process follows only one branch.
21:30
Scheduling Makes Output Order Unpredictable; Repeated fork Multiplies Processes
- The kernel chooses which process runs after fork, so parent and child output order is nondeterministic even if one order is common.
- Each process that calls fork creates another child: two successive forks can produce four processes.
- A fork inside a loop can double the process count on each iteration, quickly consuming system resources.
- Execution order remains consistent within each process even though output interleaving between processes is unpredictable.
27:40
Trace Process Ancestry and fork Calls with /proc and strace
- A program launched from a shell such as zsh descends from that shell through the process parent-child chain.
- The /proc/PID/status file reveals a process name and state, allowing users to inspect a parent process by PID.
- Run strace -f to follow child processes; the trace shows fork implemented through Linux's clone system call.
- strace labels system calls with process IDs, making it possible to distinguish parent and child activity.
33:45
Returning from a Forked Function Can Create Extra Process Generations
- In a loop that forks four children, calling exit in each child prevents it from returning to the parent's loop.
- Replacing exit with return sends the child back to the code that called the function, where it can continue the loop and fork again.
- That small control-flow change can produce 16 processes rather than the intended four children plus the original process.
40:35
execve Replaces the Process Image and Starts a New Program
- execve takes an executable path, a null-terminated argv array, and an environment-variable array.
- On success, the process keeps its identity but replaces its program image and begins executing the new program, such as /bin/ls.
- Successful execve never returns to the old code; it returns only when an error occurs, so error-handling code after the call handles failure.
- The new program inherits file descriptors unless they are changed before execve.
45:10
argv[0], Shell Redirection, and the POSIX spawn Alternative
- argv[0] conventionally names the program, although the executable path and argv[0] do not have to match.
- Programs that assume argv[0] exists can mishandle an empty argument vector; Eyolfson cites a sudo bug as an example of the risks.
- A shell commonly runs a command by forking, adjusting the child's file descriptors for redirection if needed, then calling execve.
- POSIX spawn provides a higher-level alternative that performs process creation and configured file actions without requiring application code to manage the fork-to-exec interval.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jon Eyolfson.