gpt4 book ai didi

java - FileChannel tryLock() 方法是检查锁还是只是破坏锁?

转载 作者:搜寻专家 更新时间:2023-11-01 00:57:27 25 4
gpt4 key购买 nike

我有两个进程可能同时访问同一个文件,想实现文件锁定。问题似乎是一个进程是用 java 编写的,另一个是用 C 编写的,并且不清楚在 java 端如何实现低级锁定。该平台是 Solaris 10。我试图在文件上引入锁定,以防止在 C 进程读取文件时由 Java 进程进行更新。我的想法是尝试从 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() 真的检查锁吗?

最佳答案

我不确定这是否能解决您的问题,但在我看到的示例中,flock 结构的 l_pid 字段设置如下。

fl.l_pid = getpid();

在您的问题中,您没有设置此字段。尝试看看它是否有任何不同。希望对您有所帮助。

关于java - FileChannel tryLock() 方法是检查锁还是只是破坏锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14052656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com