Regarding Locking The File
-
[quote author="Indrajeet" date="1308819271"]
2 Apps running on 2 different machines access same file located at some location.
[/quote]the following recommendation is then the method.
[quote author="loladiro" date="1308819457"]You could place a lock file somewhere, where both machines can access it.[/quote]However, I doubt that Qt will have something for you. Probably, you have to make the mechanism your self. Is the access from different machines on a frequent basis? If not, you may use a simple technique such as an additional file indicating taht the main file is locked. But it looks like a lot of traps.
-
You don't have to exchange data between more processes. Just use "aquire":http://doc.qt.nokia.com/4.7-snapshot/qsystemsemaphore.html#acquire to lock some memory block and "release":http://doc.qt.nokia.com/4.7-snapshot/qsystemsemaphore.html#release to unlock that memory block.
-
[quote author="cincirin" date="1308821592"]You don't have to exchange data between more processes. Just use "aquire":http://doc.qt.nokia.com/4.7-snapshot/qsystemsemaphore.html#acquire to lock some memory block and "release":http://doc.qt.nokia.com/4.7-snapshot/qsystemsemaphore.html#release to unlock that memory block. [/quote]
What's the help?
The idea is to share a file somewhere. Two applications are acessing the file, but since the applications are not hosted on the same machine, they do not share memory. -
bq. What's the help?
The idea is to share a file somewhere. Two applications are acessing the file, but since the applications are not hosted on the same machine, they do not share memory. [/quote]Sorry, I did not see: "2 Apps running on 2 different machines"
In first post @Indrajeet not mention this situation. -
What about "setPermissions":http://doc.qt.nokia.com/latest/qfile.html#setPermissions ? You can set QFile::ReadOwner | QFile::WriteOwner permission when you have access and QFile::ReadOther | QFile::WriteOther when you finish to read / write.
-
Hi All
I have created a DLL in QT which contains two functions.
1.)Read File
2.)Write FileThis DLL is used by more than 1 application on same PC.Read & Write will be performed on same file kept at some shared location.
So the problem Iam facing is all applications are trying to write file at same time.
So is there any way by which we can give access to only one at a time to write to the file.
so once he complete writing the other can writeGive Sample Code.
-
The fact that the function is in a dll is irrelevant. Are the applications that are in contention running on the same machine or separate machines? You hav said both so far.
If they are ont he same machine you can use QSystemSemaphore or a lock file type mechanism.
If they are on separate machines and the shared file is on some kidn of network drive then you will most likely be best using a lock file.
-
You are changing stories.
Yesterday you wrote (emphasis by me):
[quote author="Indrajeet" date="1308819271"]Hi
I dont want to exchange the data between 2 processes.
2 Apps running on 2 different machines access same file located at some location.
[/quote]Some 20 hours later it is (emphasis by me):
[quote author="Indrajeet" date="1308898293"]
This DLL is used by more than 1 application on same PC. Read & Write will be performed on same file kept at some shared location.
[/quote]Which one is the right one?
-
A QSystemSemaphore can protect a resource. A resource can almost anything: a block of memory, access to a piece of hardware, or access to a file. In your case, you allow only one access to your file at the same time. So, you create a semaphore with an appropriate key (perhaps "com.your.domain.your.app/yourFile" or something unique like that), and use an initial value of 1. Then, when you want to start accessing your file, you call the acquire() method on the semaphore, and check the return value. If the return value is true, then you have successfully acquired the resource (access to the file), and can proceed to modify the file. If you get a false, then something else is already accessing the file and you can not modify it.