PDA

View Full Version : How to lock QFile across processes in Qt4.8?



menkey18
29th January 2013, 03:48
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

Lykurg
29th January 2013, 07:44
QtLockedFile is in Qt Solutions. Have a look at http://qt.gitorious.org/qt-solutions/qt-solutions/trees/master/qtlockedfile.

wysota
29th January 2013, 14:28
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.

anda_skoa
29th January 2013, 14:34
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,
_

lanz
30th January 2013, 09:08
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)?

wysota
30th January 2013, 10:38
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).

lanz
30th January 2013, 14:03
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)