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中

相关推荐
aaa最北边3 分钟前
Linux 上的交叉编译
linux·运维·服务器
小张成长计划..35 分钟前
【Linux】27:日志
linux·运维·服务器
本地化文档1 小时前
llvm-libc-docs-l10n
c语言·github·gitcode·sphinx·llvm·crowdin
别动我齐刘海1 小时前
机器人 C++ / 系统联调补强2——异常处理、Protobuf
linux·c++·人工智能·学习·算法·机器学习·机器人
Lsetea1 小时前
curl -I检查HTTPS返回405:HEAD、GET与证书验收分层排查
linux·http·https·ssl证书·curl
一直C1 小时前
嵌入式 Linux 驱动开发学习笔记:内核定时器、platform 总线与 I²C 驱动
linux·驱动开发·嵌入式硬件·学习·imx6ull
承渊政道2 小时前
Linux网络学习【传输层协议UDP】
linux·网络·网络协议·学习·ubuntu·udp
andrsted2 小时前
练题簿免费刷题小程序:和学习搭子共用一份题库,一起刷题复习
学习·面试·微信小程序·小程序·学习方法·刷题
雯宝2 小时前
usb_serial_init/tty_driver/alloc_tty_driver/tty_register_driver/usb_register
linux
Felven2 小时前
B. Collecting Game
c语言