Hi everybody! I'm new at working with FIFO files and so I'm a bit confused about some things:
- First, the manual says a FIFO must be opened on both ends in order to write in (or read from) it. Anyway, later on, you can read this:
Under Linux, opening a FIFO for read and write will succeed both in blocking and non-blocking mode. POSIX leaves this behaviour undefined. This can be used to open a FIFO for writing while there are no readers available. A process that uses both ends of the connection in order to communicate with itself should be very careful to avoid deadlocks.
English is not my first language and sometimes I tend to missunderstand the simplest things (although I'm not so bad at English), specially if they're computer-science-related. So, let's clear this bit up: "This can be used to open a FIFO for writing while there are no readers available". Does it means you don't need to have it opened for reading? I mean, if there are "no readers avaible" it means nobody has opened it for reading, right? So under Linux is not necessary to open a FIFO for reading in order to write in it (or so I think). Is the opposite true? I mean, can you read from a FIFO even if nobody has it opened for writing in this moment (they've written before, but now it's closed)?
- Second, I've seen several examples on the net in which they open a FIFO, then inside a while they keep writing in (or reading from) it, then finally they close it. That's fine by me. What puzzles me is that in some notes my professor gave me it goes like this:
Code:
//consumidorFIFO.c
//Consumidor que usa mecanismo de comunicación FIFO.
//Ejecutar el programa: $> consumidorFIFO & (en background)
//Después ejecutar el programa productorFIFO
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#define ARCHIVO_FIFO "ComunicacionFIFO"
int main(void)
{
int fd;
char buffer[80]; // Almacenamiento del mensaje del cliente.
int leidos;
//Creamos la tubería FIFO si no existe
umask(0);
mknod(ARCHIVO_FIFO,S_IFIFO|0666,0);
//también vale: mkfifo(ARCHIVO_FIFO,0666);
while(1) {
if ( (fd= open(ARCHIVO_FIFO,O_RDONLY)) <0) {
perror("open");
exit(-1);
}
leidos=read(fd,buffer,80);
printf("\nMensaje recibido: %s\n", buffer);
close(fd);
}
return 0;
}
Code:
//productorFIFO.c
//Productor que usa mecanismo de comunicación FIFO.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#define ARCHIVO_FIFO "ComunicacionFIFO"
int main(int argc, char *argv[])
{
int fd;
//Comprobación de uso correcto del programa.
if(argc != 2) {
printf("\nproductorFIFO: faltan argumentos (mensaje)");
printf("\nPruebe 'productorFIFO <mensaje>' donde <mensaje> es una cadena de
caracteres.\n");
exit(-1);
}
//Intentar abrir para escritura el cauce FIFO.
if ( (fd= open(ARCHIVO_FIFO,"O_WRONLY")) <0) {
perror("\nError en open");
exit(-1);
}
//Escribir en la tubería FIFO el mensaje introducido como argumento.
if( (write(fd,argv[1],strlen(argv[1])) != strlen(argv[1])) {
perror("\nError al escribir en el FIFO");
exit(-1);
}
close(fd);
return 0;
}
To sum up, they open it and close it all the time! Why is this? Any idea? Thanks a lot.