玩具外壳没有正确配管

时间:2013-10-14 08:21:00

标签: c++ shell process dup2

我不会撒谎。这是一个家庭作业问题。但是,据我所知,这些点已经消失了。现在,我只是在寻找答案,因为我想 - 我可能会疯了。

该程序的目标是以类似于shell的方式执行命令ps -A | grep (inputstring) | wc -l。所以,我产生了进程,让它们互相等待。最新的进程,曾孙,execlp("ps","ps","-A",NULL)取代了ps -A进程。在它execlp之前,我确保它的标准输出转到管道输出。行中的下一个进程是wait(),并且已经自己设置,以便输入管道进入标准输入,标准输出进入输出管道,它将执行grep,依此类推。

我几乎是肯定的,我已经正确设置了它。然而......程序确实如此。不。工作

#include <stdlib.h>
#include <iostream>
#include <string>

#define MAXLINE 1500
#define READ 0
#define WRITE 1

using namespace std;

int main( int argc, char** argv ) {
//* start of input block
if ( argc != 2 ) {
    cout << "Usage: ./a.out arg1" << endl;
    return 0;
}
string in = argv[1];
// end of input block */
int pipeA[2], pipeB[2], pid, stat;

// get our first set of pipes
if ( pipe(pipeA) < 0 ) {
    cerr << "Pipe error.\n";
    exit(-1);
}
if ( pipe(pipeB) < 0 ) {
    cerr << "Pipe error.\n";
    exit(-1);
}

// make the first fork
if ( (pid = fork() ) < 0 ) { cerr << "Fork error.\n"; exit(-1); }

if ( pid > 0 ) {    // parent case
    wait(&stat);
} else {            // child case
    if ( (pid = fork()) < 0 ) { cerr << "Fork Error\n"; exit(-1); }
    if ( pid > 0 ) {    // child
        wait(&stat);
        dup2(pipeA[READ],READ);
        execlp("wc","wc","-l",NULL);
    } else {    // grand-child
        if ( (pid = fork()) < 0 ) { cerr << "Fork Error\n"; exit(-1); }
        if ( pid > 0 ) {    // still grand-child
            wait(&stat);
            dup2(pipeB[READ],READ);  
            dup2(pipeA[WRITE],WRITE); 
            close(pipeB[READ]);
            execlp("grep","grep",in.c_str(),NULL);
        } else {    // great grand-child
            dup2(pipeB[WRITE],WRITE); // t now goes to pipeB[1]
            close(READ);
            close(pipeB[READ]);
            execlp("ps", "ps", "-A", NULL);
        }
    }
}
return 0;
}

编辑:更改为我的代码的双管道变体。

2 个答案:

答案 0 :(得分:1)

我几乎可以肯定这就是你要做的。为马虎编码提前道歉。它在这里有点晚了,我现在应该睡觉了:

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>

#define READ 0
#define WRITE 1

// ps -A | grep argv[1] | wc -l

int main( int argc, char** argv )
{
    //* start of input block
    if ( argc != 2 )
    {
        std::cout << "Usage: ./a.out arg1" << std::endl;
        return 0;
    }

    // make local copy of argument
    std::string in = argv[1];
    int fd1[2], fd2[2], pid;

    // allocate two pipe sets
    if (pipe(fd1) < 0 || pipe(fd2) < 0)
    {
        perror("Failed to create pipe.");
        return EXIT_FAILURE;
    }

    // launch first child process.
    if ((pid = fork()) < 0)
    {
        perror("Failed to fork child(1)");
        return EXIT_FAILURE;
    }

    if (pid == 0)
    {
        // wc -l process. 
        //  stdin  = fd2(read)
        close(fd1[READ]);
        close(fd1[WRITE]);
        close(fd2[WRITE]);
        dup2(fd2[READ],STDIN_FILENO);
        execlp("wc","wc","-l",NULL);
    }

    // fork again. this time for grep
    if ((pid = fork()) < 0)
    {
        perror("Failed to fork child(2)");
        return EXIT_FAILURE;
    }

    if (pid == 0)
    {
        // grep argv[1] process.
        //  stdin  = fd1(read)
        //  stdout = fd2(write)            
        close(fd1[WRITE]);
        close(fd2[READ]);
        dup2(fd2[WRITE], STDOUT_FILENO);
        dup2(fd1[READ], STDIN_FILENO);
        execlp("grep", "grep", in.c_str(), NULL);
    }

    //  fork once more. this time for ps -A
    if ((pid = fork()) < 0)
    {
        perror("Failed to fork child(3)");
        return EXIT_FAILURE;
    }

    if (pid == 0)
    {
        // ps -A process.
        //  stdout = fd1(write)
        close(fd2[WRITE]);
        close(fd2[READ]);
        close(fd1[READ]);
        dup2(fd1[WRITE], STDOUT_FILENO);
        execlp("ps", "ps", "-A", NULL);
    }

    int stat=0;
    wait(&stat);

    return EXIT_SUCCESS;
}

在我的系统上,ps -A报告141行,其中41行在其中的某个位置System,只需运行ps -A | grep System | wc -l即可验证。上面的代码生成了完全相同的输出。

希望它可以帮助你。

答案 1 :(得分:0)

我不确定但是在等待孩子之前调用dup2会解决管道问题。

我不确定的原因是它通常stdin和stdout是缓冲的所以我想即使你在孩子完成运行后将它们与它们挂钩你应该得到相同的结果但是可能(如果有人知道的话)请回答这个问题请更正stdin和stdout的缓冲区在子进程结束时擦除。

另外,您是否可以更新问题中的代码以包含带有两组管道的修改后的代码?