pipes and blocking read

Started by starseed, August 02, 2006, 03:10:47 PM

Previous topic - Next topic

starseed

When using pipes, read is blocking.

Is there a way to check for available data, before trying to read?

From the help file, peek seems not to work, and when I try

(peek ((pipe) 0))

I get "invalid function (peek ((pipe) 0))

Lutz

#1
'peek' is not available on Win32 only on UNIX like OSs.



Lutz

starseed

#2
Right, there it is, "only available for Unix like systems"  ...



And how about the real question behind it? How can I work with pipes without blocking?

Lutz

#3
It very much depends what kind of application your are building.



Normally one part is the controlling part, i.e. in an GUI application the GUI could be sending the request and the other process could wait in a blocking loop for commands. This program is a good example for this:



http://newlisp.org/index.cgi?page=Tk_and_newLISP">http://newlisp.org/index.cgi?page=Tk_and_newLISP



newLISP starts Tcl/Tk then waits for input generated when the mouse is moved a button is pressed etc.



If this doesn't work for you than use other channels of communications, like or UDP Tcp/Ip together with 'select' to inquire the status of the channel.



Another possibility is, to let the controlling application send some kind of heart beat which keeps the waiting loop on the other side moving and with the abbiity to do other work while waiting. The receiving pipe unblocks on line-feeds. Running of a timer the other application could send a line-feed every 50 milli seconds. The loop on the receiving end could run like a state machine.



You also could use threads (not on Win32) or child processes (also on Win32) communicating via shared memory and synchronize using semaphores.



Lutz