Uplaod Ejercicio 3 Pipe

master
Eric 12 months ago
commit d1866a9827

@ -0,0 +1,6 @@
# Ejercicio 3 Pipe
~~~texto
El padre envía el mensaje al hijo ...
El hijo recibe algo del pipe: Buenos días hijo.
~~~

@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
void main(void) {
char saludoPadre[]="Buenos días hijo.\n";
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;
}
}
Loading…
Cancel
Save

Powered by INFORMATICA.FP.EDU.ES.