What is the difference between the functions of the exec family of system calls like exec and execve?

Viewed 40707

I have been following a system programming course recently and I came through the system calls exec() and execve(). So far I cannot find any difference between these two, Even the Wikipedia does not give a clear explanation, so is there a difference between exec() and execve().

And someone please could give brief descriptions about exec family system calls such as execl(), execv(), execle(), execvp().

7 Answers

Main Idea

exec() family of functions replaces existing process image with a new process image. This is a marked difference from fork() system call where the parent and child processes co-exist in the memory.

exec() family of functions

int execv (const char *filename, char *const argv[])

The filename is the file of the new process image.

argv represents an array of null-terminated strings.The last element of this array must be a null pointer.

int execl (const char *filename, const char *arg0, …)

Same as execv but the arguments are provided as an individual string (separated by commas) instead of an array/vector.

int execve (const char *filename, char *const argv[], char *const env[])

Same as execv but it permits to specify environment variables for new process image.

int execle (const char *filename, const char *arg0, …, char *const env[])

Same as execl but it permits to specify environment variables for new process image.

int execvp (const char *filename, char *const argv[])

Same as execv function but it searches standard environment variable PATH to find the filename if the filename does not contain a slash.

Here is a list of standard environment variable:

https://www.gnu.org/software/libc/manual/html_node/Standard-Environment.html#Standard-Environment

int execlp (const char *filename, const char *arg0, …)

Same as execl function except the fact that if performs the filename search as the execvp function.

Note

In a Linux system, if you type env or printenv on the shell or terminal you will get a list standard environment variables.

To answer the first part of your question, in the context of Linux specifically, there is only one system call and it's execve (not exec). The remainder of the so called "exec family" (execl, execle, execv, execve, execvp, etc.) are all GLIBC wrappers for for the kernel's system call, that is execve.

Within the exec family, there are functions that vary slightly in their capabilities and how they are called:

  1. Functions that contain the letter p in their names (execvp and execlp) accept a program name and search for a program by that name in the current execution path; functions that don’t contain the p must be given the full path of the program to be executed.

  2. Functions that contain the letter v in their names (execv, execvp, and execve) accept the argument list for the new program as a NULL-terminated array of pointers to strings. Functions that contain the letter l (execl, execlp, and execle) accept the argument list using the C language’s varargs mechanism.

  3. Functions that contain the letter e in their names (execve and execle) accept an additional argument, an array of environment variables.The argument should be a NULL-terminated array of pointers to character strings. Each character string should be of the form VARIABLE=value.

Source

Related