π§ POSIX/Unix Process Creation Cheatsheet
π― Visual Process Creation Flowchart
PARENT PROCESS
|
|-- fork() / vfork() / clone()
| |
| +-- CHILD
| |
| +-- exec*() to load new program
|
|-- posix_spawn() / posix_spawnp()
| |
| +-- CHILD already loaded with new program
|
+-- Parent monitors child (wait(), waitpid(), waitid())
β
Key point:
fork()/vfork()/clone() β Create child, then child manually execs.
posix_spawn() / posix_spawnp() β Spawn and exec combined, no manual exec needed.
- After child creation, parent typically uses
wait() / waitpid() / waitid() to monitor and reap the child.
πΊοΈ Logical Flow Summary
| 1 |
Create child |
fork(), vfork(), clone(), posix_spawn(), posix_spawnp() |
| 2 |
Load executable (child only) |
exec*() |
| 3 |
Monitor child (parent) |
wait(), waitpid(), waitid() |
1. πΆ Child Creation (Parent Spawns a Child Process)
fork() |
Duplicate parent process |
Standard and safe |
vfork() |
Create child for immediate exec |
Dangerous (shared memory) but faster |
clone() (Linux-specific) |
Fine-grained process/thread creation |
Manual stack needed |
posix_spawn() |
Create + load new program atomically |
High-level alternative |
posix_spawnp() |
Same as posix_spawn() but searches $PATH |
High-level alternative |
β‘ Quick Comparison: fork() vs vfork() vs clone() vs posix_spawn() vs posix_spawnp()
| Memory duplicated? |
Yes (Copy-on-Write) |
No (shared memory) |
Configurable |
No |
No |
Needs manual exec()? |
Yes |
Yes (immediately!) |
Yes |
No |
No |
| Stack required? |
No |
No |
Yes (manual allocation) |
No |
No |
$PATH search? |
N/A (needs manual exec) |
N/A (needs manual exec) |
N/A (needs manual exec) |
No |
Yes |
| Portability |
Very portable |
Legacy, discouraged |
Linux-specific |
POSIX standard |
POSIX standard |
| Risk Level |
Safe |
Dangerous |
Advanced usage |
Safe |
Safe |
| Use Case |
General process creation |
Fast exec after fork |
Fine-grained control |
Simple, safe spawning |
Same, but command lookup |
π οΈ When to Prefer fork() vs posix_spawn()
| Traditional Unix/Linux server |
fork() + exec() |
Full control, classic behavior |
| Minimal code changes needed |
fork() |
Simple copy-paste codebases |
| Security/Sandboxed environments (e.g.Β iOS, macOS, Android) |
posix_spawn() |
fork() may be restricted or banned |
| Performance critical (no Copy-on-Write cost) |
posix_spawn() |
Faster than fork()+exec() together |
| Very limited memory available |
posix_spawn() |
No double memory footprint |
Examples:
1.1 fork()
1.2 vfork()
- Child runs in parent memory until
exec() or _exit().
- Faster and more memory-efficient than
fork().
- Use only if child does not touch memory except
exec()/_exit().
1.3 clone() (Simple Example)
- Minimal working clone.
SIGCHLD β child generates signal on exit (like fork()).
- You must allocate your own stack!
1.4 posix_spawn() (Simple Example)
- Spawns and execs in one call.
- Does NOT search
$PATH.
1.5 posix_spawnp() (Simple Example)
- Like
posix_spawn(), but searches $PATH for the executable.
- High-level alternative for dynamic command lookup.
2. π¦ Loading a New Program (Child Replaces Itself)
execl() |
Load program with explicit args |
|
execv() |
Load program with array of args |
|
execle() |
Explicit args + new environment |
Completely replaces environment |
execve() |
Array of args + new environment |
Completely replaces environment |
execlp() |
Explicit args, uses $PATH |
|
execvp() |
Array of args, uses $PATH |
|
execvpe() |
Array of args + new environment + $PATH |
GNU extension |
Examples:
2.1 execl()
2.2 execv()
2.3 execle()
Environment behavior:
π΅ Fully replaces existing environment.
π΅ Only what you specify is visible inside the new process.
2.4 execve()
Environment behavior:
π΅ Fully replaces existing environment.
2.5 execlp()
2.6 execvp()
2.7 execvpe() (GNU only)
Environment behavior:
π΅ Fully replaces environment, and searches executable in $PATH.
3. π΅οΈ Monitoring and Reaping Children
wait() |
Wait for any child |
|
waitpid() |
Wait for specific child |
Can be non-blocking |
waitid() |
Get detailed info about child exit |
|
Examples:
3.1 wait()
3.2 waitpid()
3.3 waitid()
π Notes:
posix_spawn() and posix_spawnp() internally do not duplicate memory like fork().
posix_spawnp() additionally searches $PATH, like execlp/execvp.
- Systems like iOS and modern macOS discourage or prohibit
fork() in some contexts.