What you need is some sort of semaphore system. It would be simple to this with MemCache or a database, but I suspect you want to avoid such as those as well as named pipes or other IPC approaches.

That said, if (and this is a very big if) you can assign individual script 'IDs' to the slaves, then you can take the approach that each slave has a unique ID such that you can use binary operations to extract the relevant ID bit from an int stored in a 'lock' file to determine if data is ready to read or if all slaves have finished reading.

The logic would be something like this: if master, read lock file. If it exists, write new data only if the value of the lock file indicates that all slaves have read old data. If the file does not exist, write new data file and set lock file value to '0' to indicate data is ready. If slave, read lock file. If it does not exist, wait until it does. Use a bit-wise 'AND' (&) operation to determine if the slave bit is already set. If not, read data and then write lock file after bit-wise 'OR'ing (|) in the slaves bit. If the bit is already set, wait until lock file value for the slave ID bit in question is 0, indicating that new data is ready again. You will need to flock the lock file before every read, and be sure to read immediately before any write to get the current value.

If the slaves are also writing the data file, then the above logic would need to be tweaked.

I understand that this is a very crude approach, but the basic logic should be a start.

Edit: I just realized that this presumes that the master knows how many slaves there are in order to determine if all of them have read the data file. Give me another couple of shots of Scotch and I can probably come up with a more general solution. Also, the slave doesn't look for a value of '0' in the lock file, but rather, that the bit corresponding to the slave ID is 0.

It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

In reply to Re: Reading and writting data in a text file with several scripts simultaneously time synchronization problem by boftx
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.