0

I have two processes reading/writing same files. So I wanna create a lock between them. My idea is to create a file in that folder named file.lock containing the process identification information. The process which has the lock creates the file and releases the lock by deleting it.
My question is:

  1. Is it possible that while process A is creating the file and before the file is eventually created, process B checks the file and finds that it's not there, and thus the mutex fails?

  2. How can wait() method be implemented in this scenario?

gion_13
  • 41,171
  • 10
  • 96
  • 108
NSF
  • 2,499
  • 6
  • 31
  • 55
  • I think it is better to use rdbms instead of file for distributed locking. – Mike Mar 03 '13 at 08:43
  • Using a database is not always the best option, particularly for simple problems. – le3th4x0rbot Mar 03 '13 at 08:48
  • @GregS In some cases I would say it's fine to add additional information as an edit. I guess it would depend on the extent of the changes. A new paragraph at the end is one thing; a complete re-write is clearly not good. – Duncan Jones Mar 06 '13 at 20:29

1 Answers1

0

My recommendation would be to look into the FileLock API.

NPE
  • 486,780
  • 108
  • 951
  • 1,012