简体   繁体   English

通过多个叉将标准输入输出

[英]piping stdin/out through multiple forks

I can't figure out why I can pipe successfully through one fork but not through 2. The first example gives the expected output equivalent to "ps -A | grep bash" and the second example should give the output of "ps -A | grep bash | wc -l" which would just be the number of lines produced by the first output. 我不知道为什么我可以成功地通过一个分支而不是通过2进行管道传递。第一个示例给出的期望输出等效于“ ps -A | grep bash”,第二个示例给出的输出为“ ps -A | grep bash” grep bash | wc -l”,它只是第一个输出产生的行数。 Instead it gives no output and just hangs. 相反,它不提供任何输出而只是挂起。

This works: 这有效:

#include <iostream>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>     

using namespace std;

int main(int argc, char* argv[])
{
    int p1[2], p2[2];               
    pipe(p1); pipe(p2);             
    pid_t pID;

    pID = fork();

    if (pID == 0)       
    {
        close(p2[0]);                               
        dup2(p2[1], 1);                     
        close(p2[1]);                       
        execlp("ps", "ps", "-A", 0);        // print all processes  
    }   
    else
    {   
        wait(pID);                          
        close(p2[1]);                       
        dup2(p2[0],0);                      
        close(p2[0]);                       
        execlp("grep", "grep", "bash", NULL);   // search for bash (in process list)
    }
}

But this doesn't: 但这不是:

#include <iostream>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>     

using namespace std;

int main(int argc, char* argv[])
{
    int p1[2], p2[2];               
    pipe(p1); pipe(p2);             
    pid_t pID;

    pID = fork();

    if (pID == 0)       
    {
        pID = fork();

        if (pID == 0)
        {   
            close(p2[0]);                               
            dup2(p2[1], 1);                     
            execlp("ps", "ps", "-A", 0);        // print all processes  
        }   
        else
        {   
            wait(pID);                          
            close(p2[1]);                       
            dup2(p2[0],0);                      
            close(p1[0]);                               
            dup2(p1[1], 1);                     
            execlp("grep", "grep", "bash", 0);  // search for bash (in process list)
        }
    }
    else
    {   
        wait(pID);
        close(p1[1]);                       
        dup2(p1[0],0);                      
        execlp("wc", "wc", "-l", 0);            // count lines
    }
}

As moeCoke and Leffeir stated, if you change a single line in your code, it works absolutely fine. 正如moeCoke和Leffeir所说,如果您在代码中更改了一行,它就可以正常工作。

#include <iostream>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>     

using namespace std;

int main(int argc, char* argv[])
{
    int p1[2], p2[2];               
    pipe(p1);            
    pid_t pID;

    pID = fork();

    if (pID == 0)       
    {
        pipe(p2);  // <-- call pipe for p2 here ---
        pID = fork();

        if (pID == 0)
        {   
            close(p2[0]);                               
            dup2(p2[1], 1);                     
            execlp("ps", "ps", "-A", 0);        // print all processes  
        }   
        else
        {   
            wait(pID);                          
            close(p2[1]);                       
            dup2(p2[0],0);                      
            close(p1[0]);                               
            dup2(p1[1], 1);                     
            execlp("grep", "grep", "p", 0);  // search for bash (in process list)
        }
    }
    else
    {   
        wait(pID);
        close(p1[1]);                       
        dup2(p1[0],0);                      
        execlp("wc", "wc", "-l", 0);            // count lines
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM