管道文件消失但仍然有效

发布于 2024-08-18 03:59:39 字数 997 浏览 6 评论 0原文

我有 2 个程序,都是用 Java 编写的。第一个启动第二个的多个实例,然后通过管道文件与它们进行通信。当运行该程序的 2 个实例时(我将调用启动器 A,其他启动器称为 B 和 C),一切正常。管道文件位于 /tmp/[A 的 pid]/B 和 /tmp[A 的 pid]/C 中。如果 B 或 C 关闭,则其他应该继续工作,除了整个 /tmp/[A 的 pid] 文件夹消失之外。

另一个程序检测到这一点并尝试关闭自身,因为如果没有管道文​​件,它就无法工作。

我的问题是,如果管道文件消失了,为什么它还能继续工作?为什么它们首先会消失?

如果 C 关闭,则 A 和 B 继续运行。唯一运行的代码是 System.exit(0);,除了处理从管道 A 接收到的消息之外,不执行任何操作。

编辑:

根据请求创建目录和管道的代码。

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);

实际的代码有点复杂,但这是基本思想。

当程序关闭时。

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

读取和写入线程是在其自己的线程中完成的,使用 BufferedReader 和 BufferedWriter 对象将其视为普通文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-08-25 03:59:39

我无法回答为什么管道文件被删除,没有足够的信息。

我可以回答为什么该程序仍然有效。在unix中,从目录中删除文件名并不会删除该文件。仅当不再存在目录条目并且该文件未被程序使用时,才会删除该文件。

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.

薔薇婲 2024-08-25 03:59:39

在 unix 中,内核对所有打开的文件保留一个引用计数 - 如果您打开一个文件,该引用计数就会增加,而当您关闭该文件时,它就会减少。内核中的文件结构保持有效,直到引用计数降至 0。从文件系统中删除管道会阻止新进程打开该文件,但已打开该文件的进程可以继续使用该文件,不受文件删除的影响。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文