exec函数族的作用是根据指定的文件名找到可执行文件,并用它来取代调用进程的内容,换句话说,就是在调用进程内部执行一个可执行文件
exec函数族共6个函数:
1.
int execl(const char *path, const char *arg0, ... /*, (char *)0 */);
path:路径
arg0,arg1,...,argn:参数
#include
int main(int argc,char **argv){
int ret;
ret = execl ("/bin/ls", "ls", "-1", (char *)0);
}
2.
int execv(const char *path, char *const argv[]);
path:路径
argv:参数集
#include
int main(int argc,char **argv){
int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
ret = execv ("/bin/ls", cmd);
return ret;
}
3.
int execle(const char *path, const char *arg0, ... /*,(char *)0, char *const envp[]*/);
path:路径
arg0,arg1,...,argn:参数
envp:环境变量
#include
int main(int argc,char **argv){
int ret;
char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };
ret = execle ("/bin/ls", "ls", "-l", (char *)0, env);
return ret;
}
4.
int execve(const char *path, char *const argv[], char *const envp[]);
path:路径
argv:参数集
envp:环境变量
#include
int main(int argc,char **argv){
int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };
ret = execve ("/bin/ls", cmd, env);
return ret;
}
5.
int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);
file:文件
arg0,arg1,...,argn:参数
#include
int main(int argc,char **argv){
int ret;
ret = execlp ("ls", "ls", "-l", (char *)0);
return ret;
}
6.
int execvp(const char *file, char *const argv[]);
file:文件
argv:参数集
#include
int main(int argc,char **argv){
int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
ret = execvp ("ls", cmd);
return ret;
}
函数名都是由exec+性质(l-可变参数,v-参数集,e-带环境变量,p-文件)组成
int execl(const char *path, const char *arg0, ... /*, (char *)0 */);
int execv(const char *path, char *const argv[]);
int execle(const char *path, const char *arg0, ... /*,(char *)0, char *const envp[]*/);
int execve(const char *path, char *const argv[], char *const envp[]);
int execvp(const char *file, char *const argv[]);
int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);