目录

  • exec()函数族-基础概念
  • exec()函数族参数分析—很重要
  • 关于fork()的第二种常见用法
  • 案例,在fork子进程调用exec函数族
exec()函数族-基础概念----man 3 exec

作用:execute a file,执行指定路径下的可执行文件
成员:execl(); execlp(); execle(); execv(); execvp(); execvpe()–不用了;
经常使用的是execve():
注:只有execve是真正意义上的系统调用,其它都是在此基础上经过包装的库函数

//参数是列表
execl(): arg0, arg1, ..., argn.int execl(const char *path, const char *arg, ...);
execlp():int execlp(const char *file, const char *arg, ...);
execle():int execle(const char *path, const char *arg,..., char * const envp[]);
//参数是数组
execv():int execv(const char *path, char *const argv[]);
execvp():int execvp(const char *file, char *const argv[]);
execvpe():好像弃用了,资料少。。int execvpe(const char *path, char *const argv[], char * const envp[]);
execve():int execve(const char *filename, char *const argv[],char *const envp[]);

举个例子:

exec()函数族参数分析—很重要

(1):“l”和“v”表示参数是以列表还是以数组的方式提供,但是都要以NULL结尾,arg[0]:需要执行二进制程序的名字
(2):“p”表示这个函数的第一个参数是*path,就是以绝对路径来提供程序的
路径,也可以以当前目录作为目标
(3):“e”表示为程序提供新的环境变量,不需要给NULL
(4):所有参数要求全部来自man文档,请仔细阅读,如果出现错误或字符参数警告,你可以再仔细分析下这6个函数。
给一张简单直观图:

小案例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>/****************************************************************************************
**                                  exec函数族
//参数是列表
** execl(): arg0, arg1, ..., argn.
**   int execl(const char *path, const char *arg, ...);
** execlp():
**  int execlp(const char *file, const char *arg, ...);
** execle():
**  int execle(const char *path, const char *arg,..., char * const envp[]);
//参数是数组
** execv():
**   int execv(const char *path, char *const argv[]);
** execvp():
**  int execvp(const char *file, char *const argv[]);
** execvpe():
**  int execvpe(const char *path, char *const argv[], char * const envp[]);
**注意:execvpe要#ifdef __USE_GNU支持execl,execlp,execle:The first argument, by convention, should point to the filename associated with the file being executed,即arg0是可执行程序名字null-terminated strings(空终止字符串):The list of arguments must be terminated by a NULL pointer(结尾一定要是NULL)execv,execvp,execvpe:也必须满足上述两个要求,且以数组传入p:绝对路径来提供程序的路径    e:表示为程序提供新的环境变量       extern char **environ;
** RETURN VALUE
**  The exec() functions only return if an error has have occurred.  The return value is -1
****************************************************************************************/ int main()
{if((execl("/linuxsystemcode/systemcode/pid/helloexecl","helloexecl","execl")) == -1)   //存在就调用helloexecl应用程序,不会再返回来了{perror("execl");exit(1);}printf("execl error!!!\n");                       //正常情况下,是不会执行的return 0;
}

helloexecl.c

//helloexecl.c
#include <stdio.h>int main(int argc, char *argv[])
{int i = 10;while(i--){printf("%s %d.\n",argv[1], i);}
}

输出:

关于fork()的第二种常见用法

下面是程序框架:并发执行,谁先谁后执行完,并不清楚。父进程,子进程结束时间先后,会使输出不可控,你要在代码里更正

案例,在fork子进程调用exec函数族

下面的代码结合fork和6个exec族内函数的例子

