Results 1 to 3 of 3

Thread: How to manage many softwares competing for accessing the same data?

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to manage many softwares competing for accessing the same data?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to manage many softwares competing for accessing the same data?

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Momergil (6th February 2014)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,241
    Thanks
    239
    Thanked 673 Times in 663 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to manage many softwares competing for accessing the same data?

    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.
    Last edited by d_stranz; 6th February 2014 at 01:56.

  5. The following user says thank you to d_stranz for this useful post:

    Momergil (10th February 2014)

Similar Threads

  1. Accessing data of .qml file
    By jeff28 in forum Newbie
    Replies: 1
    Last Post: 29th August 2012, 16:15
  2. QTableView : accessing the data
    By marvaneke in forum Newbie
    Replies: 10
    Last Post: 30th March 2012, 12:31
  3. Replies: 4
    Last Post: 26th September 2011, 10:32
  4. Can GPLed Icons be used in Commercial softwares ?
    By sunil.thaha in forum General Discussion
    Replies: 9
    Last Post: 29th September 2006, 07:30
  5. accessing data in a QFile
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 17:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.