fork 创建子进程之后执行的是和父进程相同的程序(可能执行不同的代码分支)。
子进程调用exec函数执行另一程序,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执行。
exec并不传创建新进程,所以调用exec前后该进程的id并未改变。
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, ... , const char * 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函数
加载一个进程,借助PATH环境变量
// 成功:无返回; 失败:-1
int execlp(const char *file, const char *arg, ...);
参数1:要加载的程序的名字。该函数需要配合PAT环境变量来使用,当PATH中所有目录搜索后没有参数1则出错返回。
该函数通常用来调用系统程序。如:ls、date、cp、cat等命令。
// 一个栗子
execlp("ls","ls","-l","-a",NULL);
execl函数
加载一个进程,通过 路径 + 程序名 来加载。
// 成功:无返回; 失败:-1
int execl(const char *path, const char *arg, ...);
可以用来加载自定义的程序。
// 一个栗子
execl("/bin/ls", "ls", "-l, "-F",NULL);
// 自定义程序栗子
execl("./main", "main", NULL);
execvp函数
加载一个进程,使用自定义环境变量env
int execvp(const char *file, char *const argv[]);
变量形式:①... ②argv[]
变参终止条件:①NULL结尾 ②固定指定
execvp 与 execlp参数形式不同,原理一致。
一般规律
l(list) | 命令行参数列表 |
p(path) | 搜索 file 时使用 path 变量 |
v(vector) | 使用命令行参数数组 |
e(environment) | 使用环境变量数组,不实用进程原有的环境变量,设置新加载程序运行的环境变量 |
事实上,只有execve是真正的系统调用,其他五个函数最终都调用execve,所以execve在man手册的第2节,其他函数在man手册第3节。这些函数之间的关系:
练习:打印当前进程信息到文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(void) {
int fd;
fd = open("ps.out", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
perror("open ps.out error");
exit(1);
}
// 复制文件描述符
dup2(fd, STDOUT_FILENO);
execlp("ps", "ps", "ax", NULL);
// 出错处理
perror("exec error");
exit(1);
// 不会执行到这里
// close(fd);
return 0;
}