以ls -l为例
#include
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execl(const char *path, const char *arg, ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execl(const char *file, char *const argv[]);
int execl(const char *path, char *const argv[], char * const envp[]);
①execl
execl(“/bin/ls”, “ls”, “-l”, NULL);
②execlp
在上面的基础上去掉了ls的路径/bin/
execlp(“ls”, “ls”, “-l”, NULL);
③execv
把”ls”, “-1”, NULL这些命令行参数通过指针数组str传给exec函数
char *str[] = {“ls”, “-l”, NULL};
execv(“/bin/ls”, str);
④execvp
在上面的基础上去掉了ls的路径/bin/
char *str[] = {“ls”, “-l”, NULL};
execvp(“ls”, str);
⑤execle
char *arrEnv[] = {“PATH=/bin:/usr/bin”, “TERM=console”, NULL};
execle(“/bin/ls”, “ls”, “-l”, NULL, arrEnv);
⑥execve
char *str[] = {“ls”, “-l”, NULL};
char *arrEnv[] = {“PATH=/bin:/usr/bin”, “TERM=console”, NULL};
execve(“/bin/ls”, str, arrEnv);