【问题标题】:Executing Multiple Pipes in a Loop在循环中执行多个管道
【发布时间】:2012-09-30 23:55:39
【问题描述】:

如此接近于找出我一直在为用 C 编写的 linux shell 编写的程序。我一直想让它工作一段时间,我决定拿起它再次,并且在过去的几周里一直在修补它。

对于以下代码,请记住名为 arrayOfCommands 的数组是动态填充的。我的代码用正在运行的当前命令填充 arrayOfCommands。为了我的示例,我们将运行命令 ls -l | wc 和 arrayOfCommands 填充以下内容,具体取决于循环的时间:

//Pass #1
arrayOfCommands[]= ("ls", "-l", NULL)

//Pass #2
arrayOfCommands[]= ("wc", NULL)

这是我目前所拥有的:

//PIPING
int do_command(char **args, int pipes) {
    // pipes is the number of pipes in the command
    // (In our example, one)


    // The number of commands is one more than the
    // number of pipes (In our example, two)
    const int commands = pipes + 1;  //Ex: 2
    int i = 0;

    // Set up the pipes
    int pipefds[2*pipes];

    for(i = 0; i < pipes; i++){
        if(pipe(pipefds + i*2) < 0) {
            perror("Couldn't Pipe");
            exit(EXIT_FAILURE);
        }
    }

    // Variables
    int pid;
    int status;
    char *str_ptr;

    int j = 0;
    for (i = 0; i < commands; ++i) {

        // A magic function that updates arrayOfCommands with
        // the current command goes here.  It doesn't make 
        // sense in the context of the code, so just believe me! :)

        // Ex: The contents will be "ls -l" or "wc" depending on 
        // which time through the loop we are

        pid = fork();

        if(pid == 0) {
            //if not last command
            if(i < commands){
                if(dup2(pipefds[j + 1], 1) < 0){
                    perror("dup2");
                    exit(EXIT_FAILURE);
                }
            }

            //if not first command&& j!= 2*pipes
            if(j != 0 ){
                if(dup2(pipefds[j-2], 0) < 0){
                    perror("dup2");
                    exit(EXIT_FAILURE);
                }
            }

            for(i = 0; i < 2*pipes; i++){
                    close(pipefds[i]);
            }

            // Should any of the below inputs be *arrayOfCommands or 
            // **arrayOfCommands or &arrayOfCommands?
            // I'm REALLY bad with pointers

            if( execvp(arrayOfCommands, arrayOfCommands) < 0 ){
                    perror(arrayOfCommands);
                    exit(EXIT_FAILURE);
            }
        }
        else if(pid < 0){
            perror("error");
            exit(EXIT_FAILURE);
        }

        j+=2;
    }

    for(i = 0; i < 2 * pipes; i++){
        close(pipefds[i]);
    }

    for(i = 0; i < pipes + 1; i++){
    }
        wait(&status);
}

当我运行它时,我得到了几个错误:

  • dup2:错误的文件描述符
  • ls:|:没有这样的文件或目录
  • ls: wc: 没有这样的文件或目录

谁能帮我弄清楚以下两件事:

  1. 为什么会出现这些错误?
  2. 在 execvp 函数中,我在寻找什么样的指针? arrayOfCommands 被初始化为 char *arrayOfArgs[]

【问题讨论】:

标签: c linux shell piping


【解决方案1】:

第一件事:

//if not last command
if(i < commands)

应该是

if(i < commands -1)

因为i0变为commands -1 这应该解决 dup2: Bad file descriptor

ls: |: 没有这样的文件或目录

ls: wc: 没有这样的文件或目录

是由格式错误的arrayOfCommands 引起的。它必须由

初始化
char * arrayOfCommands[] = {"ls", "-l", NULL};

char * arrayOfCommands[] = {"wc", NULL};

分别通过execvp(arrayOfCommands[0], arrayOfCommands)调用

基本上arrayOfCommands 必须与int main(int argc, char** argv) 的参数向量(通常为argv)的格式相同。

【讨论】:

    猜你喜欢
    • 2021-02-28
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多