Linux——进程(4)——exec族函数 system函数 popen函数


一、exec族函数的作用

我们用frok函数所创建新进程后,经常会在新进程中调用exec函数去执行另外一个程序。当进程调用exec函数时,该进程被完全替换为新程序。因为exec函数并不创建新进程,所以前后进程的ID并没有改变。

函数族

exec函数族分别是:execl,execlp,execle,execv,execvp,execvpe

函数原型:

#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[]);

————————————————
版权声明:本文为CSDN博主「云英」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014530704/article/details/73848573

返回值:

exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。

参数说明:

path:可执行文件的路径名字
arg:可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束
file:如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。

exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:
l : 使用参数列表
p:使用文件名,并从PATH环境进行寻找可执行文件
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量
————————————————
版权声明:本文为CSDN博主「云英」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014530704/article/details/73848573

execl示例

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
        printf("before execl\n");
        if(execl("./echoarg","echoarg","ls","haha",NULL) == -1){
                printf("execl failed!\n");
                perror("why");//perror函数可以解析 execl函数失败的返回值  并将错误原因打印
        }
        printf("after execl\n");
        return 0;
}

在这里插入图片描述
调用date函数显示系统时间

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
        printf("before execl\n");
        printf("system time is:\n");
        if(execl("/bin/date","date",NULL) == -1){
                printf("execl failed!\n");
                perror("why");
        }
        printf("after execl\n");
        return 0;
}
~

在这里插入图片描述

execlp

execlp函数带p,所以能通过环境变量PATH查找到可执行文件ps

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);

int main(void)
{
        printf("before execl\n");
        printf("system time is:\n");
        if(execlp("ps","ps",NULL) == -1){ //execlp函数会自己在环境变量里查找可执行文件
                printf("execl failed!\n");
                perror("why");
        }
        printf("after execl\n");
        return 0;
}

在这里插入图片描述

execvp函数示例

先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execvp(const char *file, char *const argv[]);
int main(void)
{
        printf("before execl\n");
        char *argv[]={"ps",NULL};
        if(execvp("ps",argv) == -1){
                printf("execl failed!\n");
                perror("why");
        }
        printf("after execl\n");
        return 0;
}

execv函数示例

和execvp相比 第一个参数需要改为绝对路径

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execvp(const char *file, char *const argv[]);
int main(void)
{
        printf("before execl\n");
        char *argv[]={"ps",NULL};
        if(execv("/bin/ps",argv) == -1){
                printf("execl failed!\n");
                perror("why");
        }
        printf("after execl\n");
        return 0;
}

二、exec配合fork使用

实现功能,当父进程检测到输入为1的时候,创建子进程把配置文件的字段值修改掉。

#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

int main()
{
        pid_t pid;
        int data;

        while(1){
                printf("please enter a data:\n");
                scanf("%d",&data);
                if(data==1){
                        pid=fork();
                        if(pid>0){
                                wait(NULL);//收集子进程状态防止僵尸进程
                        }

                        else if(pid==0){
                                execl("./wenjian/changeData","changeData","./wenjian/TEST.config",NULL); //使用execl函数调用另一程序修改config文件内容
                                exit(0);
                        }
                }
                else{
                        printf("wait,do nothing\n");
                }
        }
        return 0;
}

在这里插入图片描述

三、 system函数

和execl 功能差不多 ,更加简单直接
但是system执行完后还会返回原程序 execl则不会

#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

int main()
{
        pid_t pid;
        int data;

        while(1){
                printf("please enter a data:\n");
                scanf("%d",&data);
                if(data==1){
                        pid=fork();
                        if(pid>0){
                                wait(NULL);
                        }

                        else if(pid==0){
                                system("./changeData TEST.config");
                                exit(0);
                        }
                }
                else{
                        printf("wait,do nothing\n");
                }
        }
        return 0;
}

四、popen函数

FILE *popen(const char *command, const char *type);

参数说明:
command: 是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用 -c 标志,shell 将执行这个命令。

mode: 只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 “r” 则文件指针连接到 command 的标准输出;如果 type 是 “w” 则文件指针连接到 command 的标准输入。

作用:
popen() 函数用于创建一个管道:其内部实现为调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程这个进程必须由 pclose() 函数关闭。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
        char ret[1024]={0};
        FILE *fp;

        fp=popen("ps","r");
        int nread=fread(ret,1,1024,fp);
        printf("read %d byte,the ret is:%s\n",nread,ret);
        return 0;
}

在这里插入图片描述
popen的好处在于可以将指令的结果存放到字符数组或者文件当中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值