bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fifo Problem


From: Chet Ramey
Subject: Re: Fifo Problem
Date: Wed, 3 Jul 2002 09:31:07 -0400

> I am sending this to this address because I couldn't find
> a mailing list and can't get on gnu.bash.bug. Hope I can get
> a reply. I am trying (all within a shell script) to open
> 2 pipes, in/out as follows
> 
>      pipe_out=/tmp/pipe_out.$$ #create the output fifos
>      mkfifo $pipe_out
>      pipe_in=/tmp/pipe_in.$$   #create the input fifos
>      mkfifo $pipe_in
> 
> Then I start an application
> 
> do_app()
> {
>      application <$pipe_in >$pipe_out &
>      .... etc
> }

Now you have two fifos, one with a single reader and one with a single
writer.

> Now I can do something later in the script like
> 
>         echo $command_input >$ftp_pipe_in

You now open the fifo for writing, write something, and close the file
descriptor (this is how redirection works).

I suspect that `application' now exits, because it gets EOF from its
standard input.  When the last writer to a FIFO closes the file
descriptor, subsequent reads return EOF.

> and this works fine. Now what I don't understand and is not
> working is something like:
> 
>            read LINE_IN < $pipe_out
>            echo $LINE_IN
> 
> The application is producing output at a certain rate, so the
> pipe has plenty of data in it. What I thought should happen
> is that the pipe would grow at a certain rate at the end and
> the 'read' would simply remove some data from the beginning.
> If the pipe emptied then I would expect the 'read' to wait. What
> happens is that the first 'read' works then the pipe becomes
> unhappy and dies with:
> 
>            No control connection for command: Broken pipe
> 
> although I know there are other lines in the pipe and as I say
> the first line reads fine.  Any thoughts or ideas?

You need to realize that redirections attached to a particular command
are `one-shot':  the shell closes the appropriate file descriptor when
the command terminates.  What you want to do is to allocate a file
descriptor and retain it so it can be used by multiple commands.

Use something like

        exec 4>$FIFO_IN
        exec 5<$FIFO_OUT

before sending any data to or reading any data from `application'.  Then
when you want to read, use

        read var <&5

and when writing use

        echo xxx >&4

Chet


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)

Chet Ramey, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]