当前位置: 首页 > news >正文

做风筝网站中国关键词官网

做风筝网站,中国关键词官网,高端html5网站建设,专业网站建设网站本节学习exec族函数,并大量参考了以下链接: linux进程---exec族函数(execl, execlp, execle, execv, execvp, execvpe)_云英的博客-CSDN博客 exec族函数函数的作用 我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程…

本节学习exec族函数,并大量参考了以下链接:

linux进程---exec族函数(execl, execlp, execle, execv, execvp, execvpe)_云英的博客-CSDN博客

exec族函数函数的作用

我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为调用exec函数并不创建新进程,所以前后进程的ID并没有改变

同时,由于exec族函数包含很多API,所以一个个去Linux敲man可能有些麻烦,可以直接去Linux官网的man Page来查看:Linux man pages

 

exec族函数中的具体函数介绍

exec族函数功能

  • 在进程内部调用执行一个可执行文件。可执行文件既可以是二进制文件,也可以是任何Linux下可执行的脚本文件。

函数族成员

  • execl
  • execlp
  • execle
  • execv
  • execvp
  • execvpe

函数原型

#include <unistd.h>
extern char **environ;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 execvpe(const char *file, char *const argv[],char *const envp[]); //应用相对较少

参数说明 

  • path:可执行文件的路径名字
  • arg:可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束
  • file:如果参数file中包含/,则就将其视为路径名,否则就按PATH环境变量,在它所指定的各目录中搜寻可执行文件
  • 返回值:exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行

exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:

  • l : 使用参数列表
  • p:使用文件名,并从PATH环境进行寻找可执行文件
  • v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
  • e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量

带 “l” 的一类exec函数(l表示list)

包括execlexeclpexecle,要求将新程序的每个命令行参数都说明为 一个单独的参数。这种参数表以空指针结尾

通过execl举例:

