Skip to content
Computing Medium #fork#exec#unix

fork() and exec()

fork duplicates the process; exec replaces its image. Together they birth every Unix program.

A free, animated fork() and exec() you can read here or embed on any website, from Scrollchart.

fork() and exec()

fork() and exec(): Unix Process Creationfork duplicates with CoW pages; exec replaces the image. Every shell command follows this path.ParentPID 1000fork()duplicatesChildPID 1001Shared CoW pages (read-only).text.dataheapwrite traps MMU, kernel copies pageexec()replacesmemory mapnew binaryfresh address spaceparent runningforkchild = copy of parentexecchild = new programKey costs:fork ~50 µs (page-table copy); CoW faults ~1 µs/page on first write; exec maps new ELF segments (~200 µs on Linux).vfork() skips CoW entirely (shares address space) for exec-only paths. posix_spawn() combines both steps.

Parent process calls fork(); two identical processes emerge sharing pages copy-on-write. Child calls exec(), the kernel replaces its memory map and reloads from the new binary. Page-table CoW visualized as shared frames flipping to private on write.

Good for

  • OS and systems programming courses explaining process creation
  • Shell implementation tutorials (every pipeline uses fork and exec)
  • Container runtime explainers (runc, Docker use clone() and exec variants)

Source & accuracy

This fork() and exec() is an editorial illustration built to represent the concept accurately. Where it shows figures, they are typical or representative values chosen to make the relationship clear, not a single underlying dataset. The diagram and its explainer are reviewed and maintained centrally, and updated over time as understanding improves.

Duplicating and Replacing the Process Image

fork() creates a new process as a duplicate of the calling process. After fork(), both the parent and child execute the same code, but with separate memory and resources. fork() returns twice: it returns the child's PID to the parent, and 0 to the child. The child inherits a copy of the parent's file descriptors, environment variables, and signal handlers.

exec() replaces the current process's image with a new program. It loads the executable from disk, initializes its memory (code, heap, stack), and jumps to the entry point. exec() does not create a new process; it overwrites the current process in place. If exec() succeeds, the calling code never runs again. If it fails, it returns an error to the original code.

The Unix Pattern for Spawning Programs

In Unix shells and system services, spawning a new program is implemented as fork+exec. The shell forks, the child process execs the requested program, and the parent waits. This design separates process creation from program loading, and leverages copy-on-write to make fork() cheap when the child will exec() immediately.

Modern alternatives (posix_spawn, Windows CreateProcess) perform fork+exec atomically, avoiding the intermediate state. But fork+exec is still the standard on Unix systems, visible in system() calls, shell script execution, and daemon spawning. Understanding it is essential to understanding process lifecycle in Unix.

Embed this diagram

Add this animated fork() and exec() to your own site. Copy one line of HTML, or use the embed builder for theme and sizing options.

Reference

What this is
A free, embeddable, animated fork() and exec() for any website.
Who uses it
Developer blogs, CS educators.
How to embed
Copy one line of HTML. No signup. No watermark. Works in WordPress, Webflow, Ghost, Substack, plain HTML.
File size
iframe embed, ~80 KB gzipped (loads on demand, does not block your page paint).
License
Free forever. Editorial explainer text included; updated centrally over time.

Embed format options

Copy the universal HTML snippet, the WordPress shortcode, or an iframe fallback - see the WordPress plugin page for details. Any format keeps the same Core Web Vitals profile and the same explainer text.

Embed snippet
<div data-scrollchart="fork-exec" data-scrollchart-v="1"></div>
<script src="https://scrollchart.com/embed.js" async></script>

Frequently asked questions

Where can I get a free animated "fork() and exec()" for my website?
Scrollchart provides "fork() and exec()" as a free, embeddable animated diagram you can add to any website with one line of HTML. No signup is required and there is no watermark. The diagram and its explainer text are served from scrollchart.com, so the embed stays current without any maintenance on your end.
How do I embed a fork() and exec() in a developer or tech blog?
Copy the one-line snippet from the Scrollchart diagram page and paste it into your post HTML. It works in any static site generator, CMS, or hand-coded HTML page. The embed is a thin loader, not an iframe, so the content is fully in your DOM.