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中

相关推荐
Sokach10151 天前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux
AlfredZhao2 天前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3103 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode3 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
A小辣椒5 天前
TShark:Wireshark CLI 功能
linux
A小辣椒5 天前
TShark:基础知识
linux
AlfredZhao5 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao6 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334666 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪6 天前
linux 拷贝文件或目录到指定的位置
linux