C++ IPC使用多管道和分叉进程运行Python程序

C++ IPC使用多管道和分叉进程运行Python程序,c++,pipe,fork,ipc,dup2,C++,Pipe,Fork,Ipc,Dup2,我的作业遇到了一个难题。我尝试执行3个并发进程(C++),其中2个是Python程序,其中一个是C++程序。p> 我的C++程序(SAMP.CPP): 我的Python程序2(sample2.py): 这是我的驱动程序C++程序,它的处理过程是: #include <unistd.h> #include <stdio.h> #include <string.h> #include <string> #include <stdlib.h&g

我的作业遇到了一个难题。我尝试执行3个并发进程(C++),其中2个是Python程序,其中一个是C++程序。p>

我的C++程序(SAMP.CPP):

我的Python程序2(sample2.py):

这是我的驱动程序C++程序,它的处理过程是:

#include <unistd.h>
#include <stdio.h>
#include <string.h>  
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector> 
#include <signal.h>

using namespace std;

int main()
{
    vector<pid_t> kids;

    int fd[2];
    if (pipe(fd) < 0)
    {
        cout << "Error"; 
        return 1; 
    }

    int fd2[2];
    if (pipe(fd2) < 0)
    {
        cout << "Error";
        return 1;
    }

    pid_t pid;

    pid = fork();
    if (pid == 0)
    {
        dup2(fd[1], STDOUT_FILENO);
        close(fd[1]);
        close(fd[0]);

        while (true)
        {
            execvp("./sample", NULL);
        }
    }
    else
    {
        kids.push_back(pid);

        pid = fork();
        if (pid == 0)
        {
            dup2(fd[0], STDIN_FILENO);
            close(fd[0]);
            close(fd[1]);

            dup2(fd2[1], STDOUT_FILENO);
            close(fd2[1]);
            close(fd2[0]);

            char * python = "/usr/bin/python";
            char * pythonProgram = "./sample.py"; 
            char * pythonArgs[] = {python, pythonProgram, NULL, NULL};
            execvp(python, pythonArgs);
        }
        else
        {
            kids.push_back(pid);
            pid = fork();
            if (pid == 0)
            {
                dup2(fd2[0], STDIN_FILENO); 
                close(fd2[0]);
                close(fd2[1]);

                char * python = "/usr/bin/python";
                char * pythonProgram = "./sample2.py"; 
                char * pythonArgs[] = {python, pythonProgram, NULL, NULL};
                execvp(python, pythonArgs);
            }
            else
            {
                kids.push_back(pid);
            }
        }
    }

    close(fd[0]);
    close(fd[1]);
    close(fd2[0]);
    close(fd2[1]);

    for (pid_t k : kids) 
    {
        int status;
        //kill (k, SIGTERM);
        waitpid(k, &status, 0);
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
媒介儿童;
int-fd[2];
如果(管道(fd)<0)
{

cout除非您期望execvp失败,否则围绕./sample开头的while循环是毫无意义的。对execvp*的成功调用将永远不会返回。对execvp*的实际调用也是错误的:

execvp("./sample", NULL);
第二个参数应该是
char*const[]

您应该为execvp:s添加错误处理(如带有
std::exit(1)
)的行)。否则,如果execvp失败,您将在程序的主流程中运行子进程

python程序需要在没有缓冲的情况下运行,否则消息将需要很长时间才能显示。您还应该检查readline是否成功

sample.py

import sys

while True:
    line = sys.stdin.readline().strip()
    if not line: break
    print "Python says: " + str(line)
样本2.py

import sys

while True:
    line = sys.stdin.readline().strip()
    if not line: break
    print "Python 2 says: " + str(line)
driver.cpp

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
    vector<pid_t> kids;

    int fd[2];
    if (pipe(fd)==-1)
    {
        clog << "Error\n";
        return 1;
    }

    int fd2[2];
    if (pipe(fd2)==-1)
    {
        clog << "Error\n";
        return 1;
    }

    pid_t pid;

    pid = fork();
    if (pid == 0)
    {
        dup2(fd[1], STDOUT_FILENO);
        close(fd[1]);
        close(fd[0]);

        char* const args[] = { NULL };
        execvp("./sample", args);
        std::clog << "sample failed\n";
        std::exit(1);
    }
    else
    {
        kids.push_back(pid);

        pid = fork();
        if (pid == 0)
        {
            dup2(fd[0], STDIN_FILENO);
            close(fd[0]);
            close(fd[1]);

            dup2(fd2[1], STDOUT_FILENO);
            close(fd2[1]);
            close(fd2[0]);

            char const* python = "/usr/bin/python";
            char const* pythonProgram = "./sample.py";
            char const* pythonArgs[] = {python, "-u", pythonProgram, NULL};
            execvp(python, const_cast<char* const*>(pythonArgs));
            std::clog << "sample.py failed\n";
            std::exit(1);
        }
        else
        {
            kids.push_back(pid);
            pid = fork();
            if (pid == 0)
            {
                dup2(fd2[0], STDIN_FILENO);
                close(fd2[0]);
                close(fd2[1]);

                char const* python = "/usr/bin/python";
                char const* pythonProgram = "./sample2.py";
                char const* pythonArgs[] = {python, "-u", pythonProgram, NULL};
                execvp(python, const_cast<char* const*>(pythonArgs));
                std::clog << "sample2.py failed\n";
                std::exit(1);
            }
            else
            {
                kids.push_back(pid);
            }
        }
    }

    close(fd[0]);
    close(fd[1]);
    close(fd2[0]);
    close(fd2[1]);

    for (pid_t k : kids)
    {
        int status;
        //kill (k, SIGTERM);
        waitpid(k, &status, 0);
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
媒介儿童;
int-fd[2];
如果(管道(fd)=-1)
{

阻塞我不知道你说的不从管道读取是什么意思。我只是想通过复制stdin/stdout将所有输出从sample.cpp重定向到sample.py再重定向到sample2.py。你说的从管道读取是什么意思?我也不太明白你说的输入字符串是什么意思?你是说python程序需要参数吗?如果是,唯一的目的是python程序的ose是从stdin读取数据并生成到stdout(而不接受任何参数)。
import sys

while True:
    line = sys.stdin.readline().strip()
    if not line: break
    print "Python says: " + str(line)
import sys

while True:
    line = sys.stdin.readline().strip()
    if not line: break
    print "Python 2 says: " + str(line)
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
    vector<pid_t> kids;

    int fd[2];
    if (pipe(fd)==-1)
    {
        clog << "Error\n";
        return 1;
    }

    int fd2[2];
    if (pipe(fd2)==-1)
    {
        clog << "Error\n";
        return 1;
    }

    pid_t pid;

    pid = fork();
    if (pid == 0)
    {
        dup2(fd[1], STDOUT_FILENO);
        close(fd[1]);
        close(fd[0]);

        char* const args[] = { NULL };
        execvp("./sample", args);
        std::clog << "sample failed\n";
        std::exit(1);
    }
    else
    {
        kids.push_back(pid);

        pid = fork();
        if (pid == 0)
        {
            dup2(fd[0], STDIN_FILENO);
            close(fd[0]);
            close(fd[1]);

            dup2(fd2[1], STDOUT_FILENO);
            close(fd2[1]);
            close(fd2[0]);

            char const* python = "/usr/bin/python";
            char const* pythonProgram = "./sample.py";
            char const* pythonArgs[] = {python, "-u", pythonProgram, NULL};
            execvp(python, const_cast<char* const*>(pythonArgs));
            std::clog << "sample.py failed\n";
            std::exit(1);
        }
        else
        {
            kids.push_back(pid);
            pid = fork();
            if (pid == 0)
            {
                dup2(fd2[0], STDIN_FILENO);
                close(fd2[0]);
                close(fd2[1]);

                char const* python = "/usr/bin/python";
                char const* pythonProgram = "./sample2.py";
                char const* pythonArgs[] = {python, "-u", pythonProgram, NULL};
                execvp(python, const_cast<char* const*>(pythonArgs));
                std::clog << "sample2.py failed\n";
                std::exit(1);
            }
            else
            {
                kids.push_back(pid);
            }
        }
    }

    close(fd[0]);
    close(fd[1]);
    close(fd2[0]);
    close(fd2[1]);

    for (pid_t k : kids)
    {
        int status;
        //kill (k, SIGTERM);
        waitpid(k, &status, 0);
    }
}