学习管道和流程

时间:2013-07-28 03:15:13

标签: c unix exec fork pipe

我正在努力更好地了解管道和流程。我想实现多个链式管道,如cat test.txt | sort | uniq -c。我使用cat test.txt启动了我的代码,但它无效。它会编译,但是当我在命令行中提供文件名时,例如./hwk ./test.txt。什么都没有回来。有人可以看看并给我一些提示吗?我想使用循环,因为我希望能够添加更多的管道。我知道我的代码中存在很多问题,所以我希望有人可以就这个主题给我一些指导。感谢。

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

#define SIZE 1024

int main (int argc, char **argv)
{
    int num_pipe = 1;
    int commands = num_pipe + 1; //number of commands is one more than the number of pipes
    int fds[num_pipe * 2];

    int status;
    pid_t pid;
    char *str_ptr;

    //Pass Command
    char *arrayOfCommands[] = {"cat", NULL};


    //Setting up pipes
    int i;
    for (i = 0; i < num_pipe; i++){
        if(pipe(fds + i * 2) == -1) {
            perror("Error creating pipes");
            exit(1);
        }
    }

    int j = 0;
    for (i = 0; i < commands - 1; ++i) {
        pid = fork();

        if (pid == 0) {
            if (i < commands) {
                if (dup2(fds[j+1], 1) < 0) {
                    perror("dup2 error");
                    exit(EXIT_FAILURE);
                }
            }

            if (j != 0) {
                if(dup2(fds[j-2], 0) < 0) {
                    perror("dup2 error");
                    exit(EXIT_FAILURE);
                }
            }

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


            if (execvp(arrayOfCommands[0], arrayOfCommands) < 0) {
                perror("Array error");
                exit(EXIT_FAILURE);
            }


        }
        else if (pid < 0){
            perror("Error");
            exit(EXIT_FAILURE);
        }

        j += 2;
    }

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

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


    return 0;
}

3 个答案:

答案 0 :(得分:1)

我称之为主要是对您的计划p3.c进行微调,将其编译为p3。因为只有一个命令(cat)被调用,所以我玩弄了一些东西,以便它能正常工作。当以./p3 p3.c运行时,它会打印出源代码的内容。

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

static void err_exit(const char *str);

int main (int argc, char **argv)
{
    int num_pipe = 0;            // Just cat - no pipes
    int commands = num_pipe + 1; // Number of commands is one more than the number of pipes
    int fds[num_pipe * 2 + 1];   // Avoid size 0 array
    char *arrayOfCommands[3] = { "cat", NULL, NULL};

    if (argc != 2)
        err_exit("Missing filename argument");
    arrayOfCommands[1] = argv[1];

    for (int i = 0; i < num_pipe; i++)
    {
        if (pipe(fds + i * 2) == -1)
            err_exit("Error creating pipes");
    }

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

        if (pid == 0)
        {
            printf("%d: %s %s\n", (int)getpid(), arrayOfCommands[0], arrayOfCommands[1]);
            fflush(stdout);
            if (i < commands-1 && dup2(fds[j+1], 1) < 0)
                err_exit("dup2 error");
            if (j != 0 && dup2(fds[j-2], 0) < 0)
                err_exit("dup2 error");
            for (i = 0; i < 2*num_pipe; i++)
                close(fds[i]);

            execvp(arrayOfCommands[0], arrayOfCommands);
            err_exit("Array error");
        }
        else if (pid < 0)
            err_exit("Error");

        j += 2;
    }

    for (int i = 0; i < 2 * num_pipe; i++)
        close(fds[i]);

    for (int i = 0; i < num_pipe + 1; i++)
    {
        int status;
        pid_t pid = wait(&status);
        printf("PID %d exited 0x%.4X\n", (int)pid, status);
    }

    return 0;
}

static void err_exit(const char *str)
{
    perror(str);
    exit(EXIT_FAILURE);
}

检查是否适合您。然后你需要弄清楚你将如何创建第二个命令。您的arrayOfCommands无法直接提供帮助。你需要另一种形状或形式的字符串数组。


