c++ - 加工和管道

标签 c++ process pipe

前提:实现一个提示用户输入 2 个命令的 C++ 程序。每个输入字符串应该是一个 UNIX 命令,允许有参数。例如,输入 1 可以是“ls -l”,输入 2 可以是“wc -l”。然后该程序将创建一个管道和两个子进程。第一个子进程将运行第一个输入中指定的命令。它将输出到管道而不是标准输出。第二个子进程将运行第二个输入中指定的命令。它将从管道而不是标准输入获取输入。 父进程将等待它的两个子进程完成,然后整个过程将重复。当输入“quit”作为第一个命令时,执行将停止。

我相信我几乎完成了这个程序,但我在弄清楚如何让用户输入在管道中执行时遇到了麻烦,我最终遇到了语法错误。

到目前为止,这是我的代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
using namespace std;

int main() {
    //Declare Variables
    char first, second[80];
    int rs, pipefd[2];
    int pid1, pid2;
    char *command1[80], *command2[80];

    //Asking for the commands
    cout << "Please enter your first command(incl. args) or quit: ";
    cin >> first;

    //Do the program while the first answer isn't quit
    while(first[0] != 'quit') {

    //Copy first answer into first command
    strcpy(command1, ((char*)first);

    //Just skip to end of program if first command is quit
    cout << "Please enter your second command(incl. args): ";
    cin >> second;

    //Copy second answer into second command
    strcpy(command2, ((char*)first);

    //pipe
    rs = pipe(pipefd);
    //if pipe fails to be made
    if(rs == -1){
        perror("Fail to create a pipe");
        exit(EXIT_FAILURE);
    }

    //Fork for the two processes
    pid1 = fork();

    if (pid1 != 0){
    pid2 = fork();
}

    if(pid1 == -1){
        perror("Fail to create a pipe");
        exit(EXIT_FAILURE);
    }
        if(pid2 == -1){
        perror("Fail to create a pipe");
        exit(EXIT_FAILURE);
    }

        if (pid1 == 0) { // 1st child process   
        // close write end of pipe
        close(pipefd[0]);
        // duplicate
        dup2(pipefd[1], 1);
        //execute the input argument
        execvp(command1[0], command1);
    } 
                if (pid1 == 0) { // 2st child process   
        // close write end of pipe
        close(pipefd[0]);
        // duplicate
        dup2(pipefd[1], 1);
        //execute the input argument
        execvp(command2[0], command2);
    }

        else {  // parent process   
        // close read end of pipe
        close(pipefd[0]);
        // wait for child processes
        wait(&pid1);
        wait(&pid2);    
    }

    //Asking for the commands
    cout << "Please enter your first command(incl. args) or quit: ";
    cin >> first;

    }; //end of while()

return 0;
}

任何帮助/提示将不胜感激,因为这将在不久的将来到期,我很乐意最终解决这个问题。

编辑:添加错误

In function int main()':
z1674058.cxx:26:33: error: expected ')' before ';' token
z1674058.cxx:33:33: error: expected ')' before ';' token

最佳答案

// close write end of pipe
close(pipefd[0]);
// duplicate
dup2(pipefd[1], 1);

您在 child #1 和 child #2 中都有相同的两行。两个 child 在这里应该做相反的操作。其中之一应该是关闭 pipefd[1] 并将 pipefd[0] 复制到标准输入。

您还有一些字符串处理错误。

char first, second[80];

这并没有声明两个 80 个字符的数组。它声明了一个 char 和一个 char[80]。我怀疑这就是您在此处输入 (char *) 强制转换的原因——以消除编译器错误:

strcpy(command1, ((char*)first);

不要那样做。不要使用强制转换来关闭编译器。类型转换掩盖了一个严重的错误。

char *command1[80], *command2[80];

这些声明也是不正确的。这声明了两个数组,每个数组包含 80 个 char *。也就是说,两个数组各包含 80 个字符串。我会留给你解决这个问题的办法......

关于c++ - 加工和管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16049899/

相关文章:

c++ - 使用转发引用时,std::move 是否需要与 std::forward 结合使用?

java - 执行命令时如何获取所有错误?

c - 使用 dup2 重定向 stdin 和 stdout 时出现未定义错误

.net - 2个进程(应用程序)之间的共享内存

c++ - 使用 Qt 开发音频游戏?

c++ - 在 Qt 中隐藏或显示 QStackedWidget 项

c++ - 在编译时选择全局作用域函数

linux - 后台进程在关闭 ssh 客户端后终止

swift - 在 Swift 中创建带参数的进程?