![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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. |
|
||||
|
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;
}
But I think it illustrates the point. |
|
||||
|
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 |
|
|||||
|
According to POSIX.1-2008 write() , errno is set to EPIPE and a SIGPIPE signal is sent to the calling thread.
|
|
||||
|
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].
|
| Sponsored Links | ||
|
|