《Linux应用进程控制(三) — 进程调用外部脚本exec函数簇、system、popen》

1. exec函数簇

  exec函数族的作用是根据指定的文件名找到可执行文件,并用它来取代调用进程的内容,换句话说,就是在调用进程内部执行一个可执行文件。这里的可执行文件既可以是二进制文件,也可以是任何Linux下可执行的脚本文件。

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

execl与execv区别在于参数传递方式不同,execl将参数存放在一个列表中,execv将参数存放在一个字符串数组中。
execlp和execvp增加了文件查找的路径,优先查找path参数的文件,找不到则到环境变量PATH中去查找。
execle增加了给可执行程序传递环境变量的字符串数组。

 

exec函数簇应用实例:

execl("/bin/ls", "ls", "-a", "-l", NULL);

char *const arg[] = {"ls", "-l", "-a", NULL};
execv("/bin/ls", arg);
execvp("ls",arg);

execle使用实例:

exec.c编译生成exec
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
    char *const envp[] = {"name=scorpio", "host=192.168.6.200", NULL};

    execle("./hello", "hello", NULL, envp);
    return 0;
}

 

hello.c:编译生成hello 

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

extern char **environ;

int main(int argc, char **argv)
{
    printf("hello\n");
    int i = 0;

    for(i = 0; environ[i] != NULL; i++)
    {   
        printf("%s\n", environ[i]);
    }   
    return 0;
}

运行./exec:
hello
name=scorpio
host=192.168.6.200
exec程序中定义的环境变量传递给了调用的hello程序。

  常见错误:

A、找不到文件或路径,此时errno 被设置为ENOENT

B、数组argv和envp忘记用NULL结束,此时errno被设置为EFAULT

C、没有对要执行文件的运行权限,此时errno被设置为EACCES

 

2. system函数

#include <stdlib.h>
int system(const char *__command);

  system()函数调用/bin/sh来执行参数指定的命令,/bin/sh一般是一个软连接,指向某个具体的shell,比如bash,-c选项是告诉shell从字符串command中读取命令。

system函数的源码:

int system(const char * cmdstring)
{
    pid_t pid;
    int status;
    if(cmdstring == NULL)
    {
        return (1); //如果cmdstring为空,返回非零值,一般为1
    }
    if((pid = fork())<0)
    {
        status = -1; //fork失败,返回-1
    }
    else if(pid == 0)
    {
        execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
        _exit(127); // exec执行失败返回127,注意exec只在失败时才返回现在的进程,成功的话现在的
        进程就不存在啦~~
    }
    else //父进程
    {
        while(waitpid(pid, &status, 0) < 0)
        {
            if(errno != EINTR)
            {
                status = -1; //如果waitpid被信号中断,则返回-1
                break;
            }
        }
    }
    return status; //如果waitpid成功,则返回子进程的返回状态
}

 

因此,想用system()函数调用外部程序便十分简单,例如调用/home/usrname/hello/目录下的helloworld可执行文件:
system("/home/usrname/hello/helloworld");

  如果 SIGCHLD 的信号处理函数是 SIG_IGN 或用户设置了SA_NOCLDWAIT 标志位,那么子进程就不进入僵尸状态等待父进程 wait 了。但是 system 函数的内部实现会调用 waitpid 来获取子进程的退出状态。这就是父子之前没有协调好造成的错误。这种情况下, system 返回 -1 , errno 为 ECHLD 。

  所以需要调用 system 函数的时候,先要确认 SIGCHLD 是否被设为 SIG_IGN 。如果是, system 就会返回 -1 ,而无法判断 command 执行成功与否。

  如果调用 system 函数的进程还存在其他子进程,并且对 SIGCHLD 信号的处理函数也执行了 wait ()。那么这种情况下,由 system ()创建的子进程退出并产生 SIGCHLD 信号时,主程序的信号处理函数就可能先被执行,导致 system 函数内部的 waitpid 无法等待子进程的退出,这就产生了竞争。

实际上system()函数执行了三步操作:
  1.fork一个子进程;
  2.在子进程中调用exec函数去执行command;
  3.在父进程中调用wait去等待子进程结束。
  system()函数执行过程为:fork()->exec()->waitpid()
  对于fork失败,system()函数返回-1。
  如果exec执行成功,也即command顺利执行完毕,则返回command通过exit或return返回的值
  (注意,command顺利执行不代表执行成功,比如command:"rm debuglog.txt",不管文件存不存在该command都顺利执行了)
  如果exec执行失败,也即command没有顺利执行,比如被信号中断,或者command命令根本不存在,system()函数返回127。
  如果command为NULL,则system()函数返回非0值,一般为1。

使用system()函数的建议:

1、建议system()函数只用来执行shell命令,因为一般来讲,system()返回值不是0就说明出错了;

2、建议监控一下system()函数的执行完毕后的errno值,争取出错时给出更多有用信息;

3、建议考虑一下system()函数的替代函数popen();

 

3.popen函数

#include <stdio.h>

FILE * popen ( const char * command , const char * type );
int pclose ( FILE * stream );

参数:
    command:是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用-c 标志,shell 将执行这个命令。
    type :只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 "r" 则文件指针连接到 command 的标准输出;如果 type 是 "w" 则文件指针连接到 command 的标准输入。
    popen 的返回值是个标准 I/O 流,必须由 pclose 来终止。

  实例:

#include<stdio.h>
#include<stdlib.h>
int main()
{
      FILE *fp;
     char buffer[80];
     fp = popen("cat /etc/passwd", "r");
     fgets(buffer, sizeof(buffer), fp);
     printf("%s", buffer);
     pclose(fp);
}

  运行结果:

  

 

4.popen和system的区别

  popen和system都可以执行外部命令。
  popen相当于是先创建一个管道,fork,关闭管道的一端,执行exec,返回一个标准的io文件指针。
  system相当于是先后调用了fork, exec,waitpid来执行外部命令
  popen本身是不阻塞的,要通过标准io的读取使它阻塞
  system本身就是阻塞的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

posted @ 2020-06-15 14:28  一个不知道干嘛的小萌新  阅读(669)  评论(0编辑  收藏  举报