IPC использует несколько каналов и разветвленных процессов для запуска программ на Python - PullRequest
0 голосов
/ 04 ноября 2018

У меня проблема с моим заданием. Я пытаюсь выполнить 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». Тем не менее, я ничего не вижу (полностью пустой) ... он просто зависает. Что я делаю неправильно? Я пытался найти много вещей, но безуспешно.

1 Ответ

0 голосов
/ 04 ноября 2018

Цикл while вокруг начала ./sample не имеет смысла, если только вы не ожидаете сбоя execvp. Успешный вызов exec * никогда не вернется. Фактический вызов execvp тоже неверен:

execvp("./sample", NULL);

второй аргумент должен быть char *const[].

Вы должны добавить обработку ошибок для execvp: s (как строка с std::exit(1)). В противном случае, если execvp завершится неудачно, у вас будут дочерние процессы, запущенные в основном потоке программы.

Программы на python должны запускаться без буферизации, иначе для появления сообщений потребуется много времени. Вы также должны проверить, успешно ли прочитана строка.

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)

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);
    }
}
...