Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Regarding Locking The File
Forum Updated to NodeBB v4.3 + New Features

Regarding Locking The File

Scheduled Pinned Locked Moved General and Desktop
39 Posts 10 Posters 17.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #4

    Hi cincirin

    How about QMutex?

    Which is better?

    Regards
    Indrajeet

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #5

      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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #6

        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.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Indrajeet
          wrote on last edited by
          #7

          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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            loladiro
            wrote on last edited by
            #8

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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #9

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

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cincirin
                wrote on last edited by
                #10

                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.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #11

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

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cincirin
                    wrote on last edited by
                    #12

                    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.

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Indrajeet
                      wrote on last edited by
                      #13

                      Hi All

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

                      Regards
                      Indrajeet

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        cincirin
                        wrote on last edited by
                        #14

                        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.

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          Indrajeet
                          wrote on last edited by
                          #15

                          Hi cincirin

                          Here owner is the one who opens the file first.

                          Regards
                          Indrajeet

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dangelog
                            wrote on last edited by
                            #16

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

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

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              koahnig
                              wrote on last edited by
                              #17

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

                              Vote the answer(s) that helped you to solve your issue(s)

                              1 Reply Last reply
                              0
                              • I Offline
                                I Offline
                                Indrajeet
                                wrote on last edited by
                                #18

                                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.

                                1 Reply Last reply
                                0
                                • Z Offline
                                  Z Offline
                                  ZapB
                                  wrote on last edited by
                                  #19

                                  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.

                                  Nokia Certified Qt Specialist
                                  Interested in hearing about Qt related work

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    goetz
                                    wrote on last edited by
                                    #20

                                    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?

                                    http://www.catb.org/~esr/faqs/smart-questions.html

                                    1 Reply Last reply
                                    0
                                    • I Offline
                                      I Offline
                                      Indrajeet
                                      wrote on last edited by
                                      #21

                                      Hi Zap

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

                                      Regards
                                      Indrajeet

                                      1 Reply Last reply
                                      0
                                      • I Offline
                                        I Offline
                                        Indrajeet
                                        wrote on last edited by
                                        #22

                                        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

                                        1 Reply Last reply
                                        0
                                        • Z Offline
                                          Z Offline
                                          ZapB
                                          wrote on last edited by
                                          #23

                                          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.

                                          Nokia Certified Qt Specialist
                                          Interested in hearing about Qt related work

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved