我试图更好地了解管道和流程。我想实现多个链接管道,例如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;
}

最佳答案

我称这主要是对程序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);
}

关于c - 学习管道和过程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17904646/

10-15 05:13