C++ Linux C++;运行新流程并与之沟通

C++ Linux C++;运行新流程并与之沟通,c++,linux,unix,C++,Linux,Unix,我需要制作一个程序来运行一个进程(我的另一个程序),并且可以与这个进程通信(发送stdin和接收stdout)。 我读过一些函数,如popen()和CreateProcess(),但我真的不知道如何使用它们 如果您能给我看一些示例代码(如何启动进程、发送stdin、接收stdout),那就太好了。 最好是C++函数(如果有)。 谢谢您的建议。POSIX函数接口仅适用于C语言。但是你可以在C++中使用它们。 基本上: #include <unistd.h> // Include som

我需要制作一个程序来运行一个进程(我的另一个程序),并且可以与这个进程通信(发送stdin和接收stdout)。 我读过一些函数,如
popen()
CreateProcess()
,但我真的不知道如何使用它们

如果您能给我看一些示例代码(如何启动进程、发送stdin、接收stdout),那就太好了。 最好是C++函数(如果有)。
谢谢您的建议。

POSIX函数接口仅适用于C语言。但是你可以在C++中使用它们。 基本上:

#include <unistd.h>
// Include some other things I forgot. See manpages.

int main()
{
    // Open two pipes for communication
    // The descriptors will be available to both
    // parent and child.
    int in_fd[2];
    int out_fd[2];

    pipe(in_fd);  // For child's stdin
    pipe(out_fd); // For child's stdout

    // Fork
    pid_t pid = fork();

    if (pid == 0)
    {
        // We're in the child
        close(out_fd[0]);
        dup2(out_fd[1], STDOUT_FILENO);
        close(out_fd[1]);

        close(in_fd[1]);
        dup2(in_fd[0], STDIN_FILENO);
        close(in_fd[0]);

        // Now, launch your child whichever way you want
        // see eg. man 2 exec for this.

        _exit(0); // If you must exit manually, use _exit, not exit.
                  // If you use exec, I think you don't have to. Check manpages.
    }

    else if (pid == -1)
        ; // Handle the error with fork

    else
    {
        // You're in the parent
        close(out_fd[1]);
        close(in_fd[0]);

        // Now you can read child's stdout with out_fd[0]
        // and write to its stdin with in_fd[1].
        // See man 2 read and man 2 write.

        // ...

        // Wait for the child to terminate (or it becomes a zombie)
        int status
        waitpid(pid, &status, 0);

        // see man waitpid for what to do with status
    } 
}
#包括
//包括一些我忘了的东西。见手册页。
int main()
{
//打开两条管道进行通信
//这两个描述符都可用
//父母和孩子。
int in_fd[2];
int out_fd[2];
管道(in_fd);//用于儿童标准
管道(out_fd);//用于儿童标准件
//叉子
pid_t pid=fork();
如果(pid==0)
{
//我们在孩子身上
关闭(out_fd[0]);
dup2(输出fd[1],标准输出文件号);
关闭(out_fd[1]);
关闭(in_fd[1]);
dup2(在标准fd[0]中,标准文件号);
关闭(in_fd[0]);
//现在,让你的孩子以你想要的方式出生
//关于这一点,请参见例如man 2 exec。
_退出(0);//如果必须手动退出,请使用_exit,而不是exit。
//如果你使用exec,我想你不必。检查手册页。
}
否则如果(pid==-1)
;//使用fork处理错误
其他的
{
//你是家长
关闭(out_fd[1]);
关闭(in_fd[0]);
//现在您可以不使用out_fd[0]读取孩子的标准输出
//并用in_fd[1]写入其标准输入。
//参见第二人阅读和第二人书写。
// ...
//等待子项终止(或它变成僵尸)
智力状态
waitpid(pid和status,0);
//有关如何处理状态,请参见man waitpid
} 
}
不要忘记检查错误代码(我没有检查),并参考手册页了解详细信息。但您看到了要点:当您打开文件描述符(例如,通过
管道
)时,它们将对父级和子级可用。父节点关闭一端,子节点关闭另一端(并重定向第一端)


要聪明,不要害怕谷歌和手册页。

POSIX功能的界面仅限于C语言。但是你可以在C++中使用它们。 基本上:

#include <unistd.h>
// Include some other things I forgot. See manpages.

int main()
{
    // Open two pipes for communication
    // The descriptors will be available to both
    // parent and child.
    int in_fd[2];
    int out_fd[2];

    pipe(in_fd);  // For child's stdin
    pipe(out_fd); // For child's stdout

    // Fork
    pid_t pid = fork();

    if (pid == 0)
    {
        // We're in the child
        close(out_fd[0]);
        dup2(out_fd[1], STDOUT_FILENO);
        close(out_fd[1]);

        close(in_fd[1]);
        dup2(in_fd[0], STDIN_FILENO);
        close(in_fd[0]);

        // Now, launch your child whichever way you want
        // see eg. man 2 exec for this.

        _exit(0); // If you must exit manually, use _exit, not exit.
                  // If you use exec, I think you don't have to. Check manpages.
    }

    else if (pid == -1)
        ; // Handle the error with fork

    else
    {
        // You're in the parent
        close(out_fd[1]);
        close(in_fd[0]);

        // Now you can read child's stdout with out_fd[0]
        // and write to its stdin with in_fd[1].
        // See man 2 read and man 2 write.

        // ...

        // Wait for the child to terminate (or it becomes a zombie)
        int status
        waitpid(pid, &status, 0);

        // see man waitpid for what to do with status
    } 
}
#包括
//包括一些我忘了的东西。见手册页。
int main()
{
//打开两条管道进行通信
//这两个描述符都可用
//父母和孩子。
int in_fd[2];
int out_fd[2];
管道(in_fd);//用于儿童标准
管道(out_fd);//用于儿童标准件
//叉子
pid_t pid=fork();
如果(pid==0)
{
//我们在孩子身上
关闭(out_fd[0]);
dup2(输出fd[1],标准输出文件号);
关闭(out_fd[1]);
关闭(in_fd[1]);
dup2(在标准fd[0]中,标准文件号);
关闭(in_fd[0]);
//现在,让你的孩子以你想要的方式出生
//关于这一点,请参见例如man 2 exec。
_退出(0);//如果必须手动退出,请使用_exit,而不是exit。
//如果你使用exec,我想你不必。检查手册页。
}
否则如果(pid==-1)
;//使用fork处理错误
其他的
{
//你是家长
关闭(out_fd[1]);
关闭(in_fd[0]);
//现在您可以不使用out_fd[0]读取孩子的标准输出
//并用in_fd[1]写入其标准输入。
//参见第二人阅读和第二人书写。
// ...
//等待子项终止(或它变成僵尸)
智力状态
waitpid(pid和status,0);
//有关如何处理状态,请参见man waitpid
} 
}
不要忘记检查错误代码(我没有检查),并参考手册页了解详细信息。但您看到了要点:当您打开文件描述符(例如,通过
管道
)时,它们将对父级和子级可用。父节点关闭一端,子节点关闭另一端(并重定向第一端)

要聪明,不要害怕谷歌和手册页