java mkfifo_在Java中创建命名管道
我正在嘗試使用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”.
解決方法:
外部流程具有您需要處理的輸入和輸出.否則,它們可能會掛起,但它們掛起的確切位置會有所不同.
解決問題的最簡單方法是更改??每次出現的問題:
process = Runtime.getRuntime().exec(command);
對此:
process = new ProcessBuilder(command).inheritIO().start();
Runtime.exec已過時.請改用ProcessBuilder.
更新:
inheritIO() is shorthand用于將Process的所有輸入和輸出重定向到父Java進程的輸入和輸出.您可以改為僅重定向輸入,并自己讀取輸出:
process = new ProcessBuilder(command).redirectInput(
ProcessBuilder.Redirect.INHERIT).start();
然后,您需要從process.getInputStream()中讀取進程的輸出.
標簽:java,linux,named-pipes
來源: https://codeday.me/bug/20190727/1558183.html
總結
以上是生活随笔為你收集整理的java mkfifo_在Java中创建命名管道的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汾酒股票还能买吗 最有潜力的白酒股之
- 下一篇: java美元兑换,(Java实现) 美元