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().
There is no
exec
system call -- this is usually used to refer to all theexecXX
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 theargv[]
array of the new program.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 calls with
e
in the name take an extra argument to provide the environment of the new program; otherwise, the program inherits the current process's environment.The calls with
p
in the name search thePATH
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.Within the exec family, there are functions that vary slightly in their capabilities and how they are called:
Functions that contain the letter p in their names (
execvp
andexeclp
) 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.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’svarargs
mechanism.Functions that contain the letter e in their names (
execve
andexecle
) 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 formVARIABLE=value
.Source
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.
Since all of these function belongs to exec() family, let me
differentiate
according toextra 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 willsearch for program file
inPATH
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 willsearch for program file
inPATH
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
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
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.
Same as execv but the arguments are provided as an individual string (separated by commas) instead of an array/vector.
Same as execv but it permits to specify environment variables for new process image.
Same as execl but it permits to specify environment variables for new process image.
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
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
orprintenv
on the shell or terminal you will get a list standard environment variables.Use
man exec
and read:execv
So you pass an array as parameters
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:
You are calling a file, without path, so it expects you to be already in the right
path
before calling.Last but not least:
Similar to previous one, but now you have two arrays, for arguments and environment variables.