50

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().

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
buddhi weerasinghe
  • 677
  • 2
  • 9
  • 17
  • 3
    Wikipedia isn't the primary source of information for UNIX system calls, but the man-pages are: For the `exec*()`-family of functions you might like to read here: http://man7.org/linux/man-pages/man3/execl.3.html – alk Dec 29 '13 at 09:17
  • Read also [Advanced Linux Programming](http://advancedlinuxprogramming.com/) – Basile Starynkevitch Dec 29 '13 at 09:44
  • 2
    possible duplicate of [What are the different versions of exec used for in C++?](http://stackoverflow.com/questions/5769734/what-are-the-different-versions-of-exec-used-for-in-c) – Ciro Santilli OurBigBook.com May 09 '15 at 15:58

7 Answers7

78

There is no exec system call -- this is usually used to refer to all the execXX calls as a group. They all do essentially the same thing: loading a new program into the current process, and provide it with arguments and environment variables. The differences are in how the program is found, how the arguments are specified, and where the environment comes from.

  • The calls with v in the name take an array parameter to specify the argv[] array of the new program. The end of the arguments is indicated by an array element containing NULL.

  • The calls with l in the name take the arguments of the new program as a variable-length argument list to the function itself. The end of the arguments is indicated by a (char *)NULL argument. You should always include the type cast, because NULL is allowed to be an integer constant, and default argument conversions when calling a variadic function won't convert that to a pointer.

  • The calls with e in the name take an extra argument (or arguments in the l case) to provide the environment of the new program; otherwise, the program inherits the current process's environment. This is provided in the same way as the argv array: an array for execve(), separate arguments for execle().

  • The calls with p in the name search the PATH environment variable to find the program if it doesn't have a directory in it (i.e. it doesn't contain a / character). Otherwise, the program name is always treated as a path to the executable.

  • FreeBSD 5.2 added another variant: execvP (with uppercase P). This is like execvp(), but instead of getting the search path from the PATH environment variable, it's an explicit parameter to the function:

int execvP(const char *file, const char *search_path, char *const argv[]);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The only difference between the above system calls is with the parameters. Is that the case? If so, is the ultimate outcome of all the exec family system calls to execute a program (with different parameters)? – buddhi weerasinghe Dec 29 '13 at 08:37
  • 8
    Actually, the *only* [system call](http://en.wikipedia.org/wiki/System_call) is [execve(2)](http://man7.org/linux/man-pages/man2/execve.2.html) and all other `exec*` functions are wrapping it. – Basile Starynkevitch Dec 29 '13 at 09:43
  • I know that, but the distinction is not really important unless you're a kernel developer. – Barmar Dec 29 '13 at 09:44
  • Thanks. Is there difference between the popularity of these exec* functions? I haven't seen much examples, but it seems to me execlp() (and maybe execvp()) is used most often? – Tim Nov 28 '18 at 15:23
26

Use man exec and read:

The execv(), execvp(), and execvpe() functions provide an array of pointers to 
null-terminated strings that represent the argument list available to the new program. 
The first argument, by convention, should point to the filename associated with the file 
being executed. The array of pointers must be terminated by a NULL pointer. 

execv

int execv(const char *path, char *const argv[]);

So you pass an array as parameters

int execle(const char *path, const char *arg,
              ..., char * const envp[]);

Almost the same, but not as an array, but rather as a list of values (strings), followed by an array the designates the environment.

Here:

int execvp(const char *file, char *const argv[]);

You are calling a file, without path, so it expects you to be already in the right path before calling.

Last but not least:

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

Similar to previous one, but now you have two arrays, for arguments and environment variables.

Noam Rathaus
  • 5,405
  • 2
  • 28
  • 37
  • 3
    The only difference between the above system calls is with the parameters. Is that the case? If so, is the ultimate outcome of all the exec family system calls to execute a program (with different parameters)? – buddhi weerasinghe Dec 29 '13 at 08:36
  • 3
    Just different parameters, nothing else is different – Noam Rathaus Dec 29 '13 at 08:39
17

Since all of these function belongs to exec() family, let me differentiate according to extra characters with the meanings,

1.execve():

p : not present => name of the program to run will be taken from pathname

v : present => argument will be passed as array

e : present => environment will be taken from envp argument

2.execle():

p : not present => name of the program to run will be taken from pathname

l : present => argument will be passed as list

e : present => environment will be taken from envp argument

3.execlp():

p : present => name of the program to run will be taken from filename specified or system will search for program file in PATH variable.

l : present => argument will be passed as list

e : not present => environment will be taken from caller's environ

4.execvp():

p : present => name of the program to run will be taken from filename specified or system will search for program file in PATH variable.

v : present => argument will be passed as array

e : not present => environment will be taken from caller's environ

5.execv():

p : not present => name of the program to run will be taken from pathname

v : present => argument will be passed as array

e : not present => environment will be taken from caller's environ

6.execl():

p : not present => name of the program to run will be taken from pathname

l : present => argument will be passed as list

e : not present => environment will be taken from caller's environ

Prateek Joshi
  • 3,929
  • 3
  • 41
  • 51
4

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.

Asif
  • 664
  • 1
  • 7
  • 14
3

The arguments are different for these functions.

  • The function execl, execlp, and execle require each of the command line arguments to the new program to be specified as separate arguments.

  • The execv, execvp and execve, we have to build an array of pointers to the arguments, and the address of this array is the argument to these three functions.

  • The execve, execle functions allow us to pass the pointer to an array of pointers to the environment strings. The other four functions use the environ variable in the calling process to copy the existing environment to the program.

  • The letter p means that the functions takes a file name argument and uses the PATH environment variable to find the executable file.
  • The letter l means that the function takes a list of arguments and is mutually exclusive with the letter v, which means that it takes an argv[] vector.
  • The letter e means that the function takes an envp[] array instead of using the current environment.

  • The new program inherits the following additional features from the calling process.

    Process ID and the Parent Process ID
    Real user ID and Real Group ID
    Supplementary group IDs
    Process group ID
    Session ID
    Controlling terminal
    Time left until alarm clock
    Current working directory
    Root directory
    File mode creation mask
    File locks
    Process signal mask
    Pending signals
    Resource limits
    Values for tms_utime, tms_stime, tms_cutime, and tms_cstime.
  • The real user ID and the real group ID remain the same across the exec but the effective IDs can change, depending on the status of the set-user-id and the set-group-id bits for the program file executed.
2

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.

0

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

rodgdor
  • 2,530
  • 1
  • 19
  • 26