exec函数族

exec函数族

让父子进程执行不相干的操作

能够替换进程地址空间中的源代码.txt段

当前程序中调用另外一个应用程序

是否需要exec函数族判断: 首先考虑exec之前需要fork

返回值: (1) 如果函数执行成功, 不返回; (2) 执行失败, 打印错误信息, 退出当前进程

头文件: #include <unistd.h>

系统环境变量: extern char **environ;

执行指定目录下的程序

int execl(const char *path, const char *arg, ...); 常用
一般执行自己写的程序
参数:
  path: 要执行的程序的绝对路径
  变参args: 要执行的程序的需要的参数
    第一arg: 占位, 任意, 不能为空, 一般写执行程序名
    后边的arg: 命令的参数
    参数写完之后: NULL

execl("/home/robin/a.out", "a.out", NULL);

int execv(const char *path, char *const argv[]);
参数:

char* argv[] = {"a.out", NULL};
execv("/home/robin/a.out", argv);

执行PATH环境变量能够搜索到的程序

int execlp(const char *file, const char *arg, ...); 常用
执行系统自带的程序, 即不需用绝对路径
参数:
  file: 执行的命令的名字
  变参arg:
    第一arg: 占位
    后边的arg: 命令的参数
    参数写完之后: NULL
    execpl执行自定义的程序: file参数绝对路径
示例

execlp("ls", "ls", "-la", NULL);

int execvp(const char *file, char *const argv[]);
示例

char* arg[] = {"ls", "-a", "-l", NULL};
execvp("ls", arg);

执行指定路径, 指定环境变量下的程序

int execle(const char *path, const char *arg, ..., char * const envp[]);
从用户指定的目录中搜索指定的命令
参数:
  path: 执行的程序的绝对路径
  arg: 执行的程序的参数
  envp: 用户自己指定的搜索目录, 代替PATH
示例

char* env[] = {"/bin", "./", "/home/robin", NULL};	// 自己指定环境变量,对应PATH
execle("/bin/ls", "ls", "-l", "-a", NULL, env);

int execve(const char *filename, char *const argv[], char *const envp[]);
示例

char* argv[] = {"ps", "a", "u", "x", NULL};
char* env[] = {"/bin", "/home/", "./", NULL};
execve("/bin/ps", argv, env);

实例程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[]) {
	pid_t pid;

	pid = fork();
	if(pid == -1) {
		perror("fork");
		exit(1);
	}

	if(pid == 0) {
		// 子进程执行另外一个程序
#if 0
// ++++++++++++++++++++ execl ++++++++++++++++++++
/*
 * int execl(const char *path, const char *arg, ...);
 		函数描述:加载一个进程,通过路径 + 程序名来加载。
 		函数参数:
 			path:程序路径+程序名字
 			arg:可执行程序的相关参数,使用NULL结尾
 		返回值:
			成功:无返回
			失败:-1
 */
		// 子进程执行指定目录下的自己编写的程序,该程序参数
		printf("this is func +++++++++ execl +++++++++\n");
		execl("/home/robin/a.out", "a.out", NULL);
		// 如果执行成功没有返回值
		perror("execl");
		exit(1);
#endif

#if 0
// ++++++++++++++++++++ execlp ++++++++++++++++++++
/*
 * int execlp(const char *file, const char *arg, ...);
		函数描述:加载一个进程,借助PATH环境变量,该函数需要配合PATH环境变量来使用,
						  当PATH中所有目录搜索后没有参数1则出错返回
		函数参数:可执行程序的相关参数,使用NULL结尾
			file:可执行程序的名字
			arg:可执行程序的相关参数,使用NULL结尾
		返回值:
			成功:无返回
			失败:-1
 */
		printf("this is func +++++++++ execlp +++++++++\n");
		execlp("ls", "ls", "-la", NULL);
		// 如果执行成功没有返回值
		perror("execlp");
		exit(1);
#endif

#if 0
// ++++++++++++++++++++ execvp ++++++++++++++++++++
/*
 * int execvp(const char *file, char *const argv[]);
		用法同 execlp
 */
		printf("this is func +++++++++ execvp +++++++++\n");
		char* arg[] = {"ls", "-a", "-l", NULL};
		execvp("ls", arg);
		// 如果执行成功没有返回值
		perror("execvp");
		exit(1);
#endif

#if 0
// ++++++++++++++++++++ execle ++++++++++++++++++++
/*
 *	 int execle(const char *path, const char *arg, ..., char *const envp[]);
 *	 	从用户指定的目录中搜索指定的命令
 */
		printf("this is func +++++++++ execle +++++++++\n");
		char* env[] = {"/bin", "./", "/home/robin", NULL};	// 自己指定环境变量,对应PATH
		execle("/bin/ls", "ls", "-l", "-a", NULL, env);
		// 如果执行成功没有返回值
		perror("execle");
		exit(1);
#endif

#if 0
// ++++++++++++++++++++ execv ++++++++++++++++++++
/*
 *  int execv(const char *path, char *const argv[]);
 *  	用法同:execl
 */
		printf("this is func +++++++++ execve +++++++++\n");
		char* argv[] = {"a.out", NULL};
		execv("/home/robin/a.out", argv);
		// 如果执行成功没有返回值
		perror("execv");
		exit(1);
#endif

#if 1
// ++++++++++++++++++++ execve ++++++++++++++++++++
/*
 *  int execve(const char *path, char *const argv[], char *const envp[]);
 */
		printf("this is func +++++++++ execve +++++++++\n");
		char* argv[] = {"ps", "a", "u", "x", NULL};
		char* env[] = {"/bin", "/home/", "./", NULL};
		execve("/bin/ps", argv, env);
		// 如果执行成功没有返回值
		perror("execve");
		exit(1);
#endif
	}

	else if(pid > 0)
		printf("parent process +++++++++++++++++++\n");
	printf("*****************************\n");

	return 0;
}
posted @ 2019-04-19 21:13  张飘扬  阅读(902)  评论(0编辑  收藏  举报