Use Case 11: one writer, at max N readers
This use case extends Use Case 5 in this way: “I can run only one writer and a limited number of readers”.
The “one writer many readers” pattern can be implemented using lock modes Protected Write and Concurrent Read as explained here
If you wanted to limit the number of readers, you could implement the limit using a numeric resource as shown below.
Open four terminals and try this experiment:
- inside the first terminal write this command at prompt, but do not press “enter”: “flom -l PW -- ping -c 30 localhost”; this is our writer task (30 seconds ping)
- inside the second terminal write this command at prompt, but do not press “enter”: “flom -l CR -- flom -r foo[2] -- ping -c 10 localhost”; this is our first reader task (10 seconds ping)
- inside the third terminal write this command at prompt, but do not press “enter”: “flom -l CR -- flom -r foo[2] -- ping -c 20 localhost”; this is our second reader task (20 seconds ping)
- inside the fourth terminal write this command at prompt, but do not press “enter”: “flom -l CR -- flom -r foo[2] -- ping -c 10 localhost”; this is our third reader task (10 seconds ping)
- now press “enter” key at the first terminal
- switch to second terminal and press “enter” key
- switch to third terminal and press “enter” key
- switch to third terminal and press “enter” key
Expected result:
- the first terminal starts to display ping output immediately
- the second terminal starts to display ping output immediately
- the third terminal starts to display ping output immediately
- the fourth terminal waits some seconds (until second terminal activity ends) and then starts to display ping output
Explanation:
Two resources are used: the default simple resource to implement one writer, many readers pattern and a numeric resource of name foo[2] to implement the readers limitation (a maximum of 2 readers are allowed to executed at the same time).
The hack is implemented with flom command nidification:
flom -l CR -- flom -r foo[2] -- ping -c 10 localhost
that can be decomposed in this way:
- lock default resource using mode Concurrent Read and execute the command flom -r foo[2] -- ping -c 10 localhost protected by the obtained lock
- lock numeric “foo[2]” resource, pick-up one of the available slots, and execute the command ping -c 10 localhost protected by the obtained lock
Your attention please!
FLOM does not limit the number of nidified levels and FLOM does not implement a deadlock detection algorithm: you must carefully think about lock concatenation order to avoid resource starvation
Summary
This use case explains you how to implement a non trivial synchronization among N+1 tasks.
See also
FLoM available arguments are documented in man page: use man flom. FLoM configuration explains how you can specify flom behavior without using command line arguments.