exec函数族
- fork函数创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个函数。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并没改变
- 将当前的进程.text、.data替换为所要加载的程序的.text、.data,然后让进程从新的.text第一条指令开始执行,但进程ID不变,换核不换壳
其中有六种以exec开头的函数,统称exec函数
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., 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[]);
execlp 函数 (p表示在函数运行期间,要用到PATH环境变量)
【加载一个进程,借助PATH环境变量】
int execlp(const char *file, const char *arg,...);
【返回值】成功:无返回;失败:-1
【参数一】要加载的程序的名字。该函数需要配合PATH环境变量来使用,当PATH中所有目录搜索后没有参数一则出错返回
该函数通常用来调用系统程序。如:ls、date、cp、cat等命令
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
return -1;
}
else if(pid > 0)
{
sleep(1);
printf("I am parent\n");
}
else
{
//相当于在终端执行 ls /home/wlr -l
execlp("ls", "ls","/home/wlr", "-l", NULL);
}
return 0;
}
execl 函数 (l表示list,也就是参数列表)
【加载一个进程,通过 路径+程序名 来加载】
int execl(const char *path, const char *arg,...);
【返回值】成功:无返回;失败:-1
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
return -1;
}
else if(pid > 0)
{
sleep(1);
printf("I am parent\n");
}
else
{
//相当于在终端执行 ls /home/wlr -l
execl("/bin/ls", "ls","/home/wlr", "-l", NULL);
}
return 0;
}
【比较】
execlp("ls", "ls", "/", "-l", NULL); //使用程序名在PATH中搜索
execl("/bin/ls", "ls", "/", "-l", NULL); //使用参数一给的绝对路径搜索
execl("./while", "ls","/home/wlr", "-l", NULL); //执行自定义程序
【练习】将当前目录的ls信息,打印到文件中
这里会用到dup2函数,简单介绍一下dup2函数
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
fd = open("ps.out", O_WRONLY | O_CREAT, 0644);
if(fd < 0)
{
perror("open error\n");
return -1;
}
dup2(fd, STDOUT_FILENO);
execlp("ls", "ls", "-l", NULL); //dup2(3, 1) fd, stdout
perror("exec error\n"); //exec函数只有调用失败才会返回,调用成功便不会执行exec后面的代码
exit(0);
close(fd);
return 0;
}
exec函数族一般规律
exec函数一旦调用成功即执行新的程序,不返回。只有失败才返回,错误值为-1.所以我们通常直接在exec函数调用后直接调用perror()和exit(),无须if判断
【exec函数族后缀】
- l(list):命令行参数
- p(path):搜索file时使用path变量
- v(vector):使用命令行参数数组
- e(environment):使用环境变量数组,不使用进程原有的环境变量,设置新加载程序运行的环境变量
事实上,只有execve是真正的系统调用,其他五个函数最终都调用execve。这些函数之间关系如下