Linux系统【二】exec族函数及应用

文件描述符

文件描述符表是一个指针数组,文件描述符是一个整数。
文件描述符表对应的指针是一个结构体,名字为file_struct,里面保存的是已经打开文件的信息

需要注意的是父子进程之间读时共享,写时复制的原则是针对物理地址而言的,通过程序操控虚拟地址的我们是无法查别到这个问题的,其中的机理由MMU进行控制(详见我的上一篇博客:Linux系统【一】CPU+MMU+fork函数创建进程

测试程序:

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

int main()
{
	int *t = (int*)malloc(sizeof(int));
	*t = 0;
	printf("parent process:&t=%p\n",t);
	pid_t pid;
	pid = fork();
	if(0 == pid)
	{
		printf("child process:&t=%p\n",t);
		(*t)++;
		printf("child process:&t=%p\n",t);
		printf("child process:t=%d\n",*t);
	}
	else
	{
		wait(NULL);
		printf("parent process:&t=%p\n",t);
		printf("parent process:t=%d\n",*t);
	}
	return 0;
}

运行结果:
在这里插入图片描述

exec函数族

fork创建子进程后执行的是和父进程相同的程序(有可能执行不同的代码分支),子进程往往要调用一种exec函数来执行另一个程序,当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程(简单来讲就是main函数,实际上是一个由汇编和C混合编写的程序)。调用exec并不创建新进程,所以调用exec前后进程的id并未改变

命令行执行程序的实质其实就是fork+exec

#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...
                /* (char  *) NULL */);
int execlp(const char *file, const char *arg, ...
//第一个参数为可执行文件的文件名,后面的参数是命令行参数
                /* (char  *) NULL */);
int execle(const char *path, const char *arg, ...
                /*, (char *) NULL, char * const envp[] */);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],
                char *const envp[]);
  • l list 命令行参数列表
  • p PATH 搜索file时使用PATH变量
  • v vector 使用命令行参数数组
  • e environment 使用环境变量数组,不适用进程原有的环境变量,设置新加载程序运行的环境变量
execlp

调用程序的时候会搜索一遍环境变量,如果在环境变量中可以找到程序就会执行,否则按照路径运行程序。

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
	pid_t pid;
	pid = fork();
	if(-1 == pid)
	{
		perror("fork error:");
		exit(1);
	}
	else if(0 == pid)
	{
		execlp("ls","ls","-l","-a",(char *)NULL);
	}
	else
	{
		sleep(1);
		printf("Parent");
	}
	return 0;
}

这里解释一下上述程序:execlp函数第一个参数是调用的程序,从第二个参数开始是命令行参数,最后一定要以(char*)NULL结尾才能正常运行。其中比较特殊的是命令行参数的第一个参数是什么不重要,我上面虽然写的是ls,但其实随便写什么都可以。

为什么会有这种写什么都可以的情况出现呢?实际原因是:我们在执行程序的时候系统会默认带有一个参数(虽然这个参数大多数情况下都与程序名相同,但是某些shell会将此参数设置为完全的路径名),所以我们第一个参数是不会有作用的,真正有作用的参数是从参数数组中第一个元素开始的。

例如:

//test.c
#include<stdio.h>

int main(int argc,char* args[])
{
	for(int i=0;i<=argc;++i)
	{
		printf("%d=[%s]\n",i,args[i]);
	}
	return 0;
}

在这里插入图片描述

将上面两个程序组合:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
	pid_t pid;
	pid = fork();
	if(-1 == pid)
	{
		perror("fork error:");
		exit(1);
	}
	else if(0 == pid)
	{
		execlp("./test","first","second","third",(char *)NULL);
		printf("Child\n");
	}
	else
	{
		sleep(1);
		printf("Parent\n");
	}
	return 0;
}

在这里插入图片描述
我们可以发现一旦运行execlp将不会再运行原来进程后面的代码,原本进程的代码块完全被新的文件代替

execl

不会查找环境变量,直接按照路径运行程序

execle

需要借助环境变量表char ** environ

execv

传入的需要是一个字符串数组,而不能直接传命令行参数

例题:将当前系统中的进程信息打印到文件中

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main(int argc,char *argv[])
{
	if(argc != 2)
	{
		printf("you should input filename\n");
		exit(1);
	}
	freopen(argv[1],"w",stdout);
	execlp("ps","ps","aux",(char*)NULL);
	return 0;
}

更好的做法是使用dup2函数(其中的2的意思是to,如果后面是4的话意思常常是for),并将新进程放在子进程中

dep2(old fd,new fd); // 将旧文件描述符中的内容拷贝到新文件描述符中
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(int argc,char *argv[])
{
	if(argc != 2)
	{
		printf("you should input filename\n");
		exit(1);
	}
//	freopen(argv[1],"w",stdout);
	int fd = open(argv[1],O_CREAT | O_RDWR,06444);
	if(-1 == fd)
	{
		perror("openfile error:");
		exit(1);
	}
	dup2(fd,STDOUT_FILENO);
	pid_t pid=fork();
	if(-1 == pid)
	{
		perror("fork error:");
		exit(1);
	}
	else if(0 == pid)
	{
		execlp("ps","ps","aux",(char*)NULL);
		perror("exec error:");	//不需要判断返回值,如果成功不会运行下面的语句
		exit(1);
	}
	else
	{
		close(fd);
	}
	return 0;
}

需要注意的是使用dup2函数以后原本标准输出文件指针丢失了,如果还要回到标准输出文件,应该之前用dup保存一份,使用之后再dup2回去就可以了。

exec函数族没有成功返回值,只有失败返回值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值