I'm in trouble using named pipes (i.e. created with mkfifo
) and file descriptors to redirect outputs and grab inputs to/from them within ImageMagick (convert
).
Basically, with the following bash
code (well, an easier equivalent to what I'm trying) the script hangs and won't show the image:
#!/bin/bash
mkfifo myPipe # Create a named pipe
exec 3<>myPipe # I/O file descriptor using the pipe
convert rose: -resize 640x480\! png:fd:3 # Write the image to the pipe
convert png:fd:3 win: # To read from the pipe using the file descriptor
exec 3>&- # Close the file descriptor
rm myPipe # Close and remove the pipe
Making some research, I saw here this paragraph (it's from Jonathan Leffler's answer):
When a FIFO is opened for reading, it blocks the calling process (normally). When a process opens the FIFO for writing, then the reader is unblocked. When the writer closes the FIFO, the reading process gets EOF (0 bytes to read), and there is nothing further that can be done except close the FIFO and reopen.
To avoid that, I tried to use Mark Edgar's solution consisting of these instructions (code taken from the answer):
# Create pipe and start reader.
mkfifo pipe
cat pipe &
# Open pipe for writing.
exec 3>pipe
echo one >&3
echo two >&3
# Close pipe.
exec 3>&-
Unfortunately, I need to read from the pipe and write to it with file descriptors (i.e. fd:X
using convert
). How can I accomplish that without the hanging problems?
&
afterconvert rose: -resize 640x480\! png:fd:3
convert rose: -resize 640x480\! png:fd:1 | convert png:fd:0 win:
and completely get rid of the named pipe?rm
. If you want to split it, I say use normal pipes and simply spread the commands across multiple lines. Anyway, I'm glad it worked.