Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Feb 3, 2000

Using Lockf with Seed Files

Question:
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:

  1. read file
  2. increment seed
  3. 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:

  1. lockf
  2. open file
  3. increment
  4. write file
  5. 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...?

Answer:
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:

  1. Use a shared lock to read the file
  2. Release that lock
  3. Increment the seed
  4. 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
  5. Write the new value to the file
  6. 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.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap