当前位置: 技术问答>linux和unix
java调用linux内核问题
来源: 互联网 发布时间:2015-08-27
本文导语: 我原来写了一个库函数为linux下的进程间通讯,现必须实现一个java版本,请问java下如何进行linux内核调用,或是java如何实现进程间通信(比如共享内存,但必须为内核持续的),要不如果java能调用linux下的库函数也行,谢谢 ...
我原来写了一个库函数为linux下的进程间通讯,现必须实现一个java版本,请问java下如何进行linux内核调用,或是java如何实现进程间通信(比如共享内存,但必须为内核持续的),要不如果java能调用linux下的库函数也行,谢谢
|
可以采用外部文件的方法进行通信,互斥时用文件加锁
FileLock lock = null;
RandomAccessFile rf;
try
{
rf = new RandomAccessFile(strFileName, "rw");
lock = rf.getChannel().lock();
FileDescriptor fd = rf.getFD();
FileInputStream fileStream = new FileInputStream(fd);
BufferedReader reader = new BufferedReader(
new InputStreamReader(fileStream) );
String str = null;
String [] strArrayRet = null;
while((str = reader.readLine())!= null)
{
if(str==null || str.equals(""))
continue;
strArrayRet = GetKeyFromSericeCode(strServiceCode, str) ;
if (strArrayRet != null)
{
if(lock != null && lock.isValid())
{
lock.release();
}
if(fileStream != null)
{
fileStream.close();
}
return strArrayRet;
}
}
if(lock != null && lock.isValid())
{
lock.release();
}
if(fileStream != null)
{
fileStream.close();
}
return null;
}
catch (FileNotFoundException e)
{
throw e;
}
catch (IOException e)
{
throw e;
}
catch (Exception e)
{
throw e;
}
FileLock lock = null;
RandomAccessFile rf;
try
{
rf = new RandomAccessFile(strFileName, "rw");
lock = rf.getChannel().lock();
FileDescriptor fd = rf.getFD();
FileInputStream fileStream = new FileInputStream(fd);
BufferedReader reader = new BufferedReader(
new InputStreamReader(fileStream) );
String str = null;
String [] strArrayRet = null;
while((str = reader.readLine())!= null)
{
if(str==null || str.equals(""))
continue;
strArrayRet = GetKeyFromSericeCode(strServiceCode, str) ;
if (strArrayRet != null)
{
if(lock != null && lock.isValid())
{
lock.release();
}
if(fileStream != null)
{
fileStream.close();
}
return strArrayRet;
}
}
if(lock != null && lock.isValid())
{
lock.release();
}
if(fileStream != null)
{
fileStream.close();
}
return null;
}
catch (FileNotFoundException e)
{
throw e;
}
catch (IOException e)
{
throw e;
}
catch (Exception e)
{
throw e;
}