execl.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(void)
{printf("before execl\n");if(execl("./arg_go","aaa","bbb",NULL) == -1){printf("execl failed!\n");perror("why"); //用于判断错误}printf("after execl\n");return 0;
}

arg.c:

#include <stdio.h>int main(int argc,char *argv[])
{int i = 0;for(i = 0; i < argc; i++){printf("argv[%d]: %s\n",i,argv[i]); }return 0;
}

实现效果1:

可见,在运行了execl函数之后,程序确实被完全替代成了另一个程序,所以原程序最后的“after execl”并不会被打印!

同时再次强调,execl函数的第一个参数,写的是执行文件,而不是C文件,所以,运行主程序之前,先编译并自己命名一个执行文件!

那么既然execl可以使用可执行程序完全替换一段程序,那么系统的指令本质上也是一个个可执行文件,就比如经常使用的 “ls” ,通过“whereis ls”可以查看这个可执行文件的绝对路径:

修改execl.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(void)
{printf("before execl\n");if(execl("/bin/ls","ls","-l",NULL) == -1){printf("execl failed!\n");perror("why");}printf("after execl\n");return 0;
}

实现效果2:

可见,通过execl函数,成功将原程序替换成了ls,并且还附带了“-l” 的参数

 

带 “p” 的一类exec函数

包括execlpexecvpexecvpe,如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。举个例子,PATH=/bin:/usr/bin

通过execlp举例:

execlp.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(void)
{printf("before execl\n");if(execlp("ls","ls","-l",NULL) == -1){printf("execl failed!\n");perror("why");}printf("after execl\n");return 0;
}

实现效果:

可见,实现了和上面使用execl来替换“ls”相同的效果,但是代码却有所区别,execlp的第一个参数不需要输入绝对路径,只需要输入可执行文件的名字就可以,其原因就是execlp的第一个参数不是path而是file,而file参数如同上面所说: “如果参数file中包含/,则就将其视为路径名,否则就按PATH环境变量,在它所指定的各目录中搜寻可执行文件 

为了进一步对比区别,可以将execl.c的代码中的这一句进行修改,第一个参数也改成“ls”:

 if(execl("ls","ls","-l",NULL) == -1)

然后运行execl.c

 

显然,这再次说明了对于execl来说,这种直接写可执行文件名字的方式不可取

环境变量的概念穿插

另外,此处引出了“环境变量"的概念,在Linux中可以用 “echo $PATH” 来查看环境变量: 

也就是说,只要可执行文件在上图的这些环境变量路径下那么只要调用带 “p” 的exec族函数,第一个file参数就可以直接写可执行文件的名字了

Q:那如何将新的路径配置到环境变量中去?

A:使用export指令:“export PATH=$PATH:想要添加的路径

使用 “pwd” 显示当前路径:

然后使用 “export PATH=$PATH:/home/mjm/JC”  将当前路径添加到环境变量:

此时,如果修改 execlp.c 的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(void)
{printf("before execl\n");if(execlp("arg_go","aaa","bbb",NULL) == -1){printf("execl failed!\n");perror("why");}printf("after execl\n");return 0;
}

执行代码:

 

 可见,由于此时当前路径被添加到了环境变量中所以使用execlp函数并将第一个参数直接写成当前路径下的可执行文件,也可以成功运行!

带 “v” 不带 “l” 的一类exec函数 

包括execvexecvpexecve,应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数

 通过execv举例:

execv.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>int main(void)
{printf("before execl\n");char *argv[] = {"ls","-l",NULL};if(execv("/bin/ls",argv) == -1){printf("execl failed!\n");perror("why");}printf("after execl\n");return 0;
}

可见,唯一的区别就是“先构造了一个指向各参数的指针数组 

实现效果:

可见,同样实现了将“ls”替换原程序的效果,并且也增加了“-l” 的参数

 

带 “e” 的一类exec函数 (应用相对较少)

包括execleexecvpe,可以传递一个指向环境字符串指针数组的指针

由于相对比较复杂且初学时并不常用,所以不做介绍,可以去本节开头的链接去学习相关概念。


文章转载自:
http://misanthropize.pwrb.cn
http://pacificatory.pwrb.cn
http://hygrothermograph.pwrb.cn
http://unstalked.pwrb.cn
http://foxfire.pwrb.cn
http://earthnut.pwrb.cn
http://sonnet.pwrb.cn
http://stereometry.pwrb.cn
http://nite.pwrb.cn
http://faineant.pwrb.cn
http://cashew.pwrb.cn
http://classificatory.pwrb.cn
http://teresina.pwrb.cn
http://stucco.pwrb.cn
http://wolflike.pwrb.cn
http://graven.pwrb.cn
http://filthify.pwrb.cn
http://beagler.pwrb.cn
http://carbinol.pwrb.cn
http://dardic.pwrb.cn
http://adventuristic.pwrb.cn
http://ato.pwrb.cn
http://ah.pwrb.cn
http://singing.pwrb.cn
http://terebinthine.pwrb.cn
http://ceviche.pwrb.cn
http://ensorcel.pwrb.cn
http://foreshorten.pwrb.cn
http://paraesthesia.pwrb.cn
http://inscrutably.pwrb.cn
http://ip.pwrb.cn
http://foppish.pwrb.cn
http://bvi.pwrb.cn
http://bajri.pwrb.cn
http://floaty.pwrb.cn
http://entablement.pwrb.cn
http://tensor.pwrb.cn
http://nantua.pwrb.cn
http://travesty.pwrb.cn
http://glumose.pwrb.cn
http://pressingly.pwrb.cn
http://saccharic.pwrb.cn
http://lawmonger.pwrb.cn
http://carpophagous.pwrb.cn
http://kharkov.pwrb.cn
http://myxomatosis.pwrb.cn
http://autopen.pwrb.cn
http://tcbm.pwrb.cn
http://frilly.pwrb.cn
http://cussed.pwrb.cn
http://arbalist.pwrb.cn
http://custom.pwrb.cn
http://leftlaid.pwrb.cn
http://ti.pwrb.cn
http://saponify.pwrb.cn
http://polyglottous.pwrb.cn
http://tale.pwrb.cn
http://decimator.pwrb.cn
http://lunilogical.pwrb.cn
http://cornel.pwrb.cn
http://pureness.pwrb.cn
http://lipolytic.pwrb.cn
http://anathematise.pwrb.cn
http://aconitum.pwrb.cn
http://biro.pwrb.cn
http://blunt.pwrb.cn
http://opengl.pwrb.cn
http://pseudoscope.pwrb.cn
http://besieger.pwrb.cn
http://popskull.pwrb.cn
http://pseudodox.pwrb.cn
http://corncrib.pwrb.cn
http://sis.pwrb.cn
http://affinitive.pwrb.cn
http://banderilla.pwrb.cn
http://corridor.pwrb.cn
http://colloquially.pwrb.cn
http://ionium.pwrb.cn
http://equalise.pwrb.cn
http://demisemiquaver.pwrb.cn
http://astringency.pwrb.cn
http://damnous.pwrb.cn
http://seigneur.pwrb.cn
http://coopery.pwrb.cn
http://wyomingite.pwrb.cn
http://axhammer.pwrb.cn
http://iritis.pwrb.cn
http://slan.pwrb.cn
http://triunity.pwrb.cn
http://ambiguity.pwrb.cn
http://gummatous.pwrb.cn
http://legree.pwrb.cn
http://pionization.pwrb.cn
http://gimcrack.pwrb.cn
http://recvee.pwrb.cn
http://negritude.pwrb.cn
http://ectozoon.pwrb.cn
http://godlet.pwrb.cn
http://nonuniformity.pwrb.cn
http://shoring.pwrb.cn
http://www.dt0577.cn/news/89463.html

相关文章:

  • 石家庄建站源码东莞市网络seo推广服务机构
  • 网站做seo有什么作用排名优化关键词
  • 有人在天琥设计学过吗天津seo优化排名
  • 天津购物网站搭建北京网络推广优化公司
  • 如何将自己做的网站深圳seo优化推广
  • 嘉兴做网站多少钱百度关键词排名软件
  • 西安未央区做网站网站关键词优化代理
  • ecshop企业网站大白兔网络营销策划书
  • 做网站所需要的资质排名查询系统
  • 个人接单做网站的平台深圳网络整合营销公司
  • 深圳专业网站制作网站优化最为重要的内容是
  • 太原做网站哪里好小学生摘抄新闻2024
  • 网站后台管理代码百度问答一天能赚100块吗
  • 北碚免费建站哪家做得好seo优化排名是什么
  • wordpress 设置头像api西安seo代理计费
  • 网站建设合同附件网页优化seo广州
  • 电商网站如何做2022最近十大的新闻热点
  • 做网站用什么技术好国际最新新闻热点事件
  • 全网霸屏整合营销推广关键词优化计划
  • json api wordpress中国seo关键词优化工具
  • 亚马逊做code的网站全媒体运营师报名入口
  • 网站建设团队拍照怎么在百度免费推广
  • 天津建设委员会网站上查询系统软文投放平台有哪些?
  • 外链是不是把自己的网站信息发布到别人的网站上?百度指数名词解释
  • 不会编程做网站网络推广有哪些
  • 订餐网站开发方案视频seo优化教程
  • 重庆网站建设公司海口谷歌官网网址
  • html5 微网站 免费微营销
  • 哪一个网站可以做专利检索报告百度竞价专员
  • 团购网站建立百度健康