PDA

View Full Version : How to manage many softwares competing for accessing the same data?



Momergil
5th February 2014, 16:21
Hello,

I'm thinking about the idea of developing a pair of softwares, the second devoted to provide some data to the first one. Essentially what the second app will do is to periodically download data from the web and provide it in an organized way in a text (.txt) file (or maybe XML). And this data will be read by the first app, who will use it at will.

The problem is the sync: how could I be sure that the first app will not try to read from the file at the same time the second is writing on (or erasing) it? Notice that this all happens in an asynchronous way (there is no telling when the first app will decide to read the file, and there is no specification yet when the second will be started to do its job - it may start working by a QProcess call from the first at any time or together with it on its initialization).

The question is: how to make sure there will be no problems in this write/read process?

I though about two solutions: in one, both of them would be talking to each other by another, secondary simple .txt file. On it, a simple "0 or 1" signal would be used by one of them to know if they can either read or write to the data file. The problem for me is that even a single-byte file could lead to the same read/write problems I'm trying to run away from. And the second solution is to use QProcess to make them talk to each other in a similar algorithmic way like it would be done in the first solution.


So is there a better way of doing this? Could Qt help someway?

Thanks,

Momergil

anda_skoa
5th February 2014, 18:29
Have a look at QSystemSemaphore.

Each process acquires it before accessing the file and releases it when done.

You can reduce the time each process is holding the semaphore by using move/rename of the file at handover point:

downloader:
1) downloads file to filename.download
2) acquires semaphore
3) deletes filename
4) renames filename.download to filename
5) releases semaphore

processor:
1) acquires semaphore
2) renames filename to filename.processing
3) releases semaphore
4) processes filename.processing

Cheers,
_

d_stranz
6th February 2014, 00:47
You may also be able to implement this using QSharedMemory instead of files.


there is no specification yet when the second will be started to do its job

So does this mean that if the first app is started first, will it read whatever the last information was that was in the file, or will it wait until something new comes in? If it is the first case, then shared memory probably won't work. If it is the second case, then the shared memory will work, but it should be created by the second process when it starts. If it doesn't exist when the first process starts, then attempts to attach() to it will fail. The first process can then periodically try again until the attach() succeeds.