2

I am trying to write a general purpose locking algorithm which allows me to lock a range of contiguous values in a sorted array. To start with, these could be exclusive locks - so two concurrent locks must be non-overlapping. What could be a good, scalable algorithm to do this? (Scalable ~ concurrent)

A specific example could be locking a range of contiguous bytes in a file for writing.

Concretely: Let's say {a_i} is a valid sequence, for i = 0 through N-1, such that:

  1. a_j < a_k for all j, k such that 0 <= j < k < N.
  2. lock(a_j, a_k) locks all elements from a_j through a_k.
  3. While the above lock is held, another request lock(a_x, a_y) will succeed only if either a_x > a_k, or else a_y < a_j.
CppNoob
  • 2,322
  • 1
  • 24
  • 35
  • What would happen to a request if `a_x > a_j` and `a_y < a_k`? – kiner_shah Dec 23 '18 at 09:24
  • It should block, or it should fail. It will not go through. – CppNoob Dec 23 '18 at 14:21
  • A locking mechanism [fcntl](http://man7.org/linux/man-pages/man2/fcntl.2.html) is available in Linux which does something very similar to your requirement. See [this](http://pubs.opengroup.org/onlinepubs/009604599/functions/fcntl.html) too. – kiner_shah Dec 24 '18 at 15:01
  • I am aware of fcntl's byte range locking. But this is asking about how such a system or an equivalent system for any other resource can be implemented. – CppNoob Dec 24 '18 at 15:26
  • Can you give an example of any other resource for which this can be applied? – kiner_shah Dec 25 '18 at 06:14
  • 1
    Yes. I want to lock a range of contiguous ports, use them for communication and then release them. – CppNoob Dec 25 '18 at 14:53
  • 1
    @kiner_shah fcntl's byte-range locking is implemented using some algorithm, right? How is it done? I hope that's a valid a question. Moreover, this is not a POSIX compliant behavior but an extension. If I want common behavior across different Unix platforms, I will still need to code it. – CppNoob Dec 25 '18 at 14:55

0 Answers0