#include <stdio.h>
#include <unistd.h>//#define  _GNU_SOURCE                                         //execvpe才需要
//extern char **environ;                                            //新环境变量,下面全给NULL/****************************************************************************************
**                                  exec函数族与fork
//参数是列表
** execl(): arg0, arg1, ..., argn.
**   int execl(const char *path, const char *arg, ...);
** execlp():
**  int execlp(const char *file, const char *arg, ...);
** execle():
**  int execle(const char *path, const char *arg,..., char * const envp[]);
//参数是数组
** execv():
**   int execv(const char *path, char *const argv[]);
** execvp():
**  int execvp(const char *file, char *const argv[]);
** execvpe():好像弃用了,资料少。。
**  int execvpe(const char *path, char *const argv[], char * const envp[]);
** execve():
** int execve(const char *filename, char *const argv[],char *const envp[]);
**注意:execvpe要#ifdef __USE_GNU支持execl,execlp,execle:The first argument, by convention, should point to the filename associated with the file being executed,即arg0是可执行程序名字null-terminated strings(空终止字符串):The list of arguments must be terminated by a NULL pointer(结尾一定要是NULL)execv,execvp,execvpe:也必须满足上述两个要求,且以数组传入p:绝对路径来提供程序的路径    e:表示为程序提供新的环境变量       extern char **environ;
** RETURN VALUE
**  The exec() functions only return if an error has have occurred.  The return value is -1
****************************************************************************************/ int main(void)
{char *arg[]={"ls","-i",NULL};                             //参数数组//子进程1--int execl(const char *path, const char *arg, ...);if(fork() == 0)                                               //子进程调用exe函数族{printf("Forks 1 is Ok!!! : execl\n");if(execl("/bin/ls","ls","-i",NULL) == -1){perror("execl error!!!\n");return 1;}}//子进程2--int execlp(const char *file, const char *arg, ...);  usleep(50000);if(fork() == 0){printf("Forks 2 is Ok!!! : execlp\n");if(execlp("ls","ls","-i",NULL) == -1){perror("execlp error!!!\n");return 1;}}//子进程3--int execle(const char *path, const char *arg,..., char * const envp[]);  usleep(50000);if(fork() == 0){printf("Forks 3 is Ok!!! : execle\n");if(execle("/bin/ls","ls","-i",NULL, NULL) == -1){perror("execle error!!!\n");return 1;}}//子进程4--int execv(const char *path, char *const argv[]);usleep(50000);if(fork() == 0){printf("Forks 4 is Ok!!! : execv\n");if(execv("/bin/ls",arg) == -1){perror("execv error!!!\n");return 1;}}//子进程5--int execvp(const char *file, char *const argv[]);usleep(50000);if(fork() == 0){printf("Forks 5 is Ok!!! : execvp\n");if(execvp("ls",arg) == -1){perror("execvp error!!!\n");return 1;}}//子进程6--int execve(const char *filename, char *const argv[],char *const envp[]);   usleep(50000);if(fork() == 0){printf("Forks 6 is Ok!!! : execve\n");if(execve("/bin/ls",arg,NULL) == -1){perror("execve error!!!\n");return 1;}}//子进程7--int execvpe(const char *path, char *const argv[], char * const envp[]);   // usleep(50000);// if(fork() == 0)// {// printf("Forks 7 is Ok!!! : execvpe\n");// if(execvpe("/bin/ls",arg,NULL) == -1)// {// perror("execvpe error!!!\n");// return 1;// }// }usleep(50000);                           //不添加usleep(50000),终端会出现输出混乱return 0;
}

输出:

举个例子:注释掉最后的延时,输出
当然:修改延时也会有不同结果,还与代码量有关

