Ejercicios Pipe 1- 4
This commit is contained in:
parent
5be6cfe57a
commit
2dc62bb9c4
|
@ -0,0 +1,33 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include<fcntl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
char saludo[] = "Saludos peña!!!\n";
|
||||||
|
char buffer[10];
|
||||||
|
int fd, bytesleidos;
|
||||||
|
|
||||||
|
fd = open("texto.txt",1); // abrimos para escritura
|
||||||
|
if (fd==-1) {
|
||||||
|
printf("Algo salió mal\n");
|
||||||
|
exit (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Escribo el saludo en el fichero...");
|
||||||
|
write(fd,saludo, strlen(saludo));
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
fd=open("texto.txt",0); // abrimos para lectura
|
||||||
|
printf("Contenido del Fichero: \n");
|
||||||
|
|
||||||
|
bytesleidos = read(fd,buffer,1);
|
||||||
|
while (bytesleidos!=0) {
|
||||||
|
printf("%s", buffer);
|
||||||
|
bytesleidos = read(fd,buffer,1);
|
||||||
|
}
|
||||||
|
close (fd);
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,105 @@
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
// Abuelo -> Hijo -> Nieto (comunicación con pipes)
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
pid_t pid, Hijo_pid, pid2, Hijo2_pid;
|
||||||
|
int fd1[2]; // Pipe entre abuelo e hijo
|
||||||
|
int fd2[2]; // Pipe entre hijo y nieto
|
||||||
|
|
||||||
|
// Mensajes entre procesos
|
||||||
|
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] = "";
|
||||||
|
|
||||||
|
// Crear las pipes
|
||||||
|
pipe(fd1); // Pipe entre abuelo e hijo
|
||||||
|
pipe(fd2); // Pipe entre hijo y nieto
|
||||||
|
|
||||||
|
pid = fork(); // Abuelo creando al hijo
|
||||||
|
if (pid == -1) {
|
||||||
|
perror("Error al crear al hijo");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid == 0) { // Soy el hijo
|
||||||
|
close(fd1[1]); // El hijo no escribe en fd1, solo lee del abuelo
|
||||||
|
close(fd2[0]); // El hijo no lee en fd2, solo escribe al nieto
|
||||||
|
|
||||||
|
pid2 = fork(); // Hijo creando al nieto
|
||||||
|
if (pid2 == -1) {
|
||||||
|
perror("Error al crear al nieto");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid2 == 0) { // Soy el nieto
|
||||||
|
// El nieto cierra extremos no utilizados
|
||||||
|
close(fd2[1]); // El nieto no escribe en fd2, solo lee del hijo
|
||||||
|
close(fd1[0]); // El nieto no lee ni escribe en fd1
|
||||||
|
close(fd1[1]);
|
||||||
|
|
||||||
|
// Leer mensaje del padre (hijo)
|
||||||
|
read(fd2[0], buffer, sizeof(buffer));
|
||||||
|
printf("Nieto recibe el mensaje del hijo: %s\n", buffer);
|
||||||
|
|
||||||
|
// Responder al hijo
|
||||||
|
close(fd2[0]); // Cierra lectura de fd2
|
||||||
|
write(fd1[1], saludoHijo, strlen(saludoHijo) + 1); // Escribe al hijo por fd1
|
||||||
|
close(fd1[1]); // Cierra escritura de fd1
|
||||||
|
|
||||||
|
exit(0); // Termina el nieto
|
||||||
|
} else {
|
||||||
|
// Soy el hijo
|
||||||
|
// Leer el mensaje del abuelo
|
||||||
|
read(fd1[0], buffer, sizeof(buffer));
|
||||||
|
printf("Hijo recibe el mensaje del abuelo: %s\n", buffer);
|
||||||
|
|
||||||
|
// Enviar mensaje al nieto
|
||||||
|
write(fd2[1], saludoPadre, strlen(saludoPadre) + 1);
|
||||||
|
close(fd2[1]); // Cierra escritura al nieto
|
||||||
|
|
||||||
|
// Esperar a que el nieto termine
|
||||||
|
Hijo2_pid = wait(NULL);
|
||||||
|
|
||||||
|
// Leer la respuesta del nieto
|
||||||
|
read(fd1[1], buffer, sizeof(buffer));
|
||||||
|
printf("Hijo recibe respuesta del nieto: %s\n", buffer);
|
||||||
|
|
||||||
|
// Responder al abuelo
|
||||||
|
close(fd1[0]); // Cierra lectura de fd1
|
||||||
|
write(fd1[1], saludoNieto, strlen(saludoNieto) + 1); // Escribe al abuelo por fd1
|
||||||
|
close(fd1[1]); // Cierra escritura de fd1
|
||||||
|
|
||||||
|
exit(0); // Termina el hijo
|
||||||
|
}
|
||||||
|
} else { // Soy el abuelo
|
||||||
|
// Cerrar extremos no utilizados
|
||||||
|
close(fd1[0]); // El abuelo no lee de fd1, solo escribe al hijo
|
||||||
|
close(fd2[0]); // El abuelo no lee ni escribe en fd2
|
||||||
|
close(fd2[1]);
|
||||||
|
|
||||||
|
// Enviar mensaje al hijo
|
||||||
|
printf("Abuelo envía mensaje al hijo...\n");
|
||||||
|
write(fd1[1], saludoAbuelo, strlen(saludoAbuelo) + 1);
|
||||||
|
|
||||||
|
// Esperar a que el hijo termine
|
||||||
|
Hijo_pid = wait(NULL);
|
||||||
|
|
||||||
|
// Leer la respuesta del hijo
|
||||||
|
read(fd1[0], buffer, sizeof(buffer));
|
||||||
|
printf("El abuelo recibe el siguiente mensaje del hijo: %s\n", buffer);
|
||||||
|
|
||||||
|
// Cerrar el descriptor
|
||||||
|
close(fd1[1]);
|
||||||
|
|
||||||
|
exit(0); // Termina el abuelo
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue