Course contentsShow
← Computer Science
Lesson 1224 of 2,87228. Operating Systems FundamentalsFree lesson

Process Execution: exec() Family

Learn how exec() replaces process memory with a new program while keeping the same PID.

Process Execution: exec() Family

What You'll Learn

You'll learn how the exec() family of system calls replaces a process's memory with a new program while keeping the same process ID (PID).

The Core Idea

After you create a child process with fork(), you often want that child to run a different program than the parent. That's where exec() comes in.

The exec() system call replaces the entire memory contents of the calling process with a new program. Think of it like this: imagine you have a book (the current process). When you call exec(), you rip out all the pages and replace them with pages from a completely different book—but the book cover (the PID) stays the same.

How It Works

When exec() is called:

  1. The process's current code, data, heap, and stack are discarded
  2. A new program is loaded from disk into memory
  3. The new program's code and data replace the old ones
  4. Execution begins at the new program's entry point
  5. The PID remains unchanged—it's still the same process to the OS

This is why fork() and exec() work so well together: fork() creates a new process, and exec() gives it a new job to do.

Why Keep the Same PID?

The parent-child relationship established by fork() is preserved. The parent still knows the child's PID and can wait for it to finish. From the operating system's perspective, it's managing the same process—just with different code running inside it.

Key Takeaway: The exec() family replaces a process's entire memory image with a new program while preserving the process ID, allowing a child process to run a completely different program than its parent.