tags:

views:

43

answers:

2

I have 2 programs, both written in Java. The first launches several instances of the second and then communicates with them via pipe files. When running 2 instances of the program, (I'll call the launcher A and the others B and C) everything works fine. The pipe files are in /tmp/[pid of A]/B and /tmp[pid of A]/C. If B or C close then other should keep on working, which it does except the entire /tmp/[pid of A] folder disappears.

The other program detects this and try to close itself because it shouldn't work without the pipe files.

My questions are why does it keep working if the pipe files are gone? and why do they disappear in the first place?

If C closes then A and B keep on running. The only code that runs is System.exit(0); and except for processes messages received from the pipes A doesn't do anything.

EDIT:

As per request the code that creates the directory and pipes.

File dir = new File("/tmp/" + pid);
dir.mkdirs();

File aDir = new File(dir, "A");
aDir.mkdirs();

File bDir = new File(dir, "B");
bDir.mkdirs();

Runtime.getRuntime().exec(new String[] {"mkfifo", PIPE_NAME}, null, aDir);
Runtime.getRuntime().exec(new String[] {"mkfifo", PIPE_NAME}, null, bDir);

The actual code is a little more complex but that is the basic idea.

When the program closes.

frame.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    System.exit(0);
  }
});

Reading and writing the threads is done in its own thread treating it as a normal file using BufferedReader and BufferedWriter objects.

+3  A: 

I can't answer why the pipe file gets deleted, not enough information.

I can answer why the program still works. In unix, deleting the name of a file from a directory does not delete the file. The file is only deleted when no more directory entries exist and the file isn't in use by a program.

Richard Pennington
+1 - you beat me to it
kdgregory
Ok, thanks for that. Explains one thing at least.
Android
A: 

In unix, the kernel keeps a reference count on all open files - if you open a file, that reference count increases, and when you close that file it decreases. The file structure in the kernel remains valid until the reference count drops to 0. Removing the pipe from the file system prevents new processes from opening the file, but processes that have the file open already can continue to use it, unaffected by the files deletion.

Aidan Cully