exec函数族介绍

exec函数族

exec函数族的主要作用是让一个进程去执行一个指定的程序文件。
或者这么理解:exec函数族的作用就是让一个指定的程序文件中的数据和指令替换到调用进程的数据和指令。

#include<stdio.h>

int main(int argc,char *argv[])
{
	int a,b;
	int sum;
	a = atoi(argv[1]);
	b = atoi(argv[2]);
	
	sum = a + b;
	printf("%d + %d = %d",a,b,sum);
	
	return 0;
}

atoi()函数在<stdlib.h>库文件中,是把字符串转换成整形数的一个函数

exec函数族是让一个进程去执行另外一个程序文件。就是让另外一个程序文件的数据和指令,覆盖掉当前进程的数据和指令。

exec让一个进程去执行另外一个程序,那么:

你是不是要指定这个程序文件的名字?

一个文件系统中的程序文件的名字(带路径)

你可能还得指定程序运行的参数,都是字符串。

指定程序的参数有两种方式:

l:list

把程序运行的参数,一个一个地列举出来。
程序运行的第一个参数,是程序的名字(不带路径)
“sum_ab”,“3”,“4”,NULL

v:vector(向量数组)

把程序运行的参数,弄成一个char *的数组
char *argv[ ] = {" sum_ab ", " 3 ", " 4 ", NULL};


#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[]);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值