Re: preventing pipe reader from existing on writer exiting

2009-10-01 Thread Brian J. Murrell
On Wed, 2009-09-30 at 23:13 +0200, Andreas Schwab wrote:
 
 Just make sure the write side of the pipe is not closed prematurely.

Hrm.  Yes, of course.  John's solution of having a null writer keeping
it open is one way -- which I might just use.

 $ (n=0; while [ $n -lt 10 ]; do cat /dev/zero; let n=$n+1; done)  /tmp/fifo 

But this is where (simplified) my example using cat went sideways.  :-(
In my real world use, the first cat is actually mplayer and doesn't have
the option of writing to stdout instead of a named file for this
particular use of it.

So other than John's solution, I suppose I could still follow your
advice of not having the fifo write close prematurely and insulate the
writer from the fifo.  I can't think of another way other than using two
fifos with a cat between them.  Can anyone else?

FWIW, the actual process which the second cat is standing in for is
oggenc and it can take stdin, if that makes it any easier.  So to recap:

mplayer ... /tmp/fifo 
oggenc ... /tmp/fifo

The mplayer may terminate prematurely and need to be restarted, ideally
without oggenc knowing this.

Cheers and much thanx!

b.



signature.asc
Description: This is a digitally signed message part


Re: preventing pipe reader from existing on writer exiting

2009-10-01 Thread Andreas Schwab
Brian J. Murrell br...@interlinx.bc.ca writes:

 But this is where (simplified) my example using cat went sideways.  :-(
 In my real world use, the first cat is actually mplayer and doesn't have
 the option of writing to stdout instead of a named file for this
 particular use of it.

Perhaps you can use /dev/stdout instead.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
And now for something completely different.




preventing pipe reader from existing on writer exiting

2009-09-30 Thread Brian J. Murrell
Let's say I have the following (contrived, simplified example):

$ mknod /tmp/fifo
$ cat /dev/zero  /tmp/fifo 
$ cat  /tmp/fifo

When the first cat exits (i.e. is terminated) the second cat stops.  The
problem is that I want to be able to restart the first cat and have the
second cat just keep reading such as:

$ mknod /tmp/fifo
$ (n=; while [ $n -lt 10 ]; do cat /dev/zero  /tmp/fifo; let n=$n+1; done) 
$ pid=$!
$ cat  /tmp/fifo
$ kill $pid

Where $n is a safety valve against an endless loop of cat just exiting
over and over again.

But of course that doesn't work because the second cat exits when the
first iteration of the first subshell exits.  Additionally, the kill
only kills the subshell and not any of it's children.  So to solve the
second cat exiting issue I considered:

$ mknod /tmp/fifo
$ (n=; while [ $n -lt 10 ]; do cat /dev/zero  /tmp/fifo; let n=$n+1; done) 
$ pid=$!
$ (n=; while [ $n -lt 10 ]; do cat  /tmp/fifo; let n=$n+1; done)
$ kill $pid

This has the desired effect except for the issue of not being able to
kill $pid's children.

So now I figure I must just be going about this all the wrong way.

Can anyone help?  Ultimately I need to do I/O through a named pipe and I
need to be able to restart the writer without restarting the reader.

Cheers,
b.





signature.asc
Description: This is a digitally signed message part


Re: preventing pipe reader from existing on writer exiting

2009-09-30 Thread Andreas Schwab
Brian J. Murrell br...@interlinx.bc.ca writes:

 Can anyone help?  Ultimately I need to do I/O through a named pipe and I
 need to be able to restart the writer without restarting the reader.

Just make sure the write side of the pipe is not closed prematurely.

$ (n=0; while [ $n -lt 10 ]; do cat /dev/zero; let n=$n+1; done)  /tmp/fifo 

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
And now for something completely different.




Re: preventing pipe reader from existing on writer exiting

2009-09-30 Thread John Reiser

Ultimately I need to do I/O through a named pipe and I
need to be able to restart the writer without restarting the reader.


The reader of a fifo will not be terminated as long as there is
at least one writer to the fifo.  Therefore, create a second writer.
For example, to hold the fifo open for one hour:

 sleep 3600   /tmp/fifo  

The shell forks, then opens /tmp/fifo for writing.  The open() waits
until there is a reader.  Then the forked shell execs /bin/sleep,
which waits for 3600 seconds before exiting.  During that 3600
seconds the fifo is open for writing, so the system will not
terminate any reader of the fifo for at least that long.

--