[ale] Named Pipe

Christopher Fowler cfowler at outpostsentinel.com
Tue Mar 20 16:50:40 EDT 2007


On Tue, 2007-03-20 at 16:32 -0400, David Corbin 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?

Are you writing this code?

There is a good example of this "Advanced Programming in the UNIX
Environment".  

Here is what you need to do.  You need to have a master process.  The
one feeding data.  He needs to open the pipe in read mode and he needs
to open it in write mode.  You do these operations as non-blocking.
When you get done you can then place both file descriptors back to
blocking mode.

When you want to feed data to the pipe simply write() data to the pipe.
Do nothing with the read file descriptor.


Why do this?  By default when you open a pipe for read or write you will
block until someone else opens it for the opposite operation.  If you
open for write() you'll block waiting on someone else to open on read().
The best thing to do is have your master open up for read and open up
for a write.  Now any other process can open up for a read and not block
open the open() call.  They'll block like normal on the read() call.

The master app with have open'ed in non-block mode so he will not block
in open().  


Here is an example of how I did it in C

        if((pfd = open(PIPE, O_RDONLY)) == -1)
                log_quit("open pipe");

        if((dummyfd = open(PIPE, O_WRONLY)) == -1)
                log_quit("open pipe");


It appears you need to open as RDONLY first then open as WRITE.  In that
code though the master reads the pipe and sends syslog messages.  You
can use strace to see where you are blocking in system calls.

> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale




More information about the Ale mailing list