Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-13-2012, 09:28 AM   #1
Registered: Jul 2009
Location: Montaletto
Distribution: Debian GNU/Linux
Posts: 107

Rep: Reputation: 5
Bash: < & > order is relevant; but why unrelated i/o misordering hangs my pipes?

I've built a FIFOed interface toward gimp-console in the form of a couple of public I/O FIFOs connected to an interpreting bash script which translates incoming commands into dispatches understandable by gimp, and sends them to it using another couple of I/O private FIFOs.
I/O publicly       +-------------------------+  I/O private FIFOs  +-------+
accessible FIFOs   |         INTERFACE       |  toward Gimp        |       |
------------------>|                         |-------------------->| GIMP  |
<------------------| translator / dispatcher |<--------------------|       |
                   +-------------------------+                     +-------+
Since design, the interface's end side of the two private FIFOs toward gimp have to always be kept open, in the sense that a generic sub-process has to always be fictitiously connected with the two pipes, to prevent hangs of read and write operations performed by gimp (explanation of this behavior is under man 7 pipe).

I succeeded in doing this only in the form of:
dummy_lasting_process >input_to_gimp <output_from_gimp &
gimp-console --parameters <input_to_gimp >output_from_gimp &
I discovered that if the order of redirection operators in the dummy process is inverted, as in
dummy_lasting_process <output_from_gimp >input_to_gimp &
gimp-console --parameters <input_to_gimp >output_from_gimp &
both the dummy process and gimp hang up, suggesting that the ending of one or both of the pipes is open.
I see no reason for this.

Last edited by romagnolo; 02-13-2012 at 04:40 PM. Reason: added chart for clarity
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-13-2012, 01:12 PM   #2
Registered: Jul 2009
Location: Montaletto
Distribution: Debian GNU/Linux
Posts: 107

Original Poster
Rep: Reputation: 5
More generally, I found these hangs happening with whatever other couple of processes. For example:
# preliminar
mkdir testdir
cd testdir
mkfifo A B
this completes execution:
# example 1
/bin/sleep 100 >A <B &
ls <A >B
this hangs up, probably waiting for pipes clearance:
# example 2
/bin/sleep 100 <B >A &
ls <A >B
Old 02-13-2012, 07:07 PM   #3
LQ Newbie
Registered: Nov 2009
Posts: 17

Rep: Reputation: 6
Two things come into play here, first, the shell processes
redirection from left to right.

Second, the shell would be opening these FIFO's in
"blocking mode". If nothing is attached to the other end,
the open will not complete until the other end is opened.

In your first case, sleep starts to open A and blocks.
It is in the background so ls also runs completing the
opening of the other end of A.

In the second case sleep blocks on opening B and hasn't
yet tried to open A. Then ls opens A and blocks because
the other end of A is not, and never will be opened by
sleep which is blocked trying to open B which ls has
not opened and can't because it is blocked.

Reversing the redirections on ls in the second case
should also work.

I've never tried it, but opening both ends but only
using one might work too.

sleep 100 <> B > A &
ls < A > B

By opening B for both reading and writing it might
not block and thus would open A. I don't know if
it would be important to close the read end of B
to make sure only one process had it open, but if
so this might also work, again untried.

sleep 100 <> B >&- > A &
2 members found this post helpful.
Old 02-14-2012, 10:29 AM   #4
Registered: Jul 2009
Location: Montaletto
Distribution: Debian GNU/Linux
Posts: 107

Original Poster
Rep: Reputation: 5
Thank you for the precious insight. I will study your examples as time permits.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: rearrange lines in a file by line number & print in that order SilversleevesX Linux - Newbie 7 09-20-2011 08:31 PM
LXer: Using Pipes in the Bash Shell LXer Syndicated Linux News 1 01-09-2011 01:40 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
LXer: Using Named Pipes (FIFOs) with Bash LXer Syndicated Linux News 0 03-28-2009 05:42 AM
pipes and bash scripts twistedpair Linux - General 0 06-03-2004 10:59 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration