Re: Using pipes example

Elko Tchernev <[email protected]> Sat, 12 Jul 2025 15:33:59 +0300
Newsgroups gmane.comp.lang.forth.gforth
Message-ID <[email protected]>
Thanks for all the answers.

Indeed it was good that I asked and you updated the documentation, 
because it was not clear that the function was launching a process 
rather than opening a named pipe - which is what I was expecting.

Maybe it is a good idea to add the named pipe functionality with a 
dedicated create-pipe word, because using the mkfifo shell command works 
indeed, but is not entirely straightforward to use.

The way I made it work somewhat satisfactory was by detaching the launch 
by using
s" mkfifo /tmp/pipename&" system
and then waiting for the pipe to be created in a begin...until cycle 
before using it with open-file.

A dedicated create-pipe would take care of that and return a proper 
result code. Or it could be a pipe (fam -- fam) access method for the 
files wordset.

A second caveat is that I did not see a way to send  O_NONBLOCK to 
open-file, so I had to carefully arrange the order of openings to avoid 
deadlocks. It would be nice to have a nonblock fam added.


Thanks again,
Elko




On 10/07/2025 10.34, Anton Ertl wrote:
> On Tue, Jul 08, 2025 at 01:59:18PM +0300, Elko Tchernev wrote:
>> Do pipes in gforth work at all? (Not Gforth in pipes, that works.) 
> I have now documented OPEN-PIPE: 'open-pipe' ( c-addr u wfam - wfileid 
> wior ) gforth-0.2 "open-pipe" c-addr u is the name/path of an OS-level 
> program. If wfam is 'r/o', the standard output of the program is piped 
> into the Gforth process and can be read from wfileid. If wfam is 
> 'w/o', data written to wfileid is piped as standard input into the 
> program. wior is 0 if and only if opening the pipe succeeded.
>> I'd appreciate a single working example where two gforth instances 
>> open a pipe and communicate through it 
> That would start with something like s" gforth 
> <command-line-parameters>" r/o open-pipe throw for reading the 
> standard output of the child gforth or using w/o for writing to the 
> standard input of the child gforth. Let's do a simpler example: s" cat 
> -n" w/o open-pipe throw constant catpipe ' words catpipe 
> outfile-execute catpipe close-pipe throw .
>> s" mkfifo /tmp/trrr" system  ok 
> This would be a good start for communicating between two pre-existing 
> instances of Gforth. You would then open /tmp/trrr with open-file on 
> both sides, however. Finally, unix/pthread.fs contains a word 
> create_pipe that is intended for creating a pipe to be used between 
> threads in a single gforth process (although you could then also fork 
> and have two separate gforth processes that communicate through the 
> pipe). For now this is only an internal word for our multitasker, so 
> you have to study the source to make use of it. - anton