Understanding Shell, fork, exec, ls, and Parent-Child Processes
Let's understand Shell, fork, exec, ls, and Parent-Child Processes
🌟 Scenario: You open a terminal on your computer and type this command:
ls -l
Then a list of files scrolls onto the screen.
The question is: How does the system do this?
The answer: The Shell (like bash) quietly "had a baby" and let this child process run the ls command!
Step 1: What is the Shell?
-
The Shell is the "interpreter" between you and the operating system.
-
The
$or#prompt you see in the terminal is backed by a Shell process (likebash). -
It's always waiting for you to input commands, like
ls,cd,python…
✅ At this moment, the Shell is the parent process.
Step 2: You typed ls -l, what does the Shell do?
The Shell thinks:
"This isn't my built-in function (like
cd), I need to find the external program/bin/lsto do it. But I can't just turn myself intols—that would kill me! So I need to first copy myself, then let the copy becomels."
So it does two things:
🔧 1. fork() —— "Clone Yourself"
pid_t pid = fork();
-
After calling
fork(), the system instantly creates a copy of the identical Shell process. -
Now there are two Shells running:
- The original Shell (parent process): Continues waiting to finish up;
- The new copy of the Shell (child process): Prepares to transform!
💡 Key Point: The return value of
fork()is different!
- In the parent process,
pid = Child Process's PID(e.g., 5001);- In the child process,
pid = 0.
So the code can fork like this:
if (pid == 0) {
// I am the child process → I will turn into ls!
} else {
// I am the parent process → I'll wait for the child to finish its job
}
🔧 2. exec() —— "Shed the Shell, Transform into ls"
The child process says: "I don't want to be the Shell anymore, I want to be ls!"
So it calls:
execve("/bin/ls", ["ls", "-l"], envp);
-
The
execfamily of functions will replace the current process's "soul" (code, data) entirely with the content of/bin/ls. -
From then on, this child process is no longer the Shell, but the real
lsprogram! -
It starts working: reads the directory, formats the output, prints to the screen…
✅ Note:
execdoes not return upon success! Because "you are no longer you."
🔄 Step 3: Parent Process (Shell) Waits & Cleans Up
-
The parent process (the original Shell) will call
wait()afterfork():wait(NULL); // Wait for the child process to end -
Only after
lsfinishes its job and exits does the Shell display the$prompt again, waiting for your next command.
🖼️ The Complete Flowchart (Text Version)
You type: ls -l
↓
Shell (Parent Process, PID=1000)
↓ calls fork()
├── Parent Process (PID=1000) → wait() and waits
└── Child Process (PID=1001, PPID=1000)
↓ calls execve("/bin/ls", ...)
→ Becomes the ls program
→ Prints file list
→ exit(0)
↓
Parent process is notified → Continues to display $
❓ So why do we need fork + exec? Can't we just exec directly?
No! Because:
-
If the Shell directly
execs intols, then the Shell itself is gone! -
After you finish executing
ls, the terminal would be "dead" and unable to accept more commands.
So it must be:
First fork a stand-in, let the stand-in be sacrificed (exec), while the original continues to live.
This is the classic design philosophy of Unix/Linux: Simple, Composable, Reliable.
🧪 You Can Try It Yourself!
Run this small script in the terminal (Python version, more intuitive):
import os
print("=== Start ===")
pid = os.fork()
if pid == 0:
# Child process
print(f"I am the child process, PID={os.getpid()}, I will become ls!")
os.execv("/bin/ls", ["/bin/ls", "-l"])
# Note: After execv succeeds, the line below will never execute!
print("This line will not be printed!")
else:
# Parent process
print(f"I am the parent process (Shell), child process PID={pid}")
os.wait() # Wait for child process to end
print("=== End ===")
Save it as demo.py and run:
python3 demo.py
You will see:
- First, parent/child information is printed;
- Then, the result of
ls -l; - Finally, return to the prompt.
Congratulations! You've personally created a pair of parent-child processes! 🎉
❤️ Final summary in one sentence:
Parent process = the original Shell; Child process = a temporary worker copied from the Shell specifically to run
ls.fork()is responsible for "giving birth,"exec()is responsible for "letting the child change jobs."