我正在编写一些 JNI 代码,其中运行在系统上各种进程的进程空间中的 DLL 需要与 java 进程对话。在权衡了共享内存/套接字/rpc 等之后,我决定使用命名管道来执行此操作(出于各种原因)。我的问题是,有没有一种很好的方法来处理 Java 中的命名管道,或者我应该写一个?
问问题
1385 次
2 回答
1
假设您在 Unix 上运行,您不能只使用 exec 创建管道,然后使用 File*Stream 进行读写吗?
@Test public void pipe() throws IOException, InterruptedException {
Runtime.getRuntime().exec("mkfifo mypipe");
final String[] read = new String[1];
Thread t = new Thread() {
@Override
public void run() {
try {
BufferedReader r = new BufferedReader(new FileReader("mypipe"));
read[0] = r.readLine();
} catch (IOException e) {
}
}
};
t.start();
FileWriter w = new FileWriter("mypipe");
w.write("hello\n");
w.flush();
t.join();
assertEquals("hello", read[0]);
}
于 2009-11-03T12:04:00.870 回答
0
我曾经通过进程的输入和输出流进行进程通信:例如
Process p = Runtime.getRuntime().exec("myproc");
OutputStream is = p.getOutputStream();
BufferedOutputStream bis = new BufferedOutputStream(is);
bis.write("your_command");
同样,您可以使用输入流来读取其他进程必须对您说的内容。
于 2009-11-03T12:00:05.763 回答