一. 概念
当进程成功调用一种
exec
函数时,该进程的用户空间代码和数据完全被新程序替换,开始执行新程序而不再执行自己后面的代码。调用exec
并不创建新进程,所以调用exec
前后该进程的pid
并未改变。
二. 程序替换函数
#include<unistd.h> // 他们的头文件,调用的时候记得包含
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[]); // 这个是系统接口
/*
1. l(list) : 表示参数采用列表
2. v(vector) : 参数用数组
3. p(path) : 有p自动搜索环境变量PATH
4. e(environ) : 表示自己维护环境变量
5. 这些函数如果调用成功则加载新的程序从启动代码开始执行,不再返回。如果调用出错则返回-1所以exec函数只有出错的返回值而没有成功的返回值
6. 事实上,只有execve是真正的系统调用,其它五个函数最终都调用execve
*/
三. 六大函数调用使用例子
3.1 execl
// int execl(const char *path, const char *arg, ...);
// path是程序的路径(绝对路径和相对路径都可以)
// arg...是在命令行运行该程序是输入的参数,最后以NULL结束
void test_execl()
{
execl("/bin/ls", "ls", "-a", NULL); // Ubuntu的ls命令的绝对路径是/bin/ls,其他系统自测
}
3.2 exelp
// int execlp(const char *file, const char *arg, ...);
// file是程序文件的名字
void test_execlp()
{
execlp("ls", "ls", "-a", NULL);
}
3.3 execle
// int execle(const char *path, const char *arg, ...,char *const envp[]);
// envp是我们自己传过去的环境变量
void test_execle()
{
const char* envp[] =
{
"hello",
"你好",
"我是环境变量",
NULL
};
execle("./test", "test", NULL, envp); // 这里是用C写的一个叫test的可执行文件(其他语言写的也可以的)
}
3.4 execv
// int execv(const char *path, char *const argv[]);
void test_execv()
{
char* const argv[] =
{
"ls",
"-l",
NULL
};
execv("/bin/ls", argv);
}
3.5 execvp
// int execvp(const char *file, char *const argv[]);
void test_execvp()
{
char* const argv[] =
{
"ls",
"-l",
NULL
};
execvp("ls", argv);
}
3.6 execve
// int execve(const char *path, char *const argv[], char *const envp[]);
void test_execvp()
{
const char* envp[] =
{
"hello",
"你好",
"我是环境变量",
NULL
};
char* const argv[] =
{
"test",
NULL
};
execve("./test", argv, envp);
}
四. 实现一个minishell
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/wait.h>
#define NUM 64
#define CMD_NUM 16
int main()
{
char command[NUM];
while (1)
{
char* argv[CMD_NUM] = { NULL };
command[0] = '\0';
printf("[li@ThinkPad:~#] ");
fflush(stdout);
// 1. 获取输入的字符串
fgets(command, NUM, stdin);
command[strlen(command) - 1] = '\0';
const char *sep = " ";
// 2. 将字符串解析成命令一个个命令字符串
argv[0] = strtok(command, sep);
int i = 1;
while (argv[i] = strtok(NULL, sep))
{
i++;
}
// 3. 内建命令调用函数
if (strcmp(argv[0], "cd") == 0)
{
if (NULL != argv[1])
{
chdir(argv[1]);
}
continue;
}
// 4. 创建子进程调用替换函数去执行命令
if (0 == fork())
{
execvp(argv[0], argv);
exit(0);
}
int status = 0;
waitpid(-1, &status, 0);
//printf("exit code: %d\n", (status >> 8) & 0xff);
//printf("%s\n", command);
}
return 0;
}