mkfifo /tmp/fifo创建fifo文件。
java通信
public class Read {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream inputStream = new FileInputStream("/tmp/fifo");
byte[] bs = new byte[1024];
int n = 0;
while ((n = inputStream.read(bs)) != -1) {
System.out.println(n);
System.out.println(new String(bs, 0, n));
}
System.out.println(n);
}
}
public class Writer {
public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
FileOutputStream outputStream = new FileOutputStream("/tmp/fifo");
for(;;){
outputStream.write("abcdef".getBytes());
outputStream.flush();
Thread.sleep(1000);
}
// outputStream.close();
}
}
这样可以实现java和c之间的通信,这里只有java的代码。以上代码只限制于linux平台。