exec族函数函数的作用:
        exec函数族的作用是根据指定的文件名找到可执行文件,并用它来取代调用进程的内容,换句话说,就是在调用进程内部执行一个可执行文件。这里的可执行文件既可以是二进制文件,也可以是任何Linux下可执行的脚本文件。
        与一般情况不同,exec函数族的函数执行成功后不会返回,因为调用进程的实体,包括代码段,数据段和堆栈等都已经被新的内容取代,只留下进程ID等一些表面上的信息仍保持原样,颇有些神似"三十六计"中的"金蝉脱壳"。看上去还是旧的躯壳,却已经注入了新的灵魂。只有调用失败了,它们才会返回一个-1,从原程序的调用点接着往下执行。
        我们应该明白了,Linux下是如何执行新程序的,每当有进程认为自己不能为系统和用户做出任何贡献了,他就可以发挥最后一点余热,调用任何一个exec,让自己以新的面貌重生;或者,更普遍的情况是,如果一个进程想执行另一个程序,它就可以fork出一个新进程,然后调用任何一个exec,这样看起来就好像通过执行应用程序而产生了一个新进程一样。
函数族
        exec函数族分别是:execl, execlp, execle, execv, execvp, execvpe 但是对于初学者来说先学习一下四个exec族函数。
返回值
        如果执行成功则函数不会返回,执行失败则直接返回-1,原程序接着从调用处执行,失败原因存于errno 中。
函数原型

    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族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:
l : 使用参数列表
p:使用文件名,并从PATH环境进行寻找可执行文件
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量

execl函数

int execl(const char *path, const char *arg, ...);
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int execl(const char*path,const char *arg,..)
int main()
{printf("before execl \n");if(execl("./exec","echoarg","abc",NULL)==-1)//要求exec结尾必须是NULL//echoarg是argv[0],abc是argv[1] // ./exec是可执行程序的路径名{printf("execl failed \n");perror("why");//用来打印错误}printf("after execl\n");return 0;
}

execl实现ls指令

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

execl实现 ls -l 指令

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int execl(const char*path,const char *arg,..)
int main()
{printf("before execl \n");if(execl("/bin/ls","ls","-l",NULL)==-1)//这要在ls后给ls传参-l即可{printf("execl failed \n");perror("why");}printf("after execl\n");return 0;
}

execl 获取系统时间

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int execl(const char*path,const char *arg,..)
int main()
{printf("this pro get system date: \n");if(execl("/bin/date","date",NULL,NULL)==-1){printf("execl failed \n");perror("why");}printf("after execl\n");return 0;
}

execlp函数

int execlp(const char *file, const char *arg, ...);

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

获取系统时间代码

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int execl(const char*path,const char *arg,..)
int main()
{printf("this pro get system date: \n");if(execlp("date","date",NULL,NULL)==-1)//第一个参数不含/所以按 PATH环境变量,在它所指定的各目录中搜寻可执行文件{printf("execl failed \n");perror("why");}printf("after execl\n");return 0;
}

execvp函数

int execvp(const char *file, char *const
argv[]);
//其实就是将execlp函数的后三个参数
//写入创建已好的数组指针

代码演示

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int execl(const char*path,const char *arg,..)
int main()
{printf("this pro get system date: \n");char*argv[]={"data",NULL,NULL};if(execvp("date",argv)==-1)printf("execl failed \n");perror("why");}printf("after execl\n");return 0;
}

execv函数

int execv(const char *path, char *const argv[]);
//其实就是将execl函数的后三个参数
//写入创建已好的数组指针

代码演示

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//int execl(const char*path,const char *arg,..)
int main()
{printf("this pro get system date: \n");char*argv[]={"data",NULL,NULL};if(execv("/bin/date",argv)==-1){printf("execl failed \n");perror("why");}printf("after execl\n");return 0;
}

exec配合fork使用代码示例

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main()
{pid_t fpid;int data;while(1){printf("please input a data\n");scanf("%d",&data);if(data==1){fpid=fork();if(fpid>0){wait(NULL);}if(fpid==0){execl("./changedata","changedata","config.txt",NULL);}}else{printf("do nothing\n");}}return 0;
}
//调用在子进程中调用exec去执行修改文件中的数值

以下代码和上面的代码效果相同

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main()
{pid_t fpid;int data;while(1){printf("please input a data\n");scanf("%d",&data);if(data==1){fpid=fork();if(fpid>0){wait(NULL);}if(fpid==0){int fdSrc;char* readbuf=NULL;fdSrc=open("config.txt",O_RDWR);int size=lseek(fdSrc,0,SEEK_END);readbuf=(char *)malloc(sizeof(char)*size+8);lseek(fdSrc,0,SEEK_SET);int n_read=read(fdSrc,readbuf,size);char *p=strstr(readbuf,"LENG=");if(p==NULL){printf("find  fail\n");exit(-1);}p=p+strlen("LENG=");*p='5';lseek(fdSrc,0,SEEK_SET);int n_write=write(fdSrc,readbuf,strlen(readbuf));close(fdSrc);}}else{printf("do nothing\n");}}return 0;
}

