java. io. Is filedescriptor #sync() specific to a single filedescriptor

I want to force synchronization to disk after writing files at some point in my application Because it runs on Linux, I can avoid running

Runtime.getRuntime().exec("sync");

However, I would rather not introduce Linux specific system calls than use

java.io.FileDescriptor#sync();

However, I use Apache VFS to perform operations on the local file system. As far as I know, it does not provide access to the underlying file descriptor However, do I need to access the actual file descriptor just written to force synchronization? For example, can I invoke synchronization effects without using any filedescriptors

FileDescriptor.in.sync();

This is an effective method. Does the result match the result of synchronous call in Linux?

It is also useful if someone knows how to access the underlying filedescriptor in VFS

Edit: looks like

FileDescriptor.in.sync();

I don't want to run on Linux (although it can run on my windows machine when running from eclipse), but

new FileOutputStream(new File("anyfile")).getFD().sync();

It must be valid. The result of calling it matches the result of directly calling Linux sync command However, it involves opening and closing redundant file output streams, so it is not entirely ideal For any other reason, could this be a bad idea because it seems to work? Is there another way to get a filedescriptor that can be used for synchronization?

Solution

I investigated these questions some time ago: Question 1, question 2

In Linux, Java io. The filedescriptor #sync call ensures that the modified data of the file associated with the descriptor is sent to disk (the cheap disk tends to skip writes and only put data in unreliable (i.e. no NVRAM) write cache is a different / additional problem.) It does not guarantee that the modified data of other files will also be written back This is not a synchronization contract or underlying fsync POSIX function

However, in some cases (for example, ext3 = ordered mode in the data), an fsync on a file writes back up modified data of the file system This is interesting because it may cause significant delays because some other applications create a large number of dirty blocks

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>