It's been years since my paws had to pound out a solution in C, so I'm a bit rusty and would like a second opinion.. or third... or fourth.
Problem: I need to serialize access to a "seed" file containing an incremental sequencing number. Process is:
- read file
- increment seed
- write number back to file
The entire transaction has to be atomic and block until completion.
SimpleMindedSolution #1: Write a posix threaded program and Mutex lock around the file operation.
Problem: I would have to buy a C language posix threads package. Costs $$.
SimpleMindedSolution #2: Write a user-level threaded program with DCE threads.
Problem: DCE threads usage is going away. DCE supports only user-level threads. User-level threads don't work well on 11.x
SimpleMindedSolution #3: Forget about threads and use "lockf". Process:
- open file
- write file
- close file (releases lock)
Problem? Perhaps none. It seems too simple.
Do you have experience with lockf? Specifically, in areas such as deadlock, sleeping (wish I could), etc...?
There's a golden rule in software design: If you have several design approaches, and all of which seem sound and feasible, go for the simplest one. You can always optimize and tune your code later.
Reading a file, incrementing a seed, and writing that seed back to the file can be implemented as a single threaded task. The complexity and overhead of multithreading is really unnecessary for such a simple and short operation.
Assuming your file is not extremely large (i.e., it doesn't contain more than a few megabytes), the entire operation should take about a second or less. In addition, it seems like you need a shared lock during most of the operation, because the task only reads from the file most of the time, but doesn't write to it. Only when you write the incremented seed to the file does the task need to obtain an exclusive lock. However, even in this case, you don't need a file lock, but rather, a record lock.
Thus, your task consists of the following steps:
- Use a shared lock to read the file
- Release that lock
- Increment the seed
- Obtain an exclusive record lock (instead of locking the entire file) only for the number of bytes and offset of the seed's position in the file
- Write the new value to the file
- Release the exclusive record lock
You can use either lockf() or fcntl() for obtaining the locks, depending on the specific Unix flavor you're using.