exec函数族的基本用法相关推荐

  1. exec函数族用法总结

    1.exec函数说明 fork()函数通过系统调用创建一个与原来进程(父进程)几乎完全相同的进程(子进程是父进程的副本,它将获得父进程数据空间.堆.栈等资源的副本.注意,子进程持有的是上述存储空间的& ...

  2. UNIX再学习 -- exec 函数族

    我们在讲,文件I/O的时候,简单提到过 exec 函数,讲到 vfork 的时候,也有用到.下面我们来详细介绍下它. 参看:UNIX再学习 -- 文件I/O  参看:UNIX再学习 -- 函数 for ...

  3. 【Linux系统编程】进程替换:exec 函数族

    00. 目录 文章目录 00. 目录 01. exec函数族 02. 参考示例 2.1 execl函数示例 2.2 execv函数示例 2.3 execlp() 或 execvp()函数示例 2.4 ...

  4. execve系统调用_Linux系统编程——进程替换:exec 函数族

    在 Windows 平台下,我们可以通过双击运行可执行程序,让这个可执行程序成为一个进程:而在 Linux 平台,我们可以通过 ./ 运行,让一个可执行程序成为一个进程. 但是,如果我们本来就运行着一 ...

  5. exec函数族的作用与讲解

    apue看到第八章,对exec函数族的理解一直都很混乱,总觉得不对劲儿,其实不能理解的先暂时跳过,看到后面,再结合实例也就慢慢的理解了. 以下内容转自:http://www.cppblog.com/p ...

  6. 【系统编程】进程--exec函数族(execl、execlp、execle、execv、execvp、execve)

    1.exec族函数说明 fork函数是用于创建一个子进程,该子进程几乎是父进程的副本,而有时我的程序们希望子进程去执行另外,exec函数族就提供了一个在进程中启动另一个程序执行的方法.它可以根据指定的 ...

  7. exec函数族的使用

    调用shell脚本命令:execlp("sh","sh","filename",(char*)0); exec用被执行的程序完全替换调用它的 ...

  8. linux-进程的替换exec函数族

    文章目录 一.进程的替换 二.exec函数族 execl函数 execlp函数 execvp函数 一.进程的替换 exec族函数函数的作用: 我们用fork函数创建新进程后,经常会在新进程中调用exe ...

  9. Linux进程控制——exec函数族

    1.简介 在Linux中,并不存在exec()函数,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **environ; in ...

最新文章

  1. 欧盟「人脑计划」​最新进展:新算法模拟生物进化,为大脑如何工作提供新见解...
  2. openface 和openpose(pytorch)剪枝之路
  3. Rancher前奏--配置Nexus
  4. api权限管理系统与前后端分离实践
  5. 【BZOJ5469】[FJOI2018]领导集团问题(动态规划,线段树合并)
  6. pgsql自动安装shell脚本整理
  7. 项目是采用目前比较流行的 SpringBoot/SpringCloudAlibaba构建新零售微服务电商项目
  8. 品牌不可不知的YouTube的影片行销策略
  9. SpringBoot的Bean之基本定义与使用
  10. java 包装类缺点_Java 自动拆箱和自动装箱学习笔记
  11. 【收藏】一千行 MySQL 学习笔记
  12. SQL Server 2008 R2每天自动备份数据库
  13. 互联网平台掘金三四五线城市,你需要知道的9.9个真相
  14. 宁波大学2014年数学分析考研试题
  15. [CSP-S模拟测试]:跳房子(模拟)
  16. 涉案资金超10亿,又一洗钱团伙被端,“二清”警钟不能忘
  17. Android仿微信朋友圈图片查看器
  18. 二维码支付的优势与劣势
  19. simulink 脱离 matlab,Simulink软件不能脱离MATLAB环境而运行
  20. 第四篇:coalesce 和repartition 在shuffle 和并行度之间的选择(spark2.3持续更新中...........)

热门文章

  1. 想要搭建个人博客?我调研了 100 来个 Java 开源博客系统,发现这 5 个最好用!......
  2. 【Python项目】Flask + MySQL 实现用户注册,登录、注销
  3. 如何通过纯javascript实现表单提交
  4. android软件版本升级时,安装新版本后,没有出现安装成功界面或直接回到桌面
  5. IDEA打开多个项目
  6. 构建信用卡反欺诈预测模型——机器学习
  7. 原来Python自带了数据库,用起来真方便
  8. python读取.txt、.dat等文件,将其中特定内容存到其他文件
  9. 漂亮学姐计算机软考【认证】通过了的学习方法和小技巧
  10. Qt QComboBox下拉菜单背景透明