This article explains how the `fork()` and `exec()` system calls work on Unix systems. You might already be familiar with this, or you may not. When I first learned about it a few years ago, I was really impressed by how elegant and efficient the process is.
The basic idea is that to start a new process, you don't just call a simple function like `start_process(["ls", "-l", "my_cool_directory"])`. That’s how things work in DOS or Windows, but not in Linux. While there is a system call called `posix_spawn` that does something similar, we’ll focus on the more traditional approach using `fork()` and `execve`.
In Linux, every process exists as part of a process tree. You can see this by running the `pstree` command. The root of the tree is `init`, which has a process ID of 1. Every other process has a parent, and a single process can have multiple children.
Let’s say you want to run the `ls` command to list a directory. You don’t just start `ls` directly. Instead, you first create a child process using `fork()`, which is a copy of the current process. Then, you use `exec()` to replace the child's memory space with the `ls` program.
Here’s how it works step by step:
- Initially, you have:
```
My parent
|- me
```
- After calling `fork()`, you get:
```
My parent
|- me
|-- clone of me
```
- Then, the child process runs `exec("ls")`, so it becomes:
```
My parent
|- me
|-- ls
```
- Once `ls` finishes, it becomes a **zombie** process, waiting for the parent to check its exit status using `wait()`. Once the parent collects the exit code, the zombie is removed from the process table.
Now, if you're writing a shell, implementing `fork()` and `exec()` is essential. With some knowledge of C or Python, you can build a simple shell in just a few hours. I’ve done it myself, and it’s a great learning experience.
Here’s a simplified version of the code in C:
```c
int pid = fork();
if (pid == 0) {
// Child process
execve("/bin/ls", args, envp);
} else if (pid == -1) {
// Fork failed
} else {
// Parent process
wait(NULL); // Wait for child to finish
}
```
What do we mean by “the brain is eaten� When `exec()` is called, the child process replaces its entire memory space with the new program. This includes everything except for certain attributes like file descriptors, environment variables, and signal handlers — all of which are inherited from the parent.
However, the following are replaced:
- Memory (address space)
- Registers
- The executable itself
So, even though the child process starts as a copy of the parent, after `exec()`, it becomes a completely different process.
One key optimization in Linux is **copy-on-write (COW)**. When `fork()` is called, the kernel doesn’t immediately copy all the memory. Instead, it shares the same memory pages between the parent and child. Only when one of them tries to write to the memory does the system make a copy. This makes `fork()` very efficient, even for large processes.
Why should you care? Understanding these details can help you debug programs, especially when dealing with signal handlers, environment variables, or unexpected behavior. For example, Python automatically ignores `SIGPIPE`, which can cause strange behavior compared to running the same program from a shell. Knowing how processes are created and managed helps you avoid such pitfalls.
In short, `fork()` and `exec()` are fundamental to how Unix systems launch programs. They’re powerful, efficient, and worth understanding if you want to build robust software or dive deeper into operating systems.
New AC Contactor
New type AC Contactor, our model is BC1-D series. New AC Contactor is the new designed AC Contactors, which with a new face appear to the user with high quality product. This product uses the fresh PA66 nylon material, no any recycled material mixed. Have high protection level for the fire-resistant. White body, yellow inserting core, looks more charming. All the metallic parts are standards of quality control.
The AC Contactor is suitable for using in the circuits up to the rated voltage 660VAC 50Hz or 60Hz, rated current up to 95A, for making and breaking and frequently start, controlling the AC motor. Combined with the auxiliary contactor group, air delayer, machine interlocking devices etc. It is combined into the delay contactor, mechanical interlocking contactor, star-delta starter, with the thermal relay, it is combined into the electromagnetic starter.

AC Contactor,Magnetic AC Contactor,ACMagnetic Electric Contactor,Electrical Power AC Contactor
Ningbo Bond Industrial Electric Co., Ltd. , https://www.bondelectro.com