ECE344 Fall 2026 (Sec 1) Lec 4 - Process Creation
Overview
Jon Eyolfson explains Unix process creation through the Linux system calls `fork` and `execve`, beginning with process control blocks, process states, and `/proc`. He demonstrates how `fork` creates an independent child that returns 0 while the parent receives the child's PID, why scheduling makes output order unpredictable, how `execve` replaces a process with a new program, and how shells use these calls for commands and redirection. The lecture also covers copy-on-write, repeated-fork process growth, `strace -f`, and `posix_spawn` as a safer alternative to managing the fork-to-exec interval.
Key takeaways
- A successful `fork()` returns two different values: 0 in the child and the child's PID in the parent, letting both processes distinguish their roles after continuing from the same point.
- Parent and child initially inherit the same file-descriptor mappings, so both can print to the same terminal; changing a descriptor after `fork` affects only the process that changes it.
- Scheduling order between processes is unpredictable, but each process preserves its own instruction order, so concurrent output may interleave without reordering a single process's sequence.
- Repeated `fork()` calls can grow process counts exponentially: one fork per loop iteration produces 2, 4, 8, and 16 processes across successive rounds.
- `execve` replaces the current process image rather than creating a new process, and it returns only on failure; shells combine it with `fork` to launch commands.
- Shell redirection works by changing the child's file descriptor before `execve`; `posix_spawn` offers a higher-level way to configure a new process without manually handling the unsafe fork-to-exec interval.
Chapters
- Linux tracks process bookkeeping in `task_struct`; xv6 uses `struct proc` in `proc.h`.
- A process control block records state, saved registers, scheduling and virtual-memory information, file descriptors, and CPU-time accounting.
- Processes move among created, ready, running, blocked, and terminated states; ready means waiting for CPU time, while blocked means awaiting an event such as file I/O.
- Linux exposes process IDs and status through `/proc`; tools such as `top` inspect these entries, and `/proc/self/status` describes the current process.
- Unlike the Windows `CreateProcess` API, which Jon Eyolfson describes as taking 10 arguments, Unix separates cloning a process from loading a new program.
- `fork()` creates a child as a copy of the calling process and returns a `pid_t`.
- On success, the child receives return value 0 and the parent receives the child's positive process ID; failure returns -1.
- Copy-on-write lets parent and child initially share memory pages, copying pages only when either process modifies them.
- After `fork`, parent and child have independent virtual memory and registers, with matching values at the moment of creation.
- File descriptors 0, 1, and 2 are copied and initially refer to the same underlying terminal, so either process can print to it.
- A child can later change its own file-descriptor mapping without changing the parent's mapping.
- Use `getpid()` and `getppid()` to inspect process and parent IDs; use `man 2 fork` for system-call documentation and `man 3 printf` for C library documentation.
- A program that branches on `fork()`'s return value runs the parent branch when the value is positive and the child branch when it is zero.
- The parent's returned PID should match the child's `getpid()`, while the child's `getppid()` should match the parent's `getpid()`.
- The parent and child both continue from the point after `fork`, but the operating system decides which process runs first.
- Both processes can write through inherited standard output, so their messages appear in the same terminal.
- Calling `fork()` twice without controlling which process reaches the second call produces four processes, because both existing processes fork again.
- A loop that forks on every iteration doubles the process count each round: 2, 4, 8, 16, and onward.
- Process IDs and parent-child relationships can be checked in `/proc`; the shell process, such as `zsh`, is the parent of a command it launches.
- The first Linux user-space process is `init`, with process ID 1, which creates other processes.
- `strace -f` follows child processes created while tracing; without `-f`, the trace primarily follows the original process.
- On Linux, the `fork()` library call can appear as a `clone` system call because `fork` uses a particular configuration of `clone`.
- The trace associates system calls with process IDs, making parent and child activity distinguishable.
- Execution order between processes is nondeterministic, but instructions within each individual process retain program order.
- A loop that calls a helper function to `fork` one child per iteration leaves the original process to continue through the loop.
- Each child inherits the loop's current ID and can print values 0 through 9 in order before exiting.
- A one-second sleep between prints makes interleaving visible: each child's sequence stays ordered, while the order among children varies.
- The example creates four child processes for loop IDs 1 through 4, alongside the original process.
- Replacing `exit(0)` with `return 0` in the child helper returns to the copied `main` function instead of ending the child process.
- The child then resumes the parent's loop and reaches later `fork()` calls, causing additional process creation.
- With four loop iterations, this change results in 16 total processes in the demonstrated example.
- Process-creation code must distinguish terminating a child from returning through inherited control flow.
- `execve(path, argv, envp)` loads the executable at `path` into the current process and starts its entry point, replacing the old program.
- A successful `execve` does not return; code after the call runs only if the call fails, for example when the executable path does not exist.
- The `argv` parameter is a null-terminated array of strings, and `envp` supplies the environment variables.
- The first argument conventionally names the program, but it can differ from the executable path; programs such as `ls` may use it in help or usage output.
- When a shell such as `zsh` runs `ls`, it forks; the child then uses `execve` to become `ls`, while the shell remains available.
- For output redirection such as `ls > files.txt`, the child changes file descriptor 1 to point to the file before calling `execve`.
- The interval between `fork` and `execve` can be difficult to use safely, even with copy-on-write, because the child may need to modify process state.
- `posix_spawn` provides a higher-level alternative with defined actions for configuring a child before execution; it returns an error directly or stores the new process ID on success.
Transcript excerpt
0:10 Alrighty, welcome back to operating systems. So, we get to create processes today, which is a few more fun system calls we get to learn. So as a little bit of a review, recall that the kernel is the thing managing every process. So our process is now we know the thing running in user mode. It has its own independent registers, virtual memory, file descriptors and then the kernel is the thing that is managing our processes. So how it's creating the illusion of virtual registers or independent registers. It's doing it through save registers. We don't know how it's doing it for virtual memory or file descriptors yet. And the kernel is the thing running in kernel
0:59 mode. The only way to cross this user mode kernel mode boundary is through making system calls that we can now srace and figure out what our process is doing. So the actual name of the structure that the process or the kernel uses to actually manage and kind of have bookkeeping information about your process is called the process control block. Everything in Linux is in this sketch.h header file. So Linux kernel calls that process control block the task strct. in XV6 which you are currently working on lab one there it's that strruct proc and it is in that proc.h H file. If I check two right now, oh looks like in lab one over 40% of you
1:54 are already done with lab one. So that is fantastic. So some of the things that will be in that process control block is the process state that we will go over in the next slide. Save registers. So that's where they actually get stored in the process control block. any scheduling information like whether or not I want to run this process next that
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.