exec族函数、system函数和popen函数应用

exec族函数:

首先 ,先man exec 查看下exec函数原型

   #include <unistd.h>

       extern char **environ;

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

我们可以看出来,前面都是exec开头,我们来分别说一下后面几个数字:
l代表的是list:命令行参数列表
p代表的是path:搜索file时的使用的path变量
v代表的是vector:使用命令行参数数组

接下来说一个后面的参数:
path:可执行文件的路径名
arg:可执行程序所带参数,第一个参数为可执行程序名,而结尾必须以NULL结尾
file:如果file中没有包含./ 也就是说没有在当前目录,就配置PATH变量。

结合来说:
int execl(const char *path, const char *arg, …):
函数作用就是在当前目录下执行path程序的arg操作
path就是在当前目录下想要执行的命令,而arg就是命令行参数列表 ,第一个参数是命令行名字,结尾必须为NULL.

int execlp(const char *file, const char *arg, …):
函数作用就是在任何目录下执行file程序的arg操作

int execvp(const char *file, char *const argv[]):
函数作用就是在任何目录下完成file程序argv[]操作

附录: 如果执行了exec族函数 ,那么就会进入到新进程里面 ,不会继续运行之前的进程。

system函数

//函数原型
 int system(const char *command);

比起exec族函数 ,更简单粗暴。 直接把命令行传参进去给system就可以了。
command就是命令行。

system("ls -l";

附录:system函数执行完还会继续运行原程序

popen函数

//函数原型
 FILE *popen(const char *command, const char *type);

比起system函数,popen函数能够保留运行结果。
command就是命令行 ,而type只有 “r”,“w” 读写俩种。
popen会把运行结果存入管道中 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
fork-popen和system都是用来创建进程的函数。 fork函数是用于创建一个新的进程,新进程完全复制了父进程的资源。子进程可以通过返回值来区分自己是子进程还是父进程。fork函数相当于同时调用了fork、exec和waitpid这三个系统调用,会等待子进程执行完成后再继续执行。而popen函数则是通过管道来启动一个进程,并返回一个文件指针,可以用来读取或写入子进程的输入输出。popen函数不需要等待子进程执行完成就可以返回。 系统提供的system函数是用于执行shell命令的。它的原型是`int system(const char* command)`,可以通过传入shell命令作为参数来执行命令。system函数会一直等待shell命令执行完成(waitpid),然后返回。而popen函数则可以并行执行shell命令,不需要等待命令执行完成。 在使用popen函数后,需要调用pclose来对所创建的子进程进行回收,否则可能会导致僵尸进程的情况。 总结起来,fork-popen和system都是用来创建进程的函数,但是fork-popen是并行执行的,而system是串行执行的。同时,在使用popen函数后,需要调用pclose进行子进程回收。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Linux---popen、system函数](https://blog.csdn.net/y6_xiamo/article/details/80156598)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [重新实现popen和system函数](https://download.csdn.net/download/u013105439/10441160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值