Ejercicio pipe

master
victor 12 months ago
parent 7197dedd9a
commit 2feaf9634d

Binary file not shown.

@ -0,0 +1,28 @@
#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);
}

Binary file not shown.

@ -0,0 +1,29 @@
#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 @@
Saludos peña!!!
Loading…
Cancel
Save

Powered by INFORMATICA.FP.EDU.ES.