Regarding Locking The File



  • 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



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





  • Hi cincirin

    How about QMutex?

    Which is better?

    Regards
    Indrajeet



  • bq. 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


  • Moderators

    I guess you find the answer in this sequence of text taken from the detailed description
    [quote author="cincirin" date="1308812979"]"QSystemSemaphore":http://doc.qt.nokia.com/4.7-snapshot/qsystemsemaphore.html[/quote]

    :
    "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.



  • 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



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


  • Moderators

    [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.


  • Moderators

    [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.



  • Hi All

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

    Regards
    Indrajeet



  • 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 cincirin

    Here owner is the one who opens the file first.

    Regards
    Indrajeet



  • 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)?


  • Moderators

    peppe makes certainly a good point here.
    @Indrareet, I guess you need to tell a little more about your applications.



  • 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 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 write

    Give 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?



  • Hi Zap

    Can you please show how to lock the file while writing.

    Regards
    Indrajeet



  • Hi Volker

    Both are there but you can answer for second option.i.e
    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.

    Regards
    Indrajeet



  • No. You have not answered the question. We cannot advise you in detail until you give us a definitive description of the problem first.

    We have already told you two alternative approaches depending on your situation.



  • Sorry our last replies must have crossed in mid-air. If both apps are on the same machine then use QSystemSemaphore. What is not clear about that?



  • Hi Zap

    Can you please show me how to use QSystemSemaphore
    to avoid multiple processes to write in single file at same time.



  • 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.



  • I had a similar problem. My application is watching a directory using QFileSystemWatcher. If a new file is stored in the directory i want to read that file. The file is written to the directory by another application which is in no way connected to my app. The problem I encountered was that sometimes the FileSystemWatcher notices a new file and I start reading it with QFile.readAll() before it is acctually completely written, thus getting an empty or incomplete String. My dirty solution was checking if the file was modified within the last 2 seconds and if not start reading. This works in my case since the file is only once written and afterwards will only be accessed or modified by my application. But in the case mentioned by the OP this wont work.

    @Andre: Will QSystemSemaphore be an adequate solution for my problem as well ?



  • [quote author="KA51O" date="1309335683"]@Andre: Will QSystemSemaphore be an adequate solution for my problem as well ?[/quote]
    No, it will not.
    The use of QSystemSemaphore assumes that the process all use that same semaphore to get access to the resource. If they don't (and your case, you can be sure of that), then the semaphore is useless.



  • Too bad. Thanks for the reply anyways.
    Is there maybe another way to deal with my problem in a more "professional" way? I cannot use any OS specific methods like 'fuser' or 'lsof'.



  • Hi Andre

    If I acquire the semaphore while writing the file,
    Can any other process can read the same file at that time or only after semaphore is released.



  • No you cannot read while someone else has aquired the semaphore and is writing.

    How about using "QReadWriteLock":http://developer.qt.nokia.com/doc/qt-4.7/qreadwritelock.html ?
    I think its more intuitive to use.



  • @Rajveer:
    QReadWriteLock does not work across processes, so it is useless for this case.

    It is up to you to figure out if you want to allow reading while the file is being written to too. It depends on your application if that is a problem or not. You can look at QReadWriteLock to see what kind of problems you may expect though, and what kind of algorithm you could use.

    By default, if you only use one semaphore for all file access, then both reading and writing block the file completely. You could implement something more intelligent than that based on using one or more semaphores though. QReadWriteLock can be an inspiration for that.



  • Hi KA51O

    So if i take a WriteLock while writing to the file,so can any other process can read at that time.



  • [quote author="KA51O" date="1309336526"]Too bad. Thanks for the reply anyways.
    Is there maybe another way to deal with my problem in a more "professional" way? I cannot use any OS specific methods like 'fuser' or 'lsof'.[/quote]

    Why cannot you use those? You could implement a solution based on OS specific methods for each OS you support.



  • Hi Andre

    Below is example code for Read & write

    Read:
    @
    QFile myfile(filename);
    if (myfile.open(QIODevice::ReadOnly | QIODevice::Text)) // Open the file
    {
    Reading code;
    myfile.close();
    }
    @

    Write:
    @
    QFile myfile(filename);
    if (myfile.open(QIODevice::WriteOnly)) // Open the file
    {
    writing code;
    myfile.close();
    }
    @

    Andre can you please show me how to use the semaphore with above two codes.



  • No, I gave you enough pointers to get going on your own. Why don't you give using the semaphore a try on your own, and come back with what you came up with?



  • Ok Andre I will do that.



  • [quote author="Rajveer" date="1309338534"]Hi KA51O

    So if i take a WriteLock while writing to the file,so can any other process can read at that time.

    [/quote]

    Any other process that takes part in the (system) semaphore game will most probably not read the file. Any process which does not use a semaphore or mutex can happily read and write the file as long as it has the OS permission to do so.

    You can not avoid that situation entirely, you have to rely on the good behavior of all parties joining the party.



  • [quote author="Andre" date="1309338341"]@Rajveer:
    QReadWriteLock does not work across processes, so it is useless for this case. [/quote]
    Thanks for the hint.

    I hadn't stumbled upon "QSystemSemaphore's":http://developer.qt.nokia.com/doc/qt-4.7/qsystemsemaphore.html up until now. Thanks for that hint too.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.