[ale] Named Pipe

Christopher Fowler cfowler at outpostsentinel.com
Tue Mar 20 17:35:44 EDT 2007


I don't think the data is stored permantely either.  I only use pipes to
feed data to a master process and I do use them to feed data to a client
but I block on open()

Here is a test I cranked out in PERL
#!/usr/bin/perl -w

use Fcntl;
use strict;



my $R= undef;
sysopen $R, "/tmp/fifo", O_RDONLY| O_NONBLOCK;

my $W = undef;
sysopen $W, "/tmp/fifo", O_WRONLY or die "$!\n";

my $x = 1;
while(1) {
  my $data = sprintf "%05d
**************************************************\n", $x++;
  if(syswrite($W, $data, length($data)) <= 0) {
    die "$!\n";
  }

}


If the pipe fills.  Write will block.

If you open write as NONBLOCK to ignore this behavior then write() will
return a -1 with EAGAIN.  Resource is busy simply try again.  It is up
to you what to do then.  Since you don't seem to care about the data I
would simply check for EAGAIN and ignore the error and treat that as a
successful write().  Only question is what happens when a process
connects?  Does it get what was in the buffer then a missing chunk?  

You'll need to create some tests to see what really happens.


I've done that test for you :)

01130 **************************************************
01131 **************************************************
01132 **************************************************
01133 **************************************************
01134 **************************************************
01135 **************************************************
01136 **************************************************
1849435 **************************************************
1849436 **************************************************
1849437 **************************************************
1849438 **************************************************
1849439 **************************************************


On Tue, 2007-03-20 at 17:08 -0400, Brian Pitts wrote:
> Atlanta Linux Enthusiasts wrote:
> > I have an application for which a named pipe seems like the right 
> > answer on
> > several fronts, but what I don't want is for the writing application 
> > to block
> > if there is noone reading it.  I just want one application to stream 
> > data to
> > the pipe, and have some other application be able to jump in 
> > 'mid-stream' and
> > start processing the input data.
> >
> > Is this possible with linux named pipes?
> I don't believe any Unix has a way of storing data in named pipes 
> permanently. Even if the O_NONBLOCK flag is set, an open for write on a 
> named pipe with no reader should return -1. However, you can get around 
> this by having the process doing the writing open it for reading first 
> (also nonblocking of course). While there isn't a reader, your writer 
> will eventually fill the pipe buffer. You'll need to check the return 
> value of write to determine when this happens, and then read PIPE_BUF  
> amount of data to clear it.
> 
> If any of this is wrong, send complaints to km at mathcs.emory.edu ;-)
> 
> I'm assuming that when you say you want an application to be able to 
> jump in "mid-stream", this mean you're okay with discarding data.
> 
> -Brian
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale




More information about the Ale mailing list