How to prevent contention on resources between multiple processes?

I need a reference about how flock() works on a volume.

Particularly, will the lock be (immediately) removed if the process in locking machine dies?

That should be standard Linux, the OS handles all that stuff at the low level.

This sounds like an XY problem. It may be better if you explain why you think you need this reference; what you think you need may be a misdiagnosis. What are you actually wanting to accomplish?

I want to lock rows (of a directed graph of actions to take dependent on other options) of a database, because they may be used by two processes at once.

I realized that using a volume for this may be a bad idea, because a volume can be attached only to one machine.

I will instead lock writing the time of lock into a special DB field.

Yes, volumes won’t work here.

You can just have a hostname field in your database, plus a timestamp as you suggest. Use nulls for unclaimed rows, and a queue processor can attempt to update its hostname and a timestamp when it attempts to make a claim.

The number of rows affected will determine success: 0 means no claim, 1 means a successful claim. Since this operation is atomic, processors will either succeed or fail, avoiding race conditions.

Make sure you use an ACID-compliant database, such as MySQL or Postgres, to ensure atomic operations really are atomic.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.