Page 1 of 1

Named PIPEs

Posted: Wed Dec 20, 2017 11:14 am
by Martin_Head
Is there a way to test if a named PIPE in SMSQ/E is empty? EOF does not seem to work.

Also how do you empty a PIPE, or ensure it is empty, If there is nothing in it you want. Closing and re opening does not seem to empty it. The only idea I have had is to run a loop reading it.

If #4 is the open pipe

REP loop
a$=INKEY$(#4)
IF a$="" THEN EXIT loop
END REP loop

Re: Named PIPEs

Posted: Wed Dec 20, 2017 12:40 pm
by tofro
Martin,

EOF or EOFW on a pipe actually indicate EOF only when the other end of the pipe has been closed.

In unexpanded S*Basic, using INKEY$ in a loop (albeit with a small timeout) seems to be the only way to do what you want. (I'm assuming you want a non-blocking read).

Tiny toolkit has a function PEND that exactly does what you want - returning 1 if there's nothing to read or 0 if there is something (without blocking).

DIY toolkit issue "P" has a function Q_COUNT% that returns the number of bytes available in a pipe

Pipes are one of the device types that could actually benefit a lot from a "read from channel in a buffer as much as is there until buffer full and return #of bytes read" - Function like fread() in C, which is unfortunately not implemented in S*BASIC. This would be a direct call to IO.FBYTES

With regards to emptying a pipe I see no other way than to do INKEY$ in a loop.

Tobias

Re: Named PIPEs

Posted: Thu Dec 21, 2017 10:30 am
by Martin_Head
Thanks for that.

I am running a loop, where I need the pipe to be empty at the start of each loop. So I think I will just do the small INKEY$ loop at the start of each main loop.

Re: Named PIPEs

Posted: Thu Dec 21, 2017 11:19 am
by RWAP
I just used to pass a string to signify the end of data - eg *!PIPE_END!*