Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Thread safe equivalent of LINUX touch command

by flexvault (Parson)
on Nov 22, 2012 at 18:22 UTC ( #1005166=note: print w/ replies, xml ) Need Help??


in reply to Re: Thread safe equivalent of LINUX touch command
in thread Thread safe equivalent of LINUX touch command

BrowserUk,

Happy Thanksgiving!

Same result with 'fork' and if two or more users executed the script from the command line. I guess I think of "?-save" as that you don't allow a race condition to exist, and must use locking to prevent the race condition.

Regards...Ed

"Well done is better than well said." - Benjamin Franklin


Comment on Re^2: Thread safe equivalent of LINUX touch command
Re^3: Thread safe equivalent of LINUX touch command
by BrowserUk (Pope) on Nov 22, 2012 at 19:17 UTC
    I guess I think of "?-save" as that you don't allow a race condition to exist, and must use locking to prevent the race condition.

    In order for the potential for a "race condition" to exist, several algorithmic prerequisites are needed:

    1. Two (or more) execution contexts (EC*) need to concurrently access shared storage,
    2. Those ECs need to write to that shared storage.
    3. The values they write need to be derived from a value previously read from (the same or different) shared storage.

    These algorithmic prerequisites are required because:

    • If no writes occur, there is no race condition.
    • If the writes are not to a shared location, there is no race condition.
    • If the values written are not derived from a value held in shared storage, there is no race condition.

    As (the normal meaning of) 'touching a file' does not require any of these, a "race condition" cannot occur.

    That said, it is possible to contrive a bespoke "touch" requirement that could introduce a race condition. For example, an algorithm might require that not just the 'modified' timestamp be set; but also, one, the other, or both of the other two timestamps be set to the same value.

    In the case, if the OS, or the tool, required multiple calls to set multiple timestamps, it would be possible for one thread to modify one of the TSs, then get swapped out; then a second thread modifies all of the affected TSs; and then the first thread gets another timeslice and modifies the other one or two TSs.

    The result would be that the file would get a mismatched set of timestamps.

    The windows SetFileTime system call (can) set all three in one call, thus preventing that possibility, but that does not prevent a particular implementation of 'touch' from choosing to call the function multiple times to set multiple timestamps, in which case a race condition would be introduced.

    And I don't know if other OSs can update multiple timestamps as an atomic operation or not.

    Hence, to really answer the OPs question, it will be necessary for him to answer my question and clarify exactly what he is doing with his 'touch' operation.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

    The values they write need to be derived from a value previously read from (the same or different) shared storage.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-07-30 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls