Can FIFO file overfow?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Can FIFO file overfow?
# 1  
Old 06-30-2009
Can FIFO file overfow?

I want to output logger information into FIFO file. My application can run for months. My question is what happens to FIFO file when there is one side constantly writing to it and nobody on the other side reading from it?
Will it eventually stuck, overflow, reset, will it take all OS's RAM?

Thanks.
# 2  
Old 06-30-2009
Well, you could devise a test pretty simply.

Create the FIFO w/ mkfifo. Create two processes, a writer and a reader.

Open the FIFO for write, which will block because there's no reader. When it unblocks, write to stderr that it's awake, and then periodically write some bytes to it, and log each write.

Run a reader process, have it sleep a bit, and have it read, say, 10 times, dumping what it reads. Then it should close the fifo and terminate.

What you SHOULD see, according to Stevens APUE, is that the write should cause SIGPIPE to be generated.

Here's an example:
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>

int sigpipe_caught = 0;

#define TESTLOG(s) testlog(__FUNCTION__, s)

void testlog (const char *who, const char *msg)
{
  fprintf (stderr, "%s: %s\n", who, msg);
}

void writer (void)
{
  int f;
  int value = 0;
  int err;
  int done = 0;

  TESTLOG ("starting");
  if ((f = open ("fifo", O_WRONLY)) != -1)
  {
    TESTLOG ("opened fifo");

    while (done <= 0)
    {
      char buf[64];
      err = write (f, &value, sizeof value);

      if (sizeof value == err)
      {
        sprintf (buf, "wrote %d", value);
        TESTLOG (buf);
        value++;
      }
      else
      {
        sprintf (buf, "err = %d", err);
        TESTLOG (buf);
        sprintf (buf, "sigpipe_caught = %d", sigpipe_caught);
        TESTLOG (buf);
        done++;
      }

      TESTLOG ("sleeping");
      sleep (1);
    }

    close (f);
    TESTLOG ("closed fifo");
  }
}

void reader (void)
{
  int f;
  int i;
  int value;
  int err;

  TESTLOG ("sleeping 2");
  sleep (2);
  TESTLOG ("awake");

  if ((f = open ("fifo", O_RDONLY)) != -1)
  {
    TESTLOG ("opened fifo");

    for (i = 0; i < 10; i++)
    {
      char buf[64];

      err = read (f, &value, sizeof value);
      if (sizeof value == err)
      {
        sprintf (buf, "read %d", value);
        TESTLOG (buf);
      }
      else
      {
        sprintf (buf, "err = %d", err);
        TESTLOG (buf);
      }
    }

    TESTLOG ("closing fifo");
    close (f);
  }
  else
  {
    TESTLOG ("failed to open fifo");
  }
}

void sigpipe (int a)
{
  TESTLOG ("caught SIGPIPE");
  sigpipe_caught = 1;
}

int main (void)
{
  signal (SIGPIPE, sigpipe);

  if (fork())
    writer();
  else
    reader();

  return 0;
}

I've run this on a couple of systems, and I'm sure there are issues with it (for example, one shouldn't use fprintf from a signal handler...).

But I think it illustrates the point.
# 3  
Old 06-30-2009
FIFO's/pipes

You should also know about PIPE_BUF, the guaranteed minimum number of bytes that can be written. Implementations vary on this.

There are no partial writes to a pipe when the requested size of the buffer is greater than available space. Instead -1 is returned by write with errno set to EAGAIN.

Finally, for high performance messaging or IPC, pipes/FIFOs are not a great choice.
Because there is no guarantee of atomicity, multiple readers on a single pipe can have issues.

Last edited by jim mcnamara; 06-30-2009 at 11:17 AM.. Reason: spell
# 4  
Old 06-30-2009
According to POSIX.1-2008 write() , errno is set to EPIPE and a SIGPIPE signal is sent to the calling thread.
# 5  
Old 06-30-2009
I was referencing Marc Rochkind 'Advanced UNIX Programming' sec 6.2.2. And POSIX - I assume nonblocking.

Code:
If O_NDELAY or O_NONBLOCK is set, the write returns -1 and
              sets errno to [EAGAIN].

I was not clear.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Mplayer fifo.

Hi all, First of all, sorry for the pastebin link. The code tags aren't working for me. #!/bin/bash # while-menu-dialog: a menu driven - Pastebin.com This is a channel "changer" shell script i'm working on. It uses dialog to display the menus. It... (3 Replies)
Discussion started by: ignatius
3 Replies

2. UNIX and Linux Applications

FIFO

Hello , I am working on unix FIFO IPC. i have a doubt regarding that. If the fifo is updated(write()) through one process....can we able to send any signal that fifo is updated and ready to get read...to other process.?? (0 Replies)
Discussion started by: Harry443
0 Replies

3. Programming

fifo

Dear friends i'm want to implement a program which one file is split into fragments by the server (by some random size) and sent to some processes, so these processes get randomly the fragments of the original file from the server, then the downloader randomly connects to some of these processes... (0 Replies)
Discussion started by: saman_glorious
0 Replies

4. Programming

How to see a FIFO from all the threads?

Hello C programming fellows!!! I'm doing a program with multiple threads in gnu/linux, ubuntu for beeing precise... This program consist in multiple threads, as logical each thread do different things and communicate to each other using IPC. The problem is that in "process 1" I have a... (1 Reply)
Discussion started by: Sandia_man
1 Replies

5. Programming

help About fifo and thread

hi, my problem is i have 10 thread for produces double two dimensional array these will produces 2d array 400 times ,then i have a thread to takes a two dimensional array then it inverse , and most 10 array can be in the fifo , and threads given takes one by one, now my problem is , i cant... (0 Replies)
Discussion started by: bblackened
0 Replies

6. Programming

FIFO's and asynchronousity?

Hello, I have a FIFO which i would like to open O_WDWR | O_NONBLOCK | O_ASYNC and have a SIGIO signal generated when there is input on the FIFO, but there a re a bunch of bugs surrounding this (like for instance i read somewhere that you have to set O_ASYNC with fcntl etc. is this even... (0 Replies)
Discussion started by: davo666
0 Replies

7. UNIX for Dummies Questions & Answers

Doubts on FIFO

Hi , I m beginner for Unix and i want to use FIFO in my 2 Scripts . I want 1 script to read data from FIFO and other will write into FIFO. Despite reading so many articles/posts i am still unable sunchronize my scripts. My doubts are 1> Do We require both scripts as daemons to use... (0 Replies)
Discussion started by: Akshay
0 Replies

8. Programming

how to use fifo

hi, I have a problem. I've done a lil program which gets from the server the given persons username a personal folder. I made it with a pipe calling popen with a command, but how can i make the same thing using fifo. I make the fifo with mkfifo() func. and than what. How do I tell the sertver using... (3 Replies)
Discussion started by: atticus
3 Replies

9. Programming

FIFO issue

hello i am making a client server program that communicates via FIFOS and i cannot get it to not hang i also am forking exec-ing the client from the server my logic (i will spare you the code ) in the server i make two fifos in my server my file permissions are 0644 i then open both... (1 Reply)
Discussion started by: runawayNinja
1 Replies

10. UNIX for Advanced & Expert Users

FIFO over NFS

Hi All L2000-44 HP-UX 11.00 I am attempting to setup a FIFO over NFS. Directing and reading from the FIFO works fine on the local machine - however when I direct to the FIFO from a server which has the directory NFS mounted it does not work. The a process to read from the FIFO on the local... (2 Replies)
Discussion started by: saabir
2 Replies
Login or Register to Ask a Question