June 23, 2011

Adi Adi
Lab Rat
206 posts

Regarding Locking The File

Page  
1

Hi All

I have created a DLL in QT which contains two functions.
1.)Read File
2.)Write File

This DLL is used by more than 1 application.
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 lock or allow one application at a time to write or read.

Give Sample Code.

Regards
Indrajeet

38 replies

June 23, 2011

bobby bobby
Lab Rat
44 posts

Is it possible to replace a static file name with a random character string to prevent multiple writes to the same file?

 Signature 

“Linux is not user-friendly.” It _is_ user-friendly. It is not ignorant-friendly and idiot-friendly.
—-Source unknown

June 23, 2011

cincirin cincirin
Ant Farmer
457 posts

QSystemSemaphore [doc.qt.nokia.com]

June 23, 2011

Adi Adi
Lab Rat
206 posts

Hi cincirin

How about QMutex?

Which is better?

Regards
Indrajeet

June 23, 2011

cincirin cincirin
Ant Farmer
457 posts

A semaphore is a generalization of a mutex. While a mutex can be locked only once, a semaphore can be acquired multiple times.
bq. Unlike QSemaphore, a QSystemSemaphore can also be accessed from multiple processes

June 23, 2011

koahnig koahnig
Gene Splicer
2906 posts

I guess you find the answer in this sequence of text taken from the detailed description

cincirin wrote:
QSystemSemaphore [doc.qt.nokia.com]

“A semaphore is a generalization of a mutex. While a mutex can be locked only once, a semaphore can be acquired multiple times. Typically, a semaphore is used to protect a certain number of identical resources.
Like its lighter counterpart QSemaphore, a QSystemSemaphore can be accessed from multiple threads. Unlike QSemaphore, a QSystemSemaphore can also be accessed from multiple processes. This means QSystemSemaphore is a much heavier class, so if your application doesn’t need to access your semaphores across multiple processes, you will probably want to use QSemaphore.”

QMutex is more light-weight. It is used for synchronization between different threads.

June 23, 2011

Adi Adi
Lab Rat
206 posts

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.

Regards
Indrajeet

June 23, 2011

loladiro loladiro
Lab Rat
596 posts

You could place a lock file somewhere, where both machines can access it.

June 23, 2011

koahnig koahnig
Gene Splicer
2906 posts
Indrajeet wrote:
2 Apps running on 2 different machines access same file located at some location.

the following recommendation is then the method.

loladiro wrote:
You could place a lock file somewhere, where both machines can access it.

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.

June 23, 2011

cincirin cincirin
Ant Farmer
457 posts

You don’t have to exchange data between more processes. Just use aquire [doc.qt.nokia.com] to lock some memory block and release [doc.qt.nokia.com] to unlock that memory block.

June 23, 2011

koahnig koahnig
Gene Splicer
2906 posts
cincirin wrote:
You don’t have to exchange data between more processes. Just use aquire [doc.qt.nokia.com] to lock some memory block and release [doc.qt.nokia.com] to unlock that memory block.

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.

June 23, 2011

cincirin cincirin
Ant Farmer
457 posts

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.

Sorry, I did not see: “2 Apps running on 2 different machines”
In first post @Indrajeet not mention this situation.

June 23, 2011

Adi Adi
Lab Rat
206 posts

Hi All

So wat is the way to solve it can anyone suggest me.

Regards
Indrajeet

June 23, 2011

cincirin cincirin
Ant Farmer
457 posts

What about setPermissions [doc.qt.nokia.com] ? You can set QFile::ReadOwner | QFile::WriteOwner permission when you have access and QFile::ReadOther | QFile::WriteOther when you finish to read / write.

June 23, 2011

Adi Adi
Lab Rat
206 posts

Hi cincirin

Here owner is the one who opens the file first.

Regards
Indrajeet

June 23, 2011

peppe peppe
Ant Farmer
1029 posts

Wait a minute, how are the two applications (running on two different machines) supposed to write to the very same file? Are you using some network file system (SMB, NFS)?

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

Page  
1

  ‹‹ Need Help on Copying an Image from one GraphicsView/GraphicsScene to Another      [SOLVED] how to generate dynamic array of buttons???? ››

You must log in to post a reply. Not a member yet? Register here!