ndbm and file locking

ndbm and file locking

Post by Rob Franc » Wed, 01 Mar 1995 11:26:06



I have an ndbm database which could be accessed by more than one
process at a time.  So, of course I want to do file locking.

So I have a few questions which should be vendor independent (I hope).

lockf wants a file descriptor for the first argument, not a pointer to
DBM.

So,

1) Should I open the .pag file RDWR, do a lock on it, and then in
   the same process, do the dbm_open, dbm_store, dbm_close?

2) I would think I'll have to lock the entire file, as that fd isn't
   going to know what offset in the file will point to where
   I am about to write in the .pag file.  Must I lock the entire
   file?

As I said, this would seem to be vendor independent, but I'll mention
that I am running Solaris 2.3+patches on a Sparc 20.

Thanks in advance for any ideas/suggestions/comments.

-rob
---
Rob Francis
Paradigm Systems   <URL:http://www.sf.psca.com/Hyplans/rfrancis/>

 
 
 

ndbm and file locking

Post by Phi H Truo » Thu, 02 Mar 1995 01:48:52




>I have an ndbm database which could be accessed by more than one
>process at a time.  So, of course I want to do file locking.

No chance.  I have tried this stunt and failed.  You would have a better
chance using the new Berkeley DB (ftp.cs.berkeley.edu:.../ucb/4bsd/db.tar.Z)
or GNU's DB.  ndbm doesn't have built-in locking mechanism but the above two
dbs do.

--
_____
Phi H. Truong              "Hmmmmmmmm....... "      

Systems Analyst            237 Durham Center         ph: (515) 294 -1420

 
 
 

ndbm and file locking

Post by Arjun R » Thu, 02 Mar 1995 12:50:24




>I have an ndbm database which could be accessed by more than one
>process at a time.  So, of course I want to do file locking.

>So I have a few questions which should be vendor independent (I hope).

>lockf wants a file descriptor for the first argument, not a pointer to
>DBM.

lockf is basically obsolete. Use fcntl. For semaphore semantics, use
F_SETLKW (wait until lock is acquired.) Advice: read the _whole_ fcntl(2)
man page. Plenty of stuff to digest.

Quote:>So,

>1) Should I open the .pag file RDWR, do a lock on it, and then in
>   the same process, do the dbm_open, dbm_store, dbm_close?

Best not to mess with the .dir and .pag files. Rather, create a third
.lock file. This need be only one byte long. Arrange to have all your
processes try to acquire a lock on the first byte of that file (obviates
problems of unknown file length.)

Be aware, though, that none of this should be done on NFS mounted files.
File-locking across machines is still buggy on most Un*xes.

For a discussion of the issues and some useful source code:

Rich Stevens, "Advanced Programming in the UNIX Environment"
[Addison Wesley 1992, ISBN 0-201-56317-7]

Especially Chapter 12, Section 3.

Hope this helps.

Cheers,
ar

 
 
 

ndbm and file locking

Post by Rob Franc » Thu, 02 Mar 1995 02:34:22




Quote:

>>I have an ndbm database which could be accessed by more than one
>>process at a time.  So, of course I want to do file locking.

>No chance.  I have tried this stunt and failed.  You would have a better
>chance using the new Berkeley DB (ftp.cs.berkeley.edu:.../ucb/4bsd/db.tar.Z)
>or GNU's DB.  ndbm doesn't have built-in locking mechanism but the above two
>dbs do.

I should clarify that I am forced to use ndbm, as I am writing to a
commercial product's database, and it's not up to me to use gdbm
(which would be nicer anyway  --few holes).

-rob
---
Rob Francis
Paradigm Systems   <URL:http://www.sf.psca.com/Hyplans/rfrancis/>

 
 
 

ndbm and file locking

Post by <N.. » Tue, 07 Mar 1995 23:29:47


Quote:>I have an ndbm database which could be accessed by more than one
>process at a time.  So, of course I want to do file locking.

Here's an alternative I've used in the same situation.   It's actually
very easy to implement (use RPCGEN), and negligible system overhead.

        My_Program (client - multiple instances)
                        |
                        |       <--- RPC interface - function calls
                        |            like dbm_put, dbm_get, etc.
                        |
                ndbm daemon process (ONE instance)
                        |
                        |
                ndbm database

Effortlessly wipes out your integrity hassles.   No need for locking,
and if you DO want to lock (eg during a "list all"), just add it
internally to the dbm server (remember to put in a timeout - in case
user hits ctrl-C or something).

It works better than ANY application I've seen that uses .lock files
or similar - and some of these fetch mega-$.

 
 
 

1. partial file locking with ndbm(3) files

  I'm trying to write a database application (a small one) which
uses ndbm(3).  I'd like to find out how I could write my application
so that a minimum portion of the database is locked at any one
time.  Naturally, the ndbm man page skirts the whole issue of record
locking (What is a concurrent update anyway? :-)

  The most common operations on the database will be (in this order):

   search every record for some trait or another.
   update a single record.
   add a single record.

  I'd rather not lock the whole database when I do a 'search' since
that might hold up the updates&adds for an inordinate amount of time.
While I can readily see how to accomplish a whole-database locking
scheme, I think my performance would be substantially better with
single-record locking.

                                - Steve Benz
                                ...!hubcap!mrspock

2. why NOT echo in []?

3. ndbm and file locking

4. Win2k on dual-pentium Proliant

5. ndbm, dbm, access by concurrent processes, record locking

6. Mouse on ttyS0/cua0: Device or resource busy

7. how do i lock an ndbm database?

8. Need help w/dial-in timing issue

9. Record locking/ndbm/sdbm

10. Matrox Mystique ands X.

11. More mail file locking questions (lockf, NFS, /var/spool/mail/*.lock)

12. Locked files - who has it locked?

13. "Can't read lock file /tmp/.X0-lock"