【发布时间】:2015-06-29 14:54:55
【问题描述】:
我正在尝试使用 Java 创建命名管道。我正在使用 Linux。但是,我遇到了写入管道挂起的问题。
File fifo = fifoCreator.createFifoPipe("fifo");
String[] command = new String[] {"cat", fifo.getAbsolutePath()};
process = Runtime.getRuntime().exec(command);
FileWriter fw = new FileWriter(fifo.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(boxString); //hangs here
bw.close();
process.waitFor();
fifoCreator.removeFifoPipe(fifo.toString());
fifoCreator:
@Override
public File createFifoPipe(String fifoName) throws IOException, InterruptedException {
Path fifoPath = propertiesManager.getTmpFilePath(fifoName);
Process process = null;
String[] command = new String[] {"mkfifo", fifoPath.toString()};
process = Runtime.getRuntime().exec(command);
process.waitFor();
return new File(fifoPath.toString());
}
@Override
public File getFifoPipe(String fifoName) {
Path fifoPath = propertiesManager.getTmpFilePath(fifoName);
return new File(fifoPath.toString());
}
@Override
public void removeFifoPipe(String fifoName) throws IOException {
Files.delete(propertiesManager.getTmpFilePath(fifoName));
}
我正在编写一个包含 1000 行的字符串。写 100 行有效,但 1000 行无效。
但是,如果我在外部 shell 上运行“cat fifo”,那么程序会继续并写出所有内容而不会挂起。奇怪的是这个程序启动的cat子进程不起作用。
编辑:我对子进程进行了 ps,它的状态为“S”。
【问题讨论】:
标签: java linux named-pipes