运行cat file | rev的扩展程序。这些变化实际上很小。我创建a_cat来处理cat命令,a_rev用于rev命令,a_cmds用作命令数组。还有必要将i上的循环修复为k上的循环。

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>

static void err_exit(const char *str);

int main (int argc, char **argv)
{
    int num_pipe = 1;
    int commands = num_pipe + 1; //number of commands is one more than the number of pipes
    int fds[num_pipe * 2 + 1];   // Avoid size 0 array
    char *a_cat[3] = { "cat", NULL, NULL};
    char *a_rev[2] = { "rev", NULL};
    char **a_cmds[] = { a_cat, a_rev };

    if (argc != 2)
        err_exit("Missing filename argument");
    a_cat[1] = argv[1];

    for (int i = 0; i < num_pipe; i++)
    {
        if (pipe(fds + i * 2) == -1)
            err_exit("Error creating pipes");
    }

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

        if (pid == 0)
        {
            printf("%d: %s\n", (int)getpid(), a_cmds[i][0]);
            fflush(stdout);
            if (i < commands-1 && dup2(fds[j+1], 1) < 0)
                err_exit("dup2 error");
            if (j != 0 && dup2(fds[j-2], 0) < 0)
                err_exit("dup2 error");
            for (int k = 0; k < 2*num_pipe; k++)
                close(fds[k]);

            execvp(a_cmds[i][0], a_cmds[i]);
            err_exit("Array error");
        }
        else if (pid < 0)
            err_exit("Error");

        j += 2;
    }

    for (int i = 0; i < 2 * num_pipe; i++)
        close(fds[i]);

    for (int i = 0; i < num_pipe + 1; i++)
    {
        int status;
        pid_t pid = wait(&status);
        printf("PID %d exited 0x%.4X\n", (int)pid, status);
    }

    return 0;
}

static void err_exit(const char *str)
{
    perror(str);
    exit(EXIT_FAILURE);
}

答案 1 :(得分:0)

您没有将程序的命令行参数传递给“cat”子进程。您可以像这样初始化arrayOfCommands - &gt; char *arrayOfCommands[] = {"cat", NULL};&lt; - 然后将其按原样传递给execvp()函数作为第二个参数。

答案 2 :(得分:0)

好的,你的第一个问题就在于:

execvp(arrayOfCommands[0], arrayOfCommands);

您正在使用arrayOfCommands但我不确定如何在未显示文本文件的情况下填充arrayOfCommands。我的意思是你在代码中设置如下的arrayOfCommands:

     char *arrayOfCommands[] = {"cat", "./test.txt", NULL};

如果我理解你的程序被称为hwk,无论出于什么原因你认为应该解析./hwk ./test.txt但这意味着你应该解析argv。

现在好了,让我们看看你如何设置的更大问题。

因此,当一个shell解析出来的管道时,确实会有很多事情发生。请考虑以下事项:

foo fooparam1 fooparam2  | bar  barparam1 | baz bazparam1 bazparam2 

shell使用递归来解决问题:

foo fooparam1 fooparam2 | ( bar barparam1 | baz bazparam1 bazparam2 )

所以它会像SOMETHING一样:

   spawn_sub_pipes(const char *str) {

           char *cmd =  strtok(str, "|");
           char *rest = strtok(NULL, "|");

           int fds[2]; 
           pipe(fds[]);
           int pid = fork();
           if ( pid < 0 ) {
                 perror("pipe error");
                 exit(-1);
           }

           if ( pid ) { /* parent is the writer */
              close(fds[0]); /* close reading pipe */ 
              dup2(fds[1], 1); /* we attach stdout to the pipe */
           }

           if ( pid == 0 ) {
              close(fds[1]);
              dup2(fds[0], 0); /* attach the pipe to stdin */
              if ( rest ) { /* fork next children */
                  spawn_sub_pipes(rest);
              }
              execvpe(cmd);
           }

   }

重要提示

我刚刚编写了上面的代码而没有测试它。从中获取想法但不要逐字使用。

相关问题