Lalasa said:
Hi Willy,
There is this scenario where one application writes to a text file
and another application reads this text file. The second application
should not touch the file untill it is completely written by the first
application. So the solution we have is the first application writes to
a work file first and then after it is done renames with the right
name. The second application keeps polling the directory for the
existance of the file and as soon as it finds, it goes ahead and reads
the file.
The assumption we had was writing to a file will take a long time if
the file is too big and so writing to a temp file and renaming it
wouldnt allow the second application to touch untill the file is ready.
But once in a while we are having problems like the second application
hanging up or the second application waiting even though the file is
there and stuff like that. So I am doing research on what would be
causing this problem and the first thing I was thinking about was cpu
cycles.
If a rename in assembly language converts to a single instruction, that
means it takes a single cpu cycle. if it converts to a set of
instructions, then that means it takes mulitple cpu cycles. What you
say?
Thanks,
Lalasa.
Creating and writing to a temporary file and renaming the file when done
with it makes no sense as the second application has to wait anyway. So what
you should do is:
- Have application 1 to create the file in exclusive access mode, write the
data and close the file.
- Have the second application trying to open the same file at regular
intervals in a loop until it succeeds.
Another option (Preferred if you have control of both application) is to use
a named mutex; application 1 creates the mutex and takes ownership when the
creation process starts and releases the mutex after the file gets closed.
The second application waits for the mutex to be signaled after which the
file can be opened.
As for your "single instruction file rename" question, it seems you have no
idea at all about the complexity of a file system and what it takes to
"rename a file". As I said before, touching the filesytem for the most
simple thing like a file rename takes hundreds of thousands CPU cycles.
Willy.