#1003 Named pipes on Linux

tactics Thu 4 Mar 2010

I want to do some light IPC stuff in Fantom. I have a wisp process running in the background on a Linux box and I want to be able to give it commands from the shell (stop, restart, etc).

I figured I could use Linux's named pipes to do this:

class PipeReader
{
  Void main()
  {
    file := File(`foo`)
    in := file.in(0)
    echo(in.readLine() + "\n")
    in.close
  }
}

But it doesn't quite work:

$ mkfifo foo
$ fan PipeReader.fan &
[1] 29539
$ echo "Hello world" > foo
$

If I echo again, I get:

$ echo "Hello world" > foo
HH

Eh? Something is going on low level that I don't understand. I wrote an equivalent in C:

#include <stdio.h>

int main()
{
  FILE* f = fopen("foo", "rb");
  char string[256];
  fgets(string, 256, f);
  printf(string);
  fclose(f);
  return 0;
}

And this works as expected:

$ ./read &
[1] 29563
$ echo "Hello world" > foo
Hello world
[1]+  Done                    ./read

Any ideas on how to correct this?

brian Thu 4 Mar 2010

Don't really have a clue.

Perhaps first place to start is making sure it works in Java (since everything in Fantom ultimately just wraps Java's I/O streams)

tactics Thu 4 Mar 2010

I'll investigate it tonight then.

KevinKelley Thu 4 Mar 2010

Fan streams are automatically buffered, so once in a while I run into needing to flush() to force output. Not sure what you'd flush here, though; maybe reading character by character instead of readLine?

Seems like I remember default behavior for console-streams in C as being to flush on newlines; that'd be handy to have I think.

Login or Signup to reply.