logo

Crowdly

Browser

Додати до Chrome

Comunicación Bidireccional con Tuberías (Pipes) Lee cuidadosamente el siguient...

✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.

Comunicación Bidireccional con Tuberías (Pipes)

Lee cuidadosamente el siguiente código, que implementa una comunicación unidireccional entre un proceso padre y un proceso hijo utilizando una tubería ordinaria. 

Se desea modificar el programa para agregar una segunda tubería que permita comunicación bidireccional. El hijo debe responder al padre con el mensaje: "Bien, gracias". 

¿Cuál de las siguientes opciones implementa CORRECTAMENTE la respuesta del hijo al padre usando una segunda tubería?

 

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/wait.h>

 

#define BUFFER_SIZE 256

 

int main() {

    int fd[2];

    pid_t pid;

    char mensaje[] = "Hola hijo, ¿cómo estás?";

    char buffer[BUFFER_SIZE];

 

    if (pipe(fd) == -1) {

        perror("pipe");

        exit(EXIT_FAILURE);

    }

 

    pid = fork();

    if (pid < 0) {

        perror("fork");

        exit(EXIT_FAILURE);

    }

 

    if (pid > 0) {  // Padre

        close(fd[0]);  // Cierra extremo de lectura

        write(fd[1], mensaje, strlen(mensaje) + 1);

        close(fd[1]);

        wait(NULL);

    } else {         // Hijo

        close(fd[1]);  // Cierra extremo de escritura

        read(fd[0], buffer, BUFFER_SIZE);

        printf("Hijo recibió: %s\n", buffer);

        close(fd[0]);

        exit(EXIT_SUCCESS);

    }

 

    return 0;

}

 
0%
0%
0%
0%
Більше питань подібних до цього

Хочете миттєвий доступ до всіх перевірених відповідей на grado.pol.una.py?

Отримайте необмежений доступ до відповідей на екзаменаційні питання - встановіть розширення Crowdly зараз!

Browser

Додати до Chrome