Связь между 3 процессами с использованием труб для печати шаблона - PullRequest
0 голосов
/ 21 апреля 2020

У меня проблемы с каналами и процессами. Цель состоит в том, чтобы нарисовать пирамиду с N линиями.

У меня есть 3 процесса:

  • первый запросит у пользователя номер строки N и отправит его второму процессу
  • второй строит пирамиду и отправляет ей третий процесс
  • третий, который является родителем двух процессов, должен отображать пирамиду.

Код, который я написал, зависает и не показывает пирамиду, и я не знаю почему.

Это код с исполнением:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{

    int pip1[2]; // child 1 -- child 2 : send the number of rows N
    int pip2[2]; // child 2 -- parent   : child 2 send the pyramid to the parent

    int buf_N[1];             // input the number N
    buf_N[0] = atoi(argv[1]); 

    int F1_pid; // child process 1
    int F2_pid; // child process 2

    if (pipe(pip1) == -1) // creation of pipe 1
    {
        perror("pipe 1 error creation !");
        exit(EXIT_FAILURE);
    }
    if (pipe(pip2) == -1) // creation of pipe 2
    {
        perror("pipe 2 error creation !");
        exit(EXIT_FAILURE);
    }

    F1_pid = fork(); // creation of child 1

    if (F1_pid == -1) // test creation child 1
    {
        perror("error fils 1 creation");
        exit(EXIT_FAILURE);
    }

    if (F1_pid == 0) // child 1 write the number N in the pipe
    {
        close(pip1[0]);
        write(pip1[1], buf_N, sizeof(unsigned int));
        close(pip1[1]);
        exit(EXIT_SUCCESS);
    }



    F2_pid = fork(); // creation of child 2

    if (F2_pid == -1) // test creation child 2
    {
        perror("error fils 1 creation");
        exit(EXIT_FAILURE);
    }

    //char strings[buf_N[0]][buf_N[0]+1];

    char transport[buf_N[0] + 1]; // string that transport the row to the parent using  the pipe

    int TOTAL_rows = buf_N[0], CURRENT_row, spaces, symbol;

    if (F2_pid == 0) // child 2 read the number N   -------------- and create the pyramid
    {
        wait(NULL);
        close(pip1[1]);
        read(pip1[0], buf_N, sizeof(unsigned int));
        printf("voila le nombre de ligne %d \n", buf_N[0]);
        close(pip1[0]);

        for (CURRENT_row = 1; CURRENT_row <= TOTAL_rows; CURRENT_row++)
        {
            strcat(transport, " ");
            // print the spaces
            for (spaces = 1; spaces <= TOTAL_rows - CURRENT_row; spaces++)
            {
                strcat(transport, " ");
            }
            // print the asteriks
            for (symbol = 1; symbol <= ((2 * CURRENT_row) - 1); symbol++)
            {
                strcat(transport, "8");
            }

            close(pip2[0]);
            write(pip2[1], transport, buf_N[0] + 1); // writing the current row into the pipe
            close(pip2[1]);

            transport[0] = '\0'; // clear the string "transport" to use it again
        }
        exit(EXIT_SUCCESS);
    }
    else
    {
        wait(NULL);        
        close(pip2[1]);
        read(pip2[0], transport, buf_N[0] + 1); // read the string that hold the row 
        printf("%s", transport);
        close(pip2[0]);
    }

    return 0;
}

Это снимок экрана с исполнением:

screenshot

...