Java FileLock是POSIX咨询(fcntl)锁吗? [英] Is a Java FileLock a POSIX advisory (fcntl) lock

查看:157
本文介绍了Java FileLock是POSIX咨询(fcntl)锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++程序,该程序使用POSIX咨询锁来锁定文件.也就是说,它使用 POSIX fcntl系统调用进行锁定操作.我希望Java程序可以与该C ++程序互操作,因此我希望Java程序也使用POSIX咨询锁. Java中的文件锁定应该使用标准 FileLock 类.但是,API文档在实现锁的方式上含糊不清,这是可以理解的:

I have a C++ program that locks files using POSIX advisory locks. That is, it uses the POSIX fcntl system call for lock operations. I want a Java program to interoperate with that C++ program, so I want my Java program to also use POSIX advisory locks. File locking in Java should use the standard FileLock class. But the API documentation is understandably vague on just how locking is implemented:

此文件锁定API旨在直接映射到基础操作系统的本机锁定工具.因此,对文件具有访问权限的所有程序都应该可以看到文件上持有的锁,而与这些程序所使用的语言无关.

This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written.

如果我在POSIX操作系统或更具体地说是GNU/Linux系统上运行Java的通用实现(Oracle,Open JDK),是否可以安全地假设Java FileLock类使用POSIX咨询锁?

If I am running a common implementation of Java (Oracles, Open JDK) on a POSIX operating system, or more specifically a GNU/Linux system, is it safe to assume that the Java FileLock class uses POSIX advisory locks?

推荐答案

某些Unix操作系统(包括Linux)提供

Some Unix operating systems, including Linux, provide BSD-style (flock) locks, so it might be thought that Java FileLock could be implemented using BSD-style locks rather than POSIX locks. But that is not possible, because BSD-style locks are whole-file locks rather than record locks, and FileLock is a record lock: each lock is for a range of bytes in a file. Thus there is no real choice on a Unix system, and assuming that the implementation of FileLock uses POSIX fcntl locks is a safe assumption on a Unix operating system.

生成的FileLock锁可能会或可能不会与BSD样式的锁交互.可以使用POSIX锁来实现BSD样式的锁( Linux就是这种情况. 2.0版之前的版本),或者操作系统可能具有两种锁定交互样式( FreeBSD就是这种情况).但总的来说,这不能保证,并且BSD样式的锁和Java锁可能实际上对彼此是不可见的(

The resulting FileLock locks might or might not interact with BSD-style locks. BSD-style locks can be implemented using POSIX locks (this was the case for Linux before version 2.0), or the operating system might have the two styles of locking interact (this is the case for FreeBSD). But in general that can not be guaranteed, and BSD-style locks and Java locks might be effectively invisible to each other (this is the case for any version of Linux you are likely to encounter).

这篇关于Java FileLock是POSIX咨询(fcntl)锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