cise
cise

Reputation: 85

Creating Multiple Processes and Communicating with Pipes in C++

I am attempting to create two child processes with a shared parent, the parent must print the output of each of the child processes, It sends a list of numbers to the Child A, child A sorts these numbers, and sends the sorted list to both the parent and child B, child B then finds the median of the list of numbers, and sends it to the parent where it prints both results.

The problem I'm running into is that it executes fine for Child A and prints child A's results, but does not print anything for child 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);
        }   
    }
}

It won't even print: "The median of list is: "

Upvotes: 3

Views: 807

Answers (1)

mevets
mevets

Reputation: 10445

In Child B code, you close(fd[1]) at line 68, then write to it at line 80: write(fd1[1], &median, sizeof(double));.

UNIX gave us return values for a reason.

Upvotes: 1

Related Questions