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
Qt 6.11 is out! See what's new in the release blog

Regarding Locking The File

Scheduled Pinned Locked Moved General and Desktop
39 Posts 10 Posters 29.4k 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.
  • K Offline
    K Offline
    KA51O
    wrote on last edited by
    #29

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

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

      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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on last edited by
        #31

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #32

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

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

            Hi KA51O

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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #34

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

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

                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.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #36

                  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?

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

                    Ok Andre I will do that.

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

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

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

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KA51O
                        wrote on last edited by
                        #39

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

                        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