C 编程 fork() 多个 pipeline()

标签 c unix fork pipe

我正在使用 Pipes() forks() exec() dup() 模拟 shell。我在 stackoverflow 上看到了一些帖子来指导我一路走来。但我的程序似乎也遇到了与其他人在这里遇到的类似问题。

我使用的 LinkedList 结构包含:char* cmd 和 char** args(例如 cmd='ls' args = '-l -d')

以下是一些输出结果: ls-l ls -l -a (我想要的参数数量) ls -l |排序

ls -l | wc -w (runs but spits out wrong value)
ls | wc -w (runs, but spits out the wrong value)
ls (alone - no args, spits out A NULL argv[0] was passed through an exec system call.)
ls -l | sort | wc -w (causes system to hang)

它现在至少假装它正在接受多个参数,但结果无效或系统挂起。

void runCommand(Node* head)
{
    int old_fd[2], new_fd[2];
    int isValid, cpid, pid;

    //Loop through all commands
    for(int cmd = 0; cmd < cmdCount; cmd++)
    {
        //if(curr cmd has next cmd)
        if(cmd+1 < cmdCount)
        {
            //Create pipe new_fd
            if( pipe(new_fd) == -1) //pipe error
            {
            perror("Pipe Error.");
            exit(-1);
            }
        }

        //Parent
        if( (pid=fork()) != 0 ) //parent
        {
            //Wait for child process
            //wait(0); //moved below

            //Curr cmd has next command
            if(cmd+1 < cmdCount)
            {
                old_fd[0] = new_fd[0];
                old_fd[1] = new_fd[1];
            }

            //Curr cmd has previous command
            if(cmd-1 > -1)
            {
            close(old_fd[0]);
            close(old_fd[1]);
            }
        }
        //Child process
        else //if fork() == 0
        {
            //Curr cmd has previous command
            if(cmd-1 > -1)
            {
                dup2(old_fd[0], 0); // setting up old_pipe to input into the child
                close(old_fd[0]);
                close(old_fd[1]);
            }

            //Curr cmd has next command
            if(cmd+1 < cmdCount)
            {
                close(new_fd[0]); // setting up new_pipe to get output from child
                dup2(new_fd[1], 1);
                close(new_fd[1]);
            }

            printf("Running command '%s': \n",getCmd(cmd,head));
            printf("Arguments: "); printArgs(cmd,head);
            //Below works for 1 cmd 1+ args, but not 1 cmd 0 arg or mult pipes
            isValid = execvp(getCmd(cmd,head), getArgs(cmd,head));

            if(isValid == -1)
            printf("%s: Command not found.\n", getCmd(cmd,head));
        }
     wait();

    }
}

请注意此线程:Another_Stack_Overflow_Example我使用了那里的示例,并将我的函数替换为他们的“工作”示例。他们的工作示例似乎适用于大多数情况,除了一个没有像“ls”这样的参数的命令,它只是什么也不做。并要求输入

这里是请求的 getCmd() 和 getArgs() 函数,只需调用“getNode()”,它返回一个 Node* 结构(包含 char* 和一个 char** 以及两个整数),getCmd 提取 char* cmd 并getArgs 提取 char** 参数。

char* getCmd(int index, Node* head)
{
  Node* curr = getNode(index,head);
  return curr->cmd;
}
char** getArgs(int index, Node* head)
{
  Node* curr = getNode(index,head);
  return curr->args;
}

最佳答案

有两件事:

1. 您应该将 NULL 终止的数组传递给 execvp():

execvp("ls", NULL); // invalid!!!

这应该是:

const char *args[1] = { NULL };
execvp("ls", args);

2. “管道运算符(operator)” |是一个 shell 扩展,因此您不能在 exec... 函数中使用它。您只能在那里指定一个可执行文件。

关于C 编程 fork() 多个 pipeline(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13000917/

相关文章:

bash - 如何在bash中杀死后抑制终止消息?

c - fork系统调用

python - 动态命名进程

c - 使用 kill 向父进程发送信号让我退出

c - 如何在头文件中定义函数?

c - 查找数组中的数字是否按顺序排列

linux - 如何连续显示文件的最后几行内容

c - 进程间共享数据队列

c - 我在 c 中找到了纯虚函数的代码。谁能解释一下?

c++ - 当用户按下 'Ctrl-D' 时,我试图结束我的 C++ 程序