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中

相关推荐
orion5720 小时前
Missing Semester Class1:course overview and introduction of shell
linux
apocelipes1 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia3 天前
linux curl命令详解_curl详解
linux
扛枪的书生3 天前
Linux 网络管理器用法速查
linux
顺风尿一寸3 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode3 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行