我有两个进程可以并发访问同一个文件,我想实现文件锁定。问题似乎是一个进程是用java写的,另一个是用C写的,不清楚java端是如何实现低级别锁定的。平台是Solaris 10。我试图在文件上引入锁,以防止Java进程在C进程读取文件时进行更新。我的想法是尝试从java代码中获取锁10次,然后才无条件地写入文件(我假设锁类型是建议锁)。然而,java tryLock()在第二次尝试时打破了C进程的锁并破坏了读取。
下面是代码,简化(Java):
int iAttemptCnt = 0;
FileChannel wchannel = new FileOutputStream(new File(fileName), false).getChannel();;
FileLock flock;
while(true){
try{
MyLog.log(MyLog.LVL_INFO, "attempt to lock file");
if( (flock = wChannel.tryLock()) == null ){
// lock held by another program
if(++iAttemptCnt >= 10
break;
}
else{
MyLog.log(MyLog.LVL_INFO, " file locked");
break;
}
}catch(OverlappingFileLockException ofle){
.......
if(++iAttemptCnt >= 10 ){
...
break;
}
}catch(IOException ioe){
throw new IOException("failed to lock the file");
}
try{
MyLog.log(MyLog.LVL_INFO, "File already locked, retrying in one second");
Thread.sleep(1000);
}catch(InterruptedException ie){
.....
}
}
C代码使用fcntl:
fd = open(filename, O_RDONLY);
.....
lck.l_type = F_RDLCK;/* F_RDLCK setting a shared or read lock */
lck.l_whence = 0; /* offset l_start from beginning of file */
lck.l_start = 0LL;
lck.l_len = 0LL; /* until the end of the file address space */
....
while( fcntl(fd, F_SETLK64, &lck) < 0){
if( errno == EAGAIN )
....
else if (errno == EIO )
...
else if( errno == ENOLCK)
...
else if (errno == EDEADLK)
...
if(++ii == 10 ){
break;
}
...
sleep(1);
}
MyLongLastingRead();
...
lck.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &lck);
close(fd);
tryLock()
真的检查锁吗?
我不确定这是否会解决您的问题,但在示例中,我看到羊群结构的l_pid字段设置如下。
fl.l_pid = getpid();
在你的问题中,你没有设置这个字段。试着看看会不会有什么不同。