// FIFOs "Named pipe"
/*
1.- Qué es
Es un canal de intercomunicación entre procesos (IPC)
2.- "Named pipe" --> representación en el FileSystem
3.- Ejemplo Reader / Writer con:
int mkfifo(const char *pathname, mode_t mode);
int open(const char *pathname, int flags);
ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf. size_t count);
int close(int fd);
Debe estar abierto simultáneamente en los dos extremos, lectura y escritura, para funcionar.
open open
O_WRONLY O_RDONLY
--------------------------------
Writer/TX --> --> Reader/RX
--------------------------------
*/
/*
// PROCESO TRANSFERENCIA
#include <sys/stat.h> // para el FIFO
#include <unistd.h> // para fork
#include <fcntl.h> // para archivos
#include <stdio.h>
int main(void)
{
int fd;
char buf[] = "mensaje 1 ...";
mkfifo("Mi_Fifo", 0666); // modo octal
fd = open("Mi_Fifo", O_WRONLY);
write(fd, buf, sizeof(buf));
close(fd);
return 0;
}
*/
// PROCESO LECTOR
#include <sys/stat.h> // para el FIFO
#include <unistd.h> // para archivos
#include <fcntl.h> // para archivos
#include <stdio.h>
int main(void)
{
int fd,n;
char buf[1024];
fd = open("Mi_Fifo", O_RDONLY);
n= read(fd, buf, sizeof(buf));
printf("bytes leídos: %d \n", n);
printf("mensaje leído: %s \n", buf);
close(fd);
return 0;
}