exec函数族使用说明
#include <unistd.h>
int execl(const char *pathname, const char *arg, ...)
int execle(const char *pathname, const char *arg, ..., char *const envp[])
int execv(const char *pathname, char *const argv[])
int execve(const char *pathname, char *const argv[], char *const envp[])
int execlp(const char *filename, const char *arg, ...)
int execvp(const char *filename, char *const argv[])
l 代表 list
命令的参数是列举出来的execl("/bin/ls","ls","./",NULL)
,第一个参数是路径,后面的参数组合起来就是命令行要输入的命令
v代表vector
命令的参数是用字符串数组的形式给出
char *const ps_argv[] ={"ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL};
execv("/bin/ps", ps_argv);
e 代表 environment
传递新的环境变量
char *const ps_envp[] ={"PATH=/bin:/usr/bin", "TERM=console", NULL};
execle("/bin/ps", "ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL, ps_envp);
p代表PATH
在$PATH
环境变量里搜索程序名
char *const ps_argv[] ={"ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL};
execvp("ps", ps_argv);