FIFO pipes for interprocess communication

Linux specific questions.
Post Reply
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

FIFO pipes for interprocess communication

Post by sean_vn »

I've been experimenting with linux FIFO pipes and it's definitely a bit tricky and kinda non-robust. Anyway I've gotten this far:

Code: Select all

#include "file.bi"
#include "crt/stdio.bi"

const as string fifoname="/root/tempfifo"
if not fileexists(fifoname) then
	shell("mkfifo "+fifoname)
end if

var file=fopen(fifoname,"w")
for i as ulong=0 to 999
	fprintf(file,"Hello:"+str(i)+chr(11)+chr(13))
	fflush(file)
	print "done",i
	sleep 50
next
fclose(file)
And:

Code: Select all

#include "file.bi"
#include "crt/stdio.bi"

const as string fifoname="/root/tempfifo"
if not fileexists(fifoname) then
	shell("mkfifo "+fifoname)
end if

var file=fopen(fifoname,"r")
dim as long c
while c>=0
  c=fgetc(file)
  print chr(c);
wend
fclose(file)
You run the two pieces of code in different shells.
You may have to delete the tempfifo file if you break anything, as it will hold on to broken state and make trouble for you.
I'll experiment a bit more and see if it can be made robust enough for general use.
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

Re: FIFO pipes for interprocess communication

Post by sean_vn »

It does seem that a fifo is non-reusable. If true then you have temporal creation/open order issues to deal with, which are not fun. I guess you would need some piece of code sitting in the middle to delete an existing fifo, make a new one, then open the writer process, then open the reader process. If you want to have 2 way communication between 2 processes then I'm not sure how to figure out that mess.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FIFO pipes for interprocess communication

Post by caseih »

What do you mean it's not reusable? I must not be understanding what you mean as I can create a fifo and use it any number of times in separate sessions. For example, cat < /path/to/fifo in one terminal, cat > /path/to/fifo in another. Once the sending process sends an EOF and the receiver shuts down automatically. Then the fifo can be opened again.

As for robustness, fifos are as robust as any normal file. I don't see how a fifo can be left in a weird state. Once the two end points are closed down, the OS resets it automatically. Of course FIFOs are not two-way communication. If you need two-way communication, the unix socket is what you need (which looks a lot like a fifo).
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

Re: FIFO pipes for interprocess communication

Post by sean_vn »

Cool, thanks for the further information. This is what I had come up with this morning minus what you have indicated.

A middle-man which you start first:

Code: Select all

#include "crt/stdio.bi"

const as string fifoA="/root/fifoA"

shell("rm "+fifoA)
shell("mkfifo "+fifoA)
fopen(fifoA,"+")
print "launched"
getkey


Then you can start these 2 processes in any order:

Code: Select all

#include "crt/stdio.bi"

const as string fifoname="/root/fifoA"

var file=fopen(fifoname,"w")
for i as ulong=0 to 999
	fprintf(file,"Hello:"+str(i)+chr(11)+chr(13))
	fflush(file)
	print "done",i
	sleep 50
next
fclose(file)

Code: Select all

#include "crt/stdio.bi"

const as string fifoname="/root/fifoA"

var file=fopen(fifoname,"r")
dim as long c=fgetc(file)
while c>=0
  print chr(c);
  c=fgetc(file)
wend
fclose(file)

sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

Re: FIFO pipes for interprocess communication

Post by sean_vn »

The idea is to allow programming language mash-ups through simple file system message exchanges. Most programming languages allow interaction with the file system, I not sure access to unix sockets is always as available. However experts on such matters can correct me.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FIFO pipes for interprocess communication

Post by caseih »

Yup Unix has been doing such "mashups" for decades using fifo pipes (standard in, standard out, standard error), plumbed together using the shell! :) So you're on the right track.
sean_vn
Posts: 283
Joined: Aug 06, 2012 8:26

Re: FIFO pipes for interprocess communication

Post by sean_vn »

Dude!
jdmcbride
Posts: 28
Joined: Aug 06, 2016 16:13

Re: FIFO pipes for interprocess communication

Post by jdmcbride »

Thank you,

I was looking for an IPC solution. This may be what Im looking for.

Jerry
Post Reply