linux进程---exec族函数(execl, execlp, execv, execvp, )解释和配合fork的使用相关推荐

  1. Linux进程—exec族函数 execl, execlp, execle, execv, execvp

    exec族函数的作用: 我们用fork函数创建新进程后,经常会在新进程中调用exec族函数去执行新的程序:当该进程调用exec族函数时,该进程被替代为新程序,因为exec族函数并不创建新进程,所以前后 ...

  2. linux进程---exec族函数(execl, execlp, execle, execv, execvp, execvpe)

    原文链接:https://blog.csdn.net/u014530704/article/details/73848573 exec族函数函数的作用: 我们用fork函数创建新进程后,经常会在新进程 ...

  3. 10.linux进程---exec族函数(execl, execlp, execle, execv, execvp, execvpe)

    1.exec族函数函数的作用: 1.exec族函数的作用: 我们用fork函数创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序.当进程调用exec函数时,该进程被完全替换为新程序.因为 ...

  4. 10--linux进程---exec族函数(execl, execlp, execle, execv, ex ecvp, execvpe)

    原文链接: https://blog.csdn.net/u014530704/article/details/73848573?ops_request_misc=%257B%2522request%2 ...

  5. Linux 进程11【exec族函数(execl, execlp, execle, execv, execvp, execvpe)】

    linux进程-exec族函数(execl, execlp, execle, execv, execvp, execvpe) 原文链接:https://blog.csdn.net/u014530704 ...

  6. Linux进程5:exec族函数(execl, execlp, execle, execv, execvp, execvpe)总结及exec配合fork使用

    exec族函数(execl, execlp, execle, execv, execvp, execvpe)及exec配合fork使用 exec族函数函数的作用: 我们用fork函数创建新进程后,经常 ...

  7. Linux进程 exec族函数(execl,execlp,execle,execv,execvp,execcvpe)

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

  8. exec族函数————execl, execlp, execle, execv, execvp, execvpe

    exec族函数 exec族函数的作用[^1] exec族函数的定义 函数原型: exac函数归为带l.带p.带v.带e 四类来说明参数特点 一.带l的一类exac函数(l表示list),包括execl ...

  9. exec族函数(execl, execlp, execle, execv, execvp, execvpe)

    1.exec族函数的作用:我们用fork函数创建新进程后,经常会在新进程调用exec函数去执行另外一个程序. 当进程调用exec函数时,该进程被完全替换为新程序,因为调用exec函数并不创建新进程,所 ...

最新文章

  1. DelphiXE7中创建WebService(服务端+客户端)
  2. SAP中和计量单位有关的表
  3. 备受诟病的导购,不过是在替屈臣氏挡子弹
  4. codeforces 711B - Chris and Magic Square(矩阵0位置填数)
  5. SAP云平台CloudFoundry环境下的环境变量使用
  6. 女朋友掉水里,各类程序猿怎么救?
  7. 你为什么人到中年还是个普通员工?
  8. 调用网易云Api接口实现移动Web网易云部分功能(搜索+列表+播放)
  9. R语言从github安装recharts包
  10. 一个柠檬轻松制作环保时钟
  11. AlphaZero如何学习国际象棋的?
  12. 十二星座匹配对象_来看十二星座最配与最不配的对象是谁
  13. Socket编程面试问题
  14. Android adb常用命令 app流量消耗获取
  15. 从逻辑功能上计算机网络可以分为两部分,从逻辑功能上看计算机网络分为( )几部分...
  16. 无限速驱动管理工具Driver Genius
  17. 【ACWing】1126. 最小花费
  18. Unity 音乐播放全局类
  19. 查看电脑电池使用报告
  20. FCPX视频特效插件Boris Continuum Complete 2019

热门文章

  1. 【数据库学习笔记】——创建数据库连接对象connection
  2. 【tenserflow】——数据类型以及常用属性
  3. cap理论具体含义_分布式系统:CAP 理论的前世今生
  4. Java注解的基本概念和原理及其简单实用
  5. Android OOM的解决方式
  6. TCP 之 RST 原因分析
  7. 测试对bug如何分析和定位
  8. 《大话设计模式》--模板模式
  9. 博文视点 OpenParty第11期:世界黑客大会那些事
  10. Microsoft SQL Server 2005 Service Pack 2 已经可以下载