in reply to Reading and writting data in a text file with several scripts simultaneously time synchronization problem

I hesitate to add this here, as perhaps you don't need the more complicated solution.

I used to maintain a script that did reads/writes to a file (not a real database file, but similar use). Because of file locking issues across NFS, and the need to allow multiple readers and multiple writers, I grew my own file lock system.

Each script instance wanting to read or write the "database" file would touch a file in a directory based on the name of the DB file. Touched filenames were chosen to sort lexicographically by timestamp, including the hostname and process number of the script, and whether it was requesting read or write access. For example, the touch filename template might be YYYYMMDDHHMMSS_read_hostname_ppppp. Any process wishing to read or write would first touch a filename appropriately, then read in and sort the existing filenames. If requesting a read lock, and all processes scheduled ahead of it are also reads, then start reading. If it's a write lock, wait until it is the oldest lock request. To release a lock, delete the file. This is a FIFO lock request queue.

You may want some mechanism for timeouts, abnormal terminations, etc.

There are also some subtle considerations for any locking system, so do some research before deploying a critical system.

-QM
--
Quantum Mechanics: The dreams stuff is made of

  • Comment on Re: Reading and writting data in a text file with several scripts simultaneously time synchronization problem

Replies are listed 'Best First'.
Re^2: Reading and writting data in a text file with several scripts simultaneously time synchronization problem
by boftx (Deacon) on May 19, 2014 at 20:39 UTC

    Since he said he would like to use a DB, he can take full advantage of atomic inserts to implement the semaphore, thereby avoiding all flock issues by not using it. :)

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re^2: Reading and writting data in a text file with several scripts simultaneously time synchronization problem
by thanos1983 (Parson) on May 19, 2014 at 21:43 UTC

    To QM

    Sounds as a good solution, but unfortunately I do not feel comfortable doing something like that. I am not that advanced and I guess I will not be able to complete it, at least soon enough. But thanks a lot for your proposal is something that I could apply in future.