in reply to Locking database
No, that's not a particularly good way to lock a database, because it has a race condition. Suppose you have two processes vying to open the file. The first one spends its timeslice executing the loop in lines 1 and 2, then is suspended in favor of the second process. The second process starts, hits line 3 and opens the database. It takes a while to open the file, so it uses its timeslice up and control returns to the first process.
The first process does not check for the existence of the semaphore file, and proceeds to open the database itself. Uh oh.
The documentation for DB_File has a nice extended example of locking one of these databases. It's much to long to post here, but it's a good example. (The solution is an atomic locking operation. It gets the name because it opens and locks the file in a single, uninterruptable step.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Do not flock dbm filehandles!
by tilly (Archbishop) on Feb 18, 2001 at 08:56 UTC |