✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
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;}