创建从父进程 stdin 到子进程 stdin 的管道

发布于 2024-10-18 07:14:41 字数 1796 浏览 4 评论 0原文

我尝试将父进程的标准输入传输到子进程。

标准输入 -->父进程--> child_eingabe[1] --> child_eingabe[0] -->子进程--> stdin

我的小程序以这种方式工作,我可以将父进程中的内容写入管道(使用 write() 命令),并通过 stdin 到达我的孩子。不起作用的是父级的标准输入直接写入管道。这是否按我的计划进行?

#include <iostream>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
        // string to the client
        string sClient = "/tmp/myecho";

        int child_eingabe[2];

        cout << "Creating pipes " << endl;

        if(pipe(child_eingabe) < 0) {
                cout << " Error in pipe " << endl;
        } 

        // forking now
        pid_t pid;

        cout << "Forking process" << endl;

        if((pid = fork()) == 0) {


                close(child_eingabe[1]);

                int fid = -1;
                if(fid=dup2(child_eingabe[0], 0) < 0)
                        cout << "Could not redirect STDIN" << endl;

                close(child_eingabe[0]);

                int result = execve(sClient.c_str(),0,0);
                cout << "something went wrong while starting client" << endl;

                if (result != 0) {
                        cout << "Could not start" << endl;
                }
                exit(0);
        }
        close(child_eingabe[0]);

//      if(dup2(0,child_eingabe[1]) < 0)
        if(dup2(child_eingabe[1],0) < 0)
                cout << "Could not redirect STDIN" << endl;

        close(0);
        while(1)
        {
                sleep(1);
        }
        return 0;
}

如果我正确地看到所有内容,则父部分中的第二个 dup2 不会执行我期望的操作。

I try to transfer the stdin of my parent process to a child process.

stdin --> parentprocess --> child_eingabe[1] --> child_eingabe[0] --> childprocess --> stdin

My small program works in that way, that i can write stuff from my parent process into the pipe (with the write() command), and it reaches my child via stdin. What not works, is that the stdin of the parent is directly written INTO the pipe. Does this work as I plan?

#include <iostream>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
        // string to the client
        string sClient = "/tmp/myecho";

        int child_eingabe[2];

        cout << "Creating pipes " << endl;

        if(pipe(child_eingabe) < 0) {
                cout << " Error in pipe " << endl;
        } 

        // forking now
        pid_t pid;

        cout << "Forking process" << endl;

        if((pid = fork()) == 0) {


                close(child_eingabe[1]);

                int fid = -1;
                if(fid=dup2(child_eingabe[0], 0) < 0)
                        cout << "Could not redirect STDIN" << endl;

                close(child_eingabe[0]);

                int result = execve(sClient.c_str(),0,0);
                cout << "something went wrong while starting client" << endl;

                if (result != 0) {
                        cout << "Could not start" << endl;
                }
                exit(0);
        }
        close(child_eingabe[0]);

//      if(dup2(0,child_eingabe[1]) < 0)
        if(dup2(child_eingabe[1],0) < 0)
                cout << "Could not redirect STDIN" << endl;

        close(0);
        while(1)
        {
                sleep(1);
        }
        return 0;
}

If I see everything correctly, the second dup2 in the parent part does not what i expected it to do.

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

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

发布评论

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

评论(2

缺⑴份安定 2024-10-25 07:14:41

为什么将父母的标准输入重定向到管道?据我了解,这正是您想要避免的。删除第二个 dup2 和 close(0) 应该会给你的父进程留下一个与管道完全断开连接的标准输入。

编辑:澄清问题后 - 想要将数据从父标准输入复制到子标准输入。

像您一样创建子管道,然后手动读取父管道中的标准输入,并使用 child_eingabe[1] 上的 write() 将数据传递给子管道>

Why do you redirect the parents stdin to the pipe? As I understand you, that's exactly what you want to avoid. Removing the second dup2 and close(0) should leave your parent with a stdin that is completely disconnected from the pipe.

EDIT: After clarification of the question - Want to duplicate data from parent stdin to child stdin.

Create the child pipe as you do, then manually read stdin in the parent and pass data to the child with write() on child_eingabe[1]

谷夏 2024-10-25 07:14:41

您实际上需要输入才能进入父进程吗?

如果没有,您可以关闭父进程的标准输入,而不是所有输入都转到子进程。

否则,最简单的方法应该是埃里克写的。

其他方式(不确定是否适用于您的情况)应该将子级的标准输入和父级的标准输出重定向到管道。您的父母读取输入并将其写入其标准输出(孩子的输入)。

Do you actually need the input to go to the parent process?

If not, you can close the parent's stdin, than all input goes to child process.

Otherwise, the easiest way should be what Erik wrote.

Other way (not shure if it applies to your case), should be redirecting the child's stdin and the parent's stdout to a pipe. Your parent reads the input and writes it to it's stdout (the child's input).

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