linux c-exec系列函数学习

函数原型

cpp 复制代码
  #include <unistd.h>    

   int execl(const char *pathname, const char *arg, ...
            /* (char  *) NULL */);
   int execlp(const char *file, const char *arg, ...
            /* (char  *) NULL */);
   int execle(const char *pathname, const char *arg, ...
            /*, (char *) NULL, char *const envp[] */);
   int execv(const char *pathname, char *const argv[]);
   int execvp(const char *file, char *const argv[]);
   int execvpe(const char *file, char *const argv[],
            char *const envp[]);

函数说明

这些函数都是以替换的方式,创建一个新的进程替代当前的进程。新的进程与原来的进程号相同,所有的用户空间(代码段、数据段)及部分的内核空间都可以视为是全新的。要注意与fork函数的区别。

  1. -l 可理解为list,函数参数类似列出的方式,但最后一个参数必须以NULL结尾
  2. -p
    1. 带p的等效为shell中直接使用指令,会从PATH的路径查找相关的可执行文件;
    2. 不带p的函数,则必须使用相对路径或者绝对路径
  3. -e
    1. 带e的函数,新进程的环境变量会被清空,来源是原进程传递过来的;
    2. 不带e的函数,则使用原进程的环境变量(理解:进程替代这一代概念,但又要注意用户空间是全新的)

可变参数 :可以参考va_start系列的(宏)函数,因此,无论是...,还是vector,还是环境变量的数组,最后一个元素必须是NULL!

返回值

The exec() functions return only if an error has occurred. The return value is -1, and errno is set to indicate the error.

exec系列函数只有 在执行错误的有返回值,且返回值为**-1**,相关的错误代码会被设置到errno中

相关推荐
2301_7807896621 小时前
零信任架构中,身份感知防火墙(IAFW)的部署要点与最佳实践
linux·运维·服务器·人工智能·tcp/ip·架构
小狮子&1 天前
ubuntu2604无法共享文件夹问题解决
linux·运维·服务器
biter down1 天前
3:VMware Workstation 安装 Ubuntu 22.04 超详细教程
linux·运维·ubuntu
H__Rick1 天前
C51单片机学习-DAY3
单片机·学习·mongodb
曾阿伦1 天前
netcat / ncat / socat 用法详解与示例
linux·http·信息与通信
Benszen1 天前
Secret详解
linux·运维·服务器
Dlrb12111 天前
Linux网络编程-网络基础概念(IP, UDP协议)
linux·服务器·网络·网络基础·端口号·ip协议·udp协议
yoothey1 天前
异常学习笔记:为什么自定义异常后还要 throw?
笔记·学习
noipp1 天前
【无标题】
c语言·数据结构·c++·算法
超皮小龙猫1 天前
c语言-1
c语言·开发语言