c++ - 在 C++ 中创建多个进程并与管道通信

标签 c++ pipe fork

我正在尝试创建两个具有共享父进程的子进程,父进程必须打印每个子进程的输出,它向子 A 发送一个数字列表,子 A 对这些数字进行排序,然后发送排序后的将列表发送给父 B 和子 B,然后子 B 找到数字列表的中位数,并将其发送给打印两个结果的父。

我遇到的问题是它对子 A 执行良好并打印子 A 的结果,但不为子 b 打印任何内容。

#include <string>
#include <vector>
#include <cstdlib>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char **argv)
{
    vector<int> numbers;
    vector<int> sortedVector;
    if (argc == 6)
    {
            //Create vector
            numbers.push_back(atoi(argv[1]));
            numbers.push_back(atoi(argv[2]));
            numbers.push_back(atoi(argv[3]));
            numbers.push_back(atoi(argv[4]));
            numbers.push_back(atoi(argv[5]));
    }
    else
    {
        cout << "Error\n";
    }
    int fd[2];
    int fd1[2];

    if (pipe(fd) == -1 || pipe(fd1) == -1)
    {
        std::cout << "Pipe failed.\n";
    }
    int child_a, child_b;
    child_a = fork();

    //Child A process
    if (child_a == 0)
    {
        //Child A code
        sortedVector = numbers;
        sort(sortedVector.begin(), sortedVector.end());
        int array1[5] = { sortedVector.at(0), sortedVector.at(1), sortedVector.at(2), sortedVector.at(3), sortedVector.at(4) };
        
        //Send to parent
        close(fd[0]);
        write(fd[1], array1, 5*sizeof(int));
        close(fd[1]);

        //Send to B
        close(fd1[0]);
        write(fd1[1], array1, 5*sizeof(int));
        close(fd1[1]);
    }
    else
    {
        child_b = fork();

        if (child_b == 0)
        {

            //Child B code
            cout << "Array from A ";
            //Read from A
            int arrayFromA[5] = { 0, 0, 0, 0, 0 };
            close(fd1[1]);
            read(fd1[0], arrayFromA, 5*sizeof(int));
            close(fd1[0]);
            
            //Find median
            double median; 
            int arraySize = sizeof(arrayFromA) / sizeof(arrayFromA[0]);

            median = (double)(arrayFromA[(arraySize - 1) / 2] + arrayFromA[arraySize / 2]) / 2.0;

            //Send to Parent
            close(fd1[0]);
            write(fd1[1], &median, sizeof(double));
            close(fd1[1]);
        }
        else
        {
            //Parent code
            //Read from Child A
            vector<int> vectorFromChild;
            close(fd[1]);
            int array2[5] = { 0, 0, 0, 0, 0 };
            read(fd[0], array2, 5*sizeof(int));
            close(fd[0]);

            std::cout << "The sorted list: ";
            for (int i = 0; i < 5; i++)
            {   
                if (i != 4)
                {
                    std::cout <<  array2[i] << ", ";
                }
                else
                {
                    std::cout << array2[i] << ".";
                }
                    
            }
            std::cout << std::endl;

            //Read from Child B
            double medianOutput;
            close(fd1[1]);
            read(fd1[0], &medianOutput, sizeof(double));
            close(fd1[0]);

            std::cout << "The median of the list is: " << medianOutput << std::endl;
            wait(NULL);
        }   
    }
}

它甚至不会打印:“列表的中位数是:”

最佳答案

在 Child B 代码中,您在第 68 行close(fd[1]),然后在第 80 行写入:write(fd1[1], &median, sizeof(double ));.

UNIX 为我们提供返回值是有原因的。

关于c++ - 在 C++ 中创建多个进程并与管道通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64813451/

相关文章:

c++ - 当我收到此错误 : <mutex> is not supported when compiling with/clr 时如何实现非托管线程安全集合

bash - 管道和重定向

创建一个进程并告诉它 sleep ?

c++ - stdin/stdout/stderr 上的跨平台 (linux/Win32) 非阻塞 C++ IO

perl - Parallel::ForkManager,当 ->start 返回 false 时会发生什么

c - fork 过程的输出

c++ - 使用键盘 : unintended side effects when pressing enter 将数据输入到 Qt GUI

c++ - 从 Fortran 调用 C++(链接问题?)

c++ - 尝试集成外部源代码时出现编译错误

perl - 从 perl 管道运行 "less"