Learn exec family of commands
๐ง The exec Family of System Calls in UNIX/Linux
๐ฏ Purpose
The exec family of functions is used to replace the current process image with a new program.
In simple terms:
execdoes not create a new process — it loads a new program into the current process (overwriting it).
So typically:
-
A parent process calls
fork()to create a child. -
The child calls an
execfunction to replace itself with a new program.
This is how most programs (like your shell) launch new applications.
๐งฉ Analogy
Think of a process as a person reading a book (the program).
fork() makes a clone of the reader (child).
exec() makes the reader throw away the old book and start reading a new one.
⚙️ General Behavior
When an exec function is called:
-
The current code, data, and stack of the calling process are replaced by those of the new program.
-
The process ID (PID) stays the same.
-
Open file descriptors may remain open (depending on flags).
-
Environment variables can be passed to the new program.
exec never returns — because the old program has been completely replaced.๐งฉ List of Common exec Variants
| Function | Description | Search Path | Argument Type |
|---|---|---|---|
execl() | Takes a list of arguments | No | List |
execlp() | Takes a list, searches PATH | Yes | List |
execle() | Takes a list and environment | No | List |
execv() | Takes an array (vector) of args | No | Array |
execvp() | Takes an array, searches PATH | Yes | Array |
execvpe() | Takes an array and environment | Yes | Array |
๐งฉ Syntax
Each variant has a slightly different parameter style.
1️⃣ execl()
-
path: Full path to the executable. -
arg: The command-line arguments (arg[0] is the program name).
2️⃣ execv()
-
argv: Array of strings (argv[0],argv[1], …, NULL).
3️⃣ execlp() / execvp()
Same as above but automatically searches for the executable in the directories listed in the PATH environment variable.
4️⃣ execle() / execvpe()
Similar to execl() or execvp(), but with an extra environment array parameter (envp).
๐ง Example 1 – Using execl()
This replaces the current process with /bin/ls.
Compile & Run
Output
✅ The line after execl() is not printed because the process was replaced by /bin/ls.
๐ง Example 2 – Using execvp() with Arguments
execvp() is the most common version because it:
-
Accepts an array of arguments
-
Searches in the
PATH
Output
๐ง Example 3 – Using fork() + execvp()
This is the most common use case in Operating Systems labs:
Output
๐ง Example 4 – Using execle() with Custom Environment
Output
⚙️ Behavior Summary
| Property | Description |
|---|---|
| PID | Same as before exec |
| Memory | Old program replaced |
| Open Files | Usually retained unless FD_CLOEXEC set |
| Signal Handlers | Reset to default |
| Environment | Passed or replaced |
| Return Value | Returns -1 on failure only |
๐งฉ Common Usage in Practice
In shells (like bash):
-
The shell forks a new process.
-
The child process calls
execvp()to run the user’s command. -
The parent shell waits for it to finish.
This model (fork() → exec() → wait()) forms the core of UNIX process management.
๐งฉ Error Handling
Always handle failures:
⚙️ When to Use Which
| Function | Use When |
|---|---|
execl() | You know full path and fixed number of args |
execv() | You want to pass args as an array |
execlp() | You want PATH search + list of args |
execvp() | You want PATH search + array of args (most common) |
execle() / execvpe() | You want to set custom environment variables |
๐ Verification in Lab
While your program is running, use:
to observe how the child process is replaced with the new executable.
๐งพ Summary
| Function | Searches PATH | Pass Environment | Args Type |
|---|---|---|---|
| execl() | No | No | List |
| execv() | No | No | Array |
| execlp() | Yes | No | List |
| execvp() | Yes | No | Array |
| execle() | No | Yes | List |
| execvpe() | Yes | Yes | Array |
๐ง In Short:
-
fork()→ creates a new process -
exec()→ replaces the current process with another program -
wait()→ synchronizes parent and child -
Together, they form the UNIX process control triad
Comments
Post a Comment