The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Doubts on FIFO Akshay UNIX for Dummies Questions & Answers 0 06-06-2008 07:56 AM
how to use fifo atticus High Level Programming 3 06-05-2006 10:15 AM
FIFO issue runawayNinja High Level Programming 1 04-29-2004 03:10 PM
FIFO over NFS saabir UNIX for Advanced & Expert Users 2 08-06-2003 08:03 AM
Pipe & fifo.... M3xican High Level Programming 4 07-20-2002 07:22 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-30-2009
jackhab jackhab is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 1
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 (permalink)  
Old 06-30-2009
mgessner mgessner is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 50
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 (permalink)  
Old 06-30-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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 10:17 AM.. Reason: spell
  #4 (permalink)  
Old 06-30-2009
fpmurphy's Avatar
fpmurphy fpmurphy is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
According to POSIX.1-2008 write() , errno is set to EPIPE and a SIGPIPE signal is sent to the calling thread.
  #5 (permalink)  
Old 06-30-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0