Using Lockf with Seed Files

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…?

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.