dup2 的问题

发布于 2024-09-17 16:39:28 字数 1352 浏览 8 评论 0原文

将 Ben Voigt 的答案合并到代码中后,它似乎有效

原始问题:

我正在尝试使用 dup2 来:

  1. 将“ls -al”的输出作为输入传递给“grep foo”,
  2. 其输出成为“grep bar”的输入",
  3. 最终输出到标准输出。

最终输出是(blank),文件“in”是(blank) &文件“out”具有“ls -al”的输出。

有什么想法可能会发生什么吗?

int main()
{
    pid_t pid;
    int i;
    int inFileDes,outFileDes;   
    inFileDes=open("in",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR); 
    outFileDes=open("out",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR);  
    for(i=0;i<3;i++)
    {   
        if((pid=fork())==0)
        {
            switch(i)
            {
                case 0:
                dup2(outFileDes,1);
                execl("/bin/ls","ls","-al",0);
                break;
                case 1:
                                                  // originally:
                dup2(outFileDes,0);   // dup2(outFileDes,1);  
                dup2(inFileDes,1);    // dup2(inFileDes,0);

                execl("/bin/grep","grep","foo",0);   //lines having foo
                break;
                case 2:
                dup2(inFileDes,0);
                execl("/bin/grep","grep","bar",0);  //lines having foo & bar
                break;
            }
            exit(-1);  //in error   
        }
        waitpid(pid,NULL,0);
    }
    close(inFileDes);
    close(outFileDes);
    return(0);
}

After incorporating Ben Voigt's answer into the code, it appears to work

Original question:

I'm trying to use dup2 to:

  1. pass the output of "ls -al" as input to "grep foo",
  2. whose output becomes input for "grep bar",
  3. which finally outputs to stdout.

The final output is (blank), the file "in" is (blank) & the file "out" has the output of "ls -al".

Any ideas what could be happening?

int main()
{
    pid_t pid;
    int i;
    int inFileDes,outFileDes;   
    inFileDes=open("in",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR); 
    outFileDes=open("out",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR);  
    for(i=0;i<3;i++)
    {   
        if((pid=fork())==0)
        {
            switch(i)
            {
                case 0:
                dup2(outFileDes,1);
                execl("/bin/ls","ls","-al",0);
                break;
                case 1:
                                                  // originally:
                dup2(outFileDes,0);   // dup2(outFileDes,1);  
                dup2(inFileDes,1);    // dup2(inFileDes,0);

                execl("/bin/grep","grep","foo",0);   //lines having foo
                break;
                case 2:
                dup2(inFileDes,0);
                execl("/bin/grep","grep","bar",0);  //lines having foo & bar
                break;
            }
            exit(-1);  //in error   
        }
        waitpid(pid,NULL,0);
    }
    close(inFileDes);
    close(outFileDes);
    return(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-09-24 16:39:28

您的 open 调用会在“in”中创建一个空文件,并且没有程序写入该文件,因此这是预期的。由于 grep 的两个实例都是从空文件中读取的,因此它们的输出也是空的。

您真正想要的是使用 pipe 函数获取一对句柄,将其写入一个程序并由下一个程序读取。您需要调用它两次,因为子进程之间有两组连接。

Your open call creates an empty file "in" and none of the programs write to it, so that's expected. Since both instances of grep read from an empty file, their output is also empty.

What you really want is to use the pipe function to get a pair of handles, which is written to be one program and read from by the next. You'll need to call it twice because you have two sets of connections between child processes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文