Linux进程的基础(下)

 exec族函数

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

函数原型:(execle和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[]);

返回值:
  exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。
参数说明:
path:可执行文件的路径名字
arg:可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束
file:如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。

exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:
l : 使用参数列表
p:使用文件名,并从PATH环境进行寻找可执行文件
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量

#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","abc",NULL) == -1){
                printf("ececl failed!\n");
                perror("why");//execl调用失败会个错误码通过perror打印出来
        }
        printf("after execl\n");
        return 0;
}
//文件echoarg.c
#include <stdio.h>
int main(int argc,char*argv[]){
        int i=0;
        for(i=0;i<argc;i++){
                printf("argv[%d]:%s\n",i,argv[i]);
        }
        return 0;
}

利用族函数execl调用ls:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void){
        printf("before execl\n");
        if(execl("/bin/ls","ls",NULL,NULL) == -1){
                printf("ececl failed!\n");
                perror("why");
        }
        printf("after execl\n");
        return 0;
}

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

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void){
        printf("this pro system date:\n");
        if(execlp("ps","ps",NULL,NULL) == -1){//execl要用/bin/ps才能找到
                printf("ececl failed!\n");
                perror("why");
        }
        printf("after execl\n");
        return 0;
}

        带v不带l的一类exac函数,包括execv、execvp、execve,应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。execv与execvp的区别在于execv要加绝对路径才能被找到。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void){
        printf("this pro system date:\n");
        char *argv[] = {"ps",NULL,NULL};
        if(execvp("ps",argv) == -1){
                printf("ececl failed!\n");
                perror("why");
        }
        printf("after execl\n");
        return 0;
}

exec配合fork使用

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

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

int main(){
        pid_t pid;

        int data;
        while(1){
                printf("please input a data:\n");
                scanf("%d",&data);
                if(data == 1){

                        pid = fork();
                        if(pid > 0){
                                wait(NULL);
                        }
                        if(pid == 0){
                                execl("./changData","changeData","config.txt",NULL);
//                              exit(0);                          
                        }

                }
                else{
                        printf("wait do nothing\n");
                }
        }

        return 0;
}

system函数

函数说明

        system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。

       #include <stdlib.h>

       int system(const char *command);

        system()函数的返回值如下:成功,则返回进程的状态值;当sh不能执行时,返回127;失败返回-1;system函数功能与族函数一样,且简单粗暴。

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

int main(){
        pid_t pid;

        int data;
        while(1){
                printf("please input a data:\n");
                scanf("%d",&data);
                if(data == 1){

                        pid = fork();
                        if(pid > 0){
                                wait(NULL);
                        }
                        if(pid == 0){
//                              execl("./changData","changeData","config.txt",NULL);
                                system("./changData config.txt");
                        }

                }
                else{
                        printf("wait do nothing\n");
                }
        }

        return 0;
}

popen函数

函数原型

       #include <stdio.h>

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

       int pclose(FILE *stream);

        popen函数比system在应用中的好处:可以获取运行的输出结果。

参数说明:

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

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

返回值:

        如果调用成功,则返回一个读或者打开文件的指针,如果失败,返回NULL,具体错误要根据errno判断

        int pclose(FILE* stream)

参数说明:

        stream : popen返回文件指针

返回值:

        如果调用失败,返回-1

作用:

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

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

int main(void){
        char ret[1024] = {0};
        FILE *fp;
        fp = popen("ps","r");
        int nread = fread(ret,1,1024,fp);
        printf("read ret = %d byte,ret = %s\n",nread,ret);
        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从入门到捕蛇者说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值