subidos ejercicios de pipe

This commit is contained in:
Pau 2024-10-01 18:35:32 +02:00
parent a99dd50c31
commit 1b14c98f81
6 changed files with 149 additions and 0 deletions

BIN
ejercicioPipe Executable file

Binary file not shown.

83
ejercicioPipe.c Normal file
View File

@ -0,0 +1,83 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
//Abuelo-hijo-nieto
void main(void) {
pid_t pid, Hijo_pid, pid2, Hijo2_pid;
int fd1[2];
int fd2[2];
// saludos de pantalla
char saludoAbuelo[]="Saludos del Abuelo al Hijo.\0";
char saludoPadre[]="Saludos del Hijo al Nieto.\0";
char saludoHijo[]="Saludos del Nieto al Hijo.\0";
char saludoNieto[]="Saludos del Hijo al Abuelo.\0";
char buffer[80]="";
pipe (fd1);
pipe (fd2);
pid = fork(); // Abuelo creando al hijo
if (pid ==-1) {
printf("Algo falló ...");
exit(-1);
}
if (pid == 0) { //Soy el hijo
// creamos al nieto
pid2 = fork();
switch (pid2) {
case -1:
printf("Algo falló ...");
exit(-1);
break;
case 0: // Soy el nieto
close(fd2[1]);// cierro fd2 como escritura; y leo de fd2
read(fd2[0], buffer, sizeof(buffer));
printf("\tEl hijo recibe algo del pipe: %s\n",buffer);// Imprimo el mensaje que me mandó Hijo (mi padre)
// Envío un mensaje a Hijo (mi padre) a través de fd1
// con lo que tengo que cerrar fd1 como lectura
close(fd1[0]);
write(fd1[1], saludoHijo, sizeof(saludoHijo));
printf("El hijo envia algo al padre ...\n");
break;
default:
// Soy el hijo el que tiene más faena
// Leo lo que me manda el abuelo por fd1 y lo imprimo por pantalla
read(fd1[0], buffer, sizeof(buffer));
// envio a nieto (mi hijo) por fd2 el mensaje de la variable "saludoPadre"
write(fd2[1], saludoPadre, sizeof(saludoPadre));
// me quedo esperando a que Nieto termine (mi hijo)
Hijo2_pid=wait(NULL);
// Recibo el mensaje de Nieto por fd1, y lo imprimo por pantalla
read(fd1[0], buffer, sizeof(buffer));
printf("\tEl padre recibe el siguiente mensaje del nieto: %s\n",buffer);
// Envío mensaje al abuelo a través de fd2
write(fd2[1], saludoNieto, sizeof(saludoNieto));
}
} else { // Soy el abuelo
printf("Abuelo envia mensaje al hijo ...\n");
// cierra fd1[0] para la lectura el abuelo lee por fd2[0]
close(fd1[0]);
// escribe en fd1 saludoAbuelo, acuérdate de calcular la longitud del string.
write(fd1[1], saludoAbuelo, sizeof(saludoAbuelo));
// EL abuelo se queda esperando a que termine el hijo
Hijo_pid=wait(NULL);
// El abuelo recibe el mensaje por fd2, luego cierra fd2 en modo escritura
read(fd2[0], buffer, sizeof(buffer));
close(fd2[1]);
// Lee de la pipe fd2, entendemos que su hijo le dejó un mensaje antes de terminar.
read(fd2[0],buffer, sizeof(buffer));
printf("\tEl abuelo recibe el siguiente mensaje del hijo: %s\n",buffer);
}
exit(0);
}

BIN
pipe2 Executable file

Binary file not shown.

32
pipe2.c Normal file
View File

@ -0,0 +1,32 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void main(void) {
int fd[2]; // 0 lectura y 1 escritura
char buffer[30];
pid_t pid;
pipe (fd); // creamos el pipe
pid = fork(); // se crea el proceso hijo
switch(pid) {
case -1: // error
printf("Algo falló ...");
exit(-1);
break;
case 0: // soy el hijo
printf("El hijo escribe en el pipe ...\n");
write(fd[1], "Hola papi",10);
break;
default: // soy papi
wait(NULL);// espero a fin del hijo
printf("El papi lee del pipe ...\n");
read(fd[0], buffer, 10);
printf("\tMensaje recibido: %s\n", buffer);
break;
}
}

BIN
pipe3 Executable file

Binary file not shown.

34
pipe3.c Normal file
View File

@ -0,0 +1,34 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
void main(void) {
char saludoPadre[]="Buenos días hijo.\0";
char buffer[30];
int fd[2]; // 0 lectura y 1 escritura
pipe (fd); // creamos el pipe
pid_t pid;
pid = fork(); // se crea el proceso hijo
switch(pid) {
case -1: // error printf("Algo falló ...");
exit(-1);
break;
case 0: // soy el hijo
close(fd[1]); // cierra el descriptor de escritura = entrada
read(fd[0], buffer, sizeof(buffer));
printf("\tEl hijo recibe algo del pipe: %s\n",buffer);
break;
default: // padre envía
close(fd[0]);
write(fd[1],saludoPadre, strlen(saludoPadre)); //escribo en pipe
printf("El padre envía el mensaje al hijo ...\n");
wait(NULL);
break;
}
}