【问题标题】:IPC using multiple pipes and forked processes to run Python programsIPC 使用多个管道和分叉进程来运行 Python 程序
【发布时间】:2019-04-07 20:43:14
【问题描述】:

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

我的 C++ 程序(sample.cpp):

#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()
{
    while (true)
    {
        cout << "lol" << endl;
        sleep(2);
    }
    return 0;
}

我的 Python 程序 1 (sample.py):

import sys

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

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

import sys

while True:
    line = sys.stdin.readline().strip()
    print "Python 2 says: " + str(line)

这是我的驱动程序 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);
    }
}

当我运行这个程序时,我应该会看到“Python 2 说:Python 说:lol”。但是,我什么也没看到(完全空白)......它只是挂起。我究竟做错了什么?我尝试查找了很多东西,但没有运气。

【问题讨论】:

    标签: c++ pipe fork ipc dup2


    【解决方案1】:

    ./sample 开头的 while 循环是没有意义的,除非您期望 execvp 失败。对 exec* 的成功调用将永远不会返回。对 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)
    

    sample2.py

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

    驱动程序.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);
        }
    }
    

    【讨论】:

    • 我不知道您所说的不从管道读取是什么意思。我要做的就是通过 dup2-ing stdin/stdout 将所有输出从 sample.cpp 重定向到 sample.py 到 sample2.py。从管道中读取是什么意思?另外我不太明白你喂一根绳子是什么意思?你是说python程序需要参数吗?如果是这样,python 程序的唯一目的是从标准输入读取并生成到标准输出(并且不接受任何参数)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多