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


In reply to Re: Reading and writting data in a text file with several scripts simultaneously time synchronization problem by QM
in thread Reading and writting data in a text file with several scripts simultaneously time synchronization problem by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.