Skip to content
Tutorials Center
My learning

Topics

C Reference ยท POSIX and Systems

C exec family: execl, execv, execvp and more

Reference for the exec family (execl, execlp, execv, execvp, execve): replace the running program with another, how the letters differ, and how to handle failure.

The exec functions replace the program running in the current process with a different executable. The process ID stays the same, but code, stack and heap are replaced, so on success the call never returns. Open file descriptors survive unless they are marked close-on-exec, which is how shells wire up redirections and pipelines.

Because a successful exec destroys the caller, programs usually combine it with fork: the child calls exec, the parent waits with waitpid. If exec returns at all, it failed, and errno says why.

C
#include <unistd.h>

execlp("echo", "echo", "hello", (char *)NULL);   /* only returns on error */

Try it

C fork + execlp + waitpid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C execvp with an array, and a failing exec

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#include <unistd.h>

int execl (const char *path, const char *arg0, ... /*, (char *)NULL */);
int execlp(const char *file, const char *arg0, ... /*, (char *)NULL */);
int execle(const char *path, const char *arg0, ... /*, (char *)NULL, char *const envp[] */);
int execv (const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *path, char *const argv[], char *const envp[]);
Name Description
path Path of the executable (absolute or relative to the working directory).
file Program name; if it contains no slash, the directories in PATH are searched.
arg0, ... The argument list as separate parameters, ending with a null pointer cast to char *.
argv A null-terminated array of argument strings; argv[0] is the program name by convention.
envp A null-terminated array of "NAME=value" strings for the new environment.

Returns: nothing on success. -1 on failure, with errno set (ENOENT no such file, EACCES not executable or permission denied, ENOEXEC bad format, E2BIG arguments plus environment too large, ENOMEM).

Reading the suffix letters

Letter Meaning
l Arguments are passed as a list of parameters
v Arguments are passed as a vector (array)
p The PATH is searched for the file name
e An explicit environment array is supplied (otherwise the caller's is inherited)

execve is the real system call; the others are library wrappers around it (execlp and execvp add the PATH search).

Usage notes

  • Terminate the list with (char *)NULL. A bare NULL can be defined as plain 0, which is an int and breaks the variadic call. In C23 nullptr also works but the cast is portable everywhere.
  • Repeat the program name as arg0. The program sees it as argv[0]; omitting it shifts every argument by one.
  • Flush stdio first. Unflushed buffered output is lost when the image is replaced, because no atexit handlers or flushes run.
  • A failed exec returns in the child: report the error and call _exit (not exit) so the child does not flush the parent's inherited buffers.
  • With p variants a name without a slash is searched along PATH. Do not use them with untrusted names in privileged programs; use an absolute path.
  • Never pass user input to system() or through sh -c unless you quote it. Calling execv with a separate argument array avoids the shell and its injection problems.
  • Descriptors stay open across exec unless opened with O_CLOEXEC or marked FD_CLOEXEC. Close or mark everything the new program should not inherit.
  • An exec of a script with a #! first line starts the named interpreter; a file with no such line may fail with ENOEXEC (execlp/execvp retry through sh in that case).
  • posix_spawn combines fork and exec and is cheaper in large processes.