Synchronized access to a file [Linux]

Hi. I have a question concerning synchronized access to a file. I have a programm in Java, and another one which is a deamon written in C/ C++. Both of them can access a file: the Java programm reads it an updates its contents, and the deamon just udates it. This mean that I need to synchronize the access of both these programmes (I can interfere with the source so changing the code is not an issue). The question is, how to do it properly?

Thanks for any answers.

[480 byte] By [szczypiora] at [2007-9-25]
# 1
You need to use whatever File locking API's are available in each language. For Java you would have to use java.nio.channels.FileLock and FileChannel. For C/C++ checkout flock and lockf.
davidholmesa at 2007-7-14 > top of java,Core,Core APIs...
# 2

Ok, I read about it and I think it'll do. Bu tone more thing - what happens in a situation that my Java app tries to acquire a file lock, and the deamon tries to do the same? Is such thing handled internally by the linux kernell? If JVM run under Linux will simply use it's native techniques I think there will be no problem ,but I would rather know it before any problems happen :-)

Thanks for your answer!

szczypiora at 2007-7-14 > top of java,Core,Core APIs...
# 3
That's what locking is for!
ejpa at 2007-7-14 > top of java,Core,Core APIs...