Coprocesses are tricky; it's easy to get into a deadlock if you just
read and write with one thread, without multiplexing the I/O using
poll or select.
> descriptor works fine (the output goes to the printer queue) but any attempt to
> read the response from CUPS from the file descriptor simply hangs. eg:
>
> if ((pfile = popen("lp","r+")))
> {
> ... write data to printer ...
Did you fflush(pfile) here? Maybe all the data didn't go.
I think popen returns streams that are in fully buffered mode,
so output is only flushed when a buffer fills, the stream is
closed or fflush is called.
You would think that a simple half-duplex situation like this would
avoid deadlock, since lp should read your entire request until the end,
and only then produce a response.
However, if some trailing part of your request is sitting in a buffer,
such that lp is waiting for that last bit before replying, then you have
a deadlock.
(Note that ISO C itself requires a flush operation when switching from
output to input on a bidirectional stream, as a matter of well-defined
behavior. When switching from input to output, a file positioning
operation is required (regardless of whether the stream supports
positioning; seeking zero bytes from the current position with
fseek should do the trick). I think the main reason for this is that
some library implementations share one buffer between input
and output.)
> fgets(str,sizeof(str),pfile); <- hangs here
> }
--
TXR Programming Language:
http://nongnu.org/txr
Cygnal: Cygwin Native Application Library:
http://kylheku.com/cygnal
Mastodon: @
Kazi...@mstdn.ca