Results 1 to 7 of 7

Thread: How to lock QFile across processes in Qt4.8?

  1. #1

    Question How to lock QFile across processes in Qt4.8?

    Hi everyone,

    I am working on ubuntu 12.04 64.
    And I am looking for a class or a function to lock a QFile across PROCESSES, not threads.

    There used to be a range of classes performing the similar functionalities, but I can't see them available in Qt4.8:

    - QSystemReadWriteLock
    - QLock
    - QtLockedFile
    - QSystemMutex

    I wonder where have they gone, and why.

    QSystemSemaphore does not work in my case, since the other processes accessing the file is not written in Qt.

    I tried to use the C solution, fcntl(fd, F_SETLKW, &lock);
    but it always return -1 (error) if the file is opened by QFile. Although it works using the function open() from C libs.
    I can understand it used to work, as I found some Q classes using it in the past.

    Currently I am counting on QxtFileLock to provide the same functionality, but there would be an additional lib in my system and would not be the best thing to do in my case.
    but it struggles for me to understand the reason to remove file locking functionalities from Qt.
    Or does it exist somewhere that I haven't find so far?

    Thank you very much and hope to find a nice and neat solution in my working case.

    Cheers.

    Lan

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to lock QFile across processes in Qt4.8?

    QtLockedFile is in Qt Solutions. Have a look at http://qt.gitorious.org/qt-solutions...r/qtlockedfile.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to lock QFile across processes in Qt4.8?

    Quote Originally Posted by menkey18 View Post
    QSystemSemaphore does not work in my case, since the other processes accessing the file is not written in Qt.
    I don't see what one has to do with the other. Other processes can use semaphores too. What is important is that you use the same key in every case.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to lock QFile across processes in Qt4.8?

    On Unix one way to make programs kind of exclusive is using D-Bus.

    Each program attempts to use register the same service name on the bus (e.g. session bus for session exclusiveness) and only continue when it has succeeded.
    If it fails someone else has the "lock" and the program has to retry (can listen for name becoming unregistered).

    One of the advantages is that the name is automatically released when the one holding it exits, independent of clean exit or crash.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to lock QFile across processes in Qt4.8?

    Quote Originally Posted by wysota View Post
    I don't see what one has to do with the other. Other processes can use semaphores too. What is important is that you use the same key in every case.
    So, if other processes trying to open file is a third-party software, there is no cross-platform(Qt) way of exclusively lock the file (by using file-system/os means)?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to lock QFile across processes in Qt4.8?

    QSystemSemaphore is an OS means. With Qt apps one can use QSystemSemaphore and with non-Qt apps (on Linux) one can use sem_* functions (see man sem_overview).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to lock QFile across processes in Qt4.8?

    OK, I got my answer, will google better next time .)

    http://www.qtcentre.org/archive/index.php/t-21447.html
    wysota
    27th December 2010, 23:29
    On Unix systems there is no mandatory locking which means in a general case you cannot prevent someone else from writing to the same file while you have it open. It requires cooperation of all involved sides and use of advisory locking (e.g. using flock). Some filesystems may implement mandatory locking (through fcntl calls) but others will not and on some systems (like Linux) mandatory locks are unreliable (see the man page for fcntl)

Similar Threads

  1. Replies: 4
    Last Post: 9th May 2011, 09:52
  2. QFile &QFile::operator= is private
    By Fallen_ in forum Newbie
    Replies: 1
    Last Post: 15th March 2011, 15:08
  3. QWidgets and processes
    By mihaiadrian in forum Qt Programming
    Replies: 3
    Last Post: 19th May 2010, 16:38
  4. QSharedMemory in processes from different accounts
    By Ursa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2009, 09:50
  5. Enumerate processes using Qt
    By Ben.Hines in forum Qt Programming
    Replies: 5
    Last Post: 14th February 2006, 15:45

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
  •  
Qt is a trademark of The Qt Company.