Thread: working with FIFO files

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    15

    working with FIFO files

    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.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    15
    Another thing: if I don't wanna get blocked when I do a 'read' I must use the option O_NONBLOCK when opening the FIFO, right? What if sometimes I want it to get blocked and sometimes I don't (in different calls)? Is there any option to specify this behaviour when doing a 'read' instead of a 'open'? Thanks.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    15
    And this...

    Code:
    //open FIFOSERVE
    if((fd_fifoserve = open(FIFOSERVE, O_RDWR)) < 0){
        perror("error al abrir el FIFO de entrada al servidor (open)");
        exit(-1);
    }
    //open FIFOSERVS
    if((fd_fifoservs = open(FIFOSERVS, O_WRONLY)) < 0){
        perror("error al abrir el FIFO de salida del servidor (open)");
        exit(-1);
    }
    ... gives me problems too. I have no problem with the first 'open', but then the second one gets blocked (waiting for someone to open FIFOSERVS for reading, I assume). I thought I could open for "writing only" without getting blocked! Then, if I replace open(FIFOSERVS, O_WRONLY) with open(FIFOSERVS, O_WRONLY | O_NONBLOCK) I get an error saying "no such device of address" (referring to FIFOSERVS). Then again, it all works if I replace open(FIFOSERVS, O_WRONLY | O_NONBLOCK) with open(FIFOSERVS, O_RDWR), but that's not what I wanna do! Does anybody know why I'm getting this errors? Any idea to fix it? Thanks.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use a bitwise OR (|) to combine the flags to open?
    [edit] You might have to choose different flags (or use fopen with "r+"). See the man/google page. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    15
    Use a bitwise OR (|) to combine the flags to open?
    [edit] You might have to choose different flags (or use fopen with "r+"). See the man/google page. [/edit]
    That's what I'm doing. I'm using a bitwise OR here: open(FIFOSERVS, O_WRONLY | O_NONBLOCK), that is, at the only place where I'm using more than 1 option.

    And, believe me, I've consulted the man page (and other man pages related), I've googled it... I just don't know what I'm doing wrong. Thanks anyway.

    P.T. And what about my other doubts?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is there any option to specify this behaviour when doing a 'read' instead of a 'open'?
    I don't think you can change the attributes of a stream once it has been opened (besides character- to byte-oriented). I don't really know.

    P.S. Your English is very good.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    15
    I don't think you can change the attributes of a stream once it has been opened (besides character- to byte-oriented). I don't really know.
    Ok, thanks, anyway.

    P.S. Your English is very good.
    Thanks a lot! I really appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. Working with multiple source files
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 06-03-2008, 11:23 AM
  3. Working with DLL files...
    By Devil Panther in forum Windows Programming
    Replies: 8
    Last Post: 11-15-2004, 12:42 AM
  4. working with resource files
    By the Wookie in forum Windows Programming
    Replies: 4
    Last Post: 02-01-2003, 10:26 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM