Jump to content
Sign in to follow this  
IAMK

Does FileOpen() have any race conditions?

Recommended Posts

I have a co-worker's c++ program creating/updating a log (.txt file), and I need my script to open that log and read it.

My code: 

Local $previousLogged = -1

While($fileUpdated = False)
    Local $logFile = FileOpen("logFile.txt", 1)
      
    If($previoudLogged <> FileReadLine($logFile, 1)) Then ;If .txt has been updated.      
        $fileUpdated = True
    EndIf
      
    FileClose($logFile)
    Sleep(200)
WEnd

Is it possible for the data I read to be incomplete? Or is it safe to assume that FileOpen() will get the file at the state when it was last closed (by c++)?

Thank you in advance.

Share this post


Link to post
Share on other sites

Concurent readers are OK. Reading a file which can possibly be written to simultaneously can lead to disaster. Concurent writing is sure to produce permanent errors at random times (some data will not be logged at times and/or data can be definitely corrupted).

Regular primitives for file I/O in Windows are not ACID regardless of the language used to develop a program handling files. You need to use a file cooperative locking machinery or, better, a database for that. A database is a perfect match for dealing with log files, both for ACID properties and ease of querying.

Give SQLite a try (see help).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@jchd, In terms of atomicity, does it make a difference if I use FileOpen($file, 0) and FileOpen($file, 1) to read the file (as long as I don't write to it)?

Share this post


Link to post
Share on other sites

I don't believe so but I have no guarantee. An important detail: are all concurent processes local or is the .log file accessed over a network ?

Anyway you'd better use just FileRead() to read it but you have no insurance either.

The ACID properties are only put in jeopardy while asynchronous writes are taking place.
Appending new data or rewriting the whole file from memory with new data appended are by far not atomic operations. They require a variable number of cache and disk read and writes to update both the file structures in the file system and the new data content. All of the low-level operations are done by kernel-mode drivers but there is no concept of transaction in NTFS volumes and local multitasking or asynchronous remote processes can step in while the whole thing is being done.

Storage media (rotating dust or SSDs) also have their own independant "life", which defies precise top-view analysis. Most user-grade disk controlers also lie when they return the "writen to disk" OK status, because they actually put data to disk sometime later.

A [R]DBMS is nothing else than a much more robust and ACID filesystem on top of the volume filesystem, with a fine-grain user-controlled granularity of feature-rich data structures, coming along with sophisticated command-line writes/updates and an agile Explorer on alien steroïds for querying.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@jchd You slightly lost me as you went on, but I get the gist of it.
As for your question, it's all local (and usually in the same folder) :)

@KaFu I will look into that.

Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...