在百度快照看到这篇文章,为了保护文物,特转之留念。
原文链接:http://fenglei.blog.com.cn/archives/2005/39431.shtml (运气好的话或许还能复活。)

很早就知道了exec这个函数家族,也知道它们的作用,但是没有认真分析过它们之间的区别,也不知道该在什么时候用哪个,近来看到这里,顺便总结出来,以方便自己并方便大家。总结如下:
exec家族一共有六个函数,分别是:

int execl(const char *path, const char *arg, ……);
int execle(const char *path, const char *arg, …… , char * const envp[]);
int execv(const char *path, char *const argv[]);
int execve(const char *filename, char *const argv[], char *const envp[]);
int execvp(const char *file, char * const argv[]);
int execlp(const char *file, const char *arg, ……);

这些函数之间的
第一个区别是:
前四个取路径名做为参数,后两个取文件名做为参数,如果文件名中不包含 “/” 则从PATH环境变量中搜寻可执行文件, 如果找到了一个可执行文件,但是该文件不是连接编辑程序产生的可执行代码文件,则当做shell脚本处理。
第二个区别:
前两个和最后一个函数中都包括“ l ”这个字母 ,而另三个都包括“ v ”, ” l “代表 list即表 ,而” v “代表 vector即矢量,也是是前三个函数的参数都是以list的形式给出的,但最后要加一个空指针,如果用常数0来表示空指针,则必须将它强行转换成字符指 针,否则有可能出错。,而后三个都是以矢量的形式给出,即数组。
最后一个区别:
与向新程序传递环境变量有关,如第二个和第四个以e结尾的函数,可以向函数传递一个指向环境字符串指针数组的指针。即自个定义各个环境变量,而其它四个则使用进程中的环境变量。
exec家族虽然有六个函数,但根据函数名还是可以很容易的区别开来,简单说到这里。
本文参考一本UNIX编程书籍,很可惜我不知道书叫什么名字。

–转载结束–

我是看Beginning Linux Programming 3rd时,对这个家族的函数产生了兴趣,特百度之查明区别。下面是英文说明:

There is a whole family of related functions grouped under the exec heading. They differ in the way
that they start processes and present program arguments. An exec function replaces the current process
with a new process specified by the path or file argument.
#include
char **environ;
int execl(const char *path, const char *arg0, …, (char *)0);
int execlp(const char *file, const char *arg0, …, (char *)0);
int execle(const char *path, const char *arg0, …, (char *)0, 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[]);
In general, using system is a far from ideal way to start other processes, because it
invokes the desired program using a shell. This is both inefficient, because a shell is
started before the program is started, and also quite dependent on the installation
for the shell and environment that are used. In the next section, we see a much better
way of invoking programs, which should almost always be used in preference to the
system call.
453
Processes and Signals
b544977 Ch11.qxd 12/1/03 8:56 AM Page 453These functions belong to two types. execl, execlp, and execle take a variable number of arguments
ending with a null pointer. execv and execvp have as their second argument an array of strings. In both
cases, the new program starts with the given arguments appearing in the argv array passed to main.
These functions are usually implemented using execve, though there is no requirement for it to be done
this way.
The functions with names suffixed with a p differ in that they will search the PATH environment variable
to find the new program executable file. If the executable isn’t on the path, an absolute file name, includ-
ing directories, will need to be passed to the function as a parameter.
The global variable environ is available to pass a value for the new program environment.
Alternatively, an additional argument to the functions execle and execve is available for passing an
array of strings to be used as the new program environment.
If we wish to use an exec function to start the ps program, we can choose from among the six exec family
functions, as shown in the calls in the code fragment that follows