Loading ...
Sorry, an error occurred while loading the content.

Lockfiles

Expand Messages
  • clerew5
    I have a resource (an EEPROM on the i2cbus that I have brought out) that needs to be accessed by a server and various subsidiary programs. I had hoped to
    Message 1 of 4 , Mar 2 11:34 AM
    • 0 Attachment
      I have a resource (an EEPROM on the i2cbus that I have brought out) that needs to be accessed by a server and various subsidiary programs. I had hoped to arrange for the mutex in the kernel thst is part of the i2c device driver to keep them from treading on each other, but there are problems there (which I am still working on).

      So can lockfiles be reliably used for the purpose. I.e., is creating a file an atomic process such that if process A creates it and process B tries to create/access it a moment later, will process B always see that it is already there? I see that there is a special add-on that can be used to create lockfiles, but that seems to be aimed at complicated situations involving NFS. In my case, both A and B are running on the slug.

      I am using slugosBE v 5.3.
    • rolandkbs
      Creating a file and checking for its presence is not an atomic operation. Use the semaphore mechanism (man semget(2)/semop(2), or if POSIX-semaphores are
      Message 2 of 4 , Mar 2 2:11 PM
      • 0 Attachment
        Creating a file and checking for its presence is not an atomic operation.
        Use the semaphore mechanism (man semget(2)/semop(2), or if POSIX-semaphores are available under slugosBE, sem_overview(7))

        Regards
        Roland

        --- In nslu2-linux@yahoogroups.com, "clerew5" <clerew5@...> wrote:
        >
        > I have a resource (an EEPROM on the i2cbus that I have brought out) that needs to be accessed by a server and various subsidiary programs. I had hoped to arrange for the mutex in the kernel thst is part of the i2c device driver to keep them from treading on each other, but there are problems there (which I am still working on).
        >
        > So can lockfiles be reliably used for the purpose. I.e., is creating a file an atomic process such that if process A creates it and process B tries to create/access it a moment later, will process B always see that it is already there? I see that there is a special add-on that can be used to create lockfiles, but that seems to be aimed at complicated situations involving NFS. In my case, both A and B are running on the slug.
        >
        > I am using slugosBE v 5.3.
        >
      • David Given
        ... Actually... There s a long and honoured art of using the filesystem for locking, using mkdir(2) and rmdir(2), like this: while (mkdir( /tmp/lockfile ,
        Message 3 of 4 , Mar 3 3:58 AM
        • 0 Attachment
          On 02/03/12 22:11, rolandkbs wrote:
          > Creating a file and checking for its presence is not an atomic operation.

          Actually...

          There's a long and honoured art of using the filesystem for locking,
          using mkdir(2) and rmdir(2), like this:

          while (mkdir("/tmp/lockfile", 0700) == -1) // try to take lock
          {
          // if mkdir failed, the lock is already taken, so wait and try
          // again
          sleep(1);
          }

          // lock now taken
          ... do something ...
          rmdir("/tmp/lockfile"); // release lock

          You can even do this from shell scripts; see lockfile(1).

          Of course, named semaphores (see sem_open(3) for details) are better in
          every possible way, but there are still cases (e.g. mailboxes, or doing
          things from scripts) where the above approach is suitable.

          --
          ┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

          │ "Never attribute to malice what can be adequately explained by
          │ stupidity." --- Nick Diamos (Hanlon's Razor)
        • clerew5
          ... snip ... Yes, I had not realized that semaphores would work between different processes, but that is clearly the proper way to do it. I see that there are
          Message 4 of 4 , Mar 8 11:00 AM
          • 0 Attachment
            --- In nslu2-linux@yahoogroups.com, David Given <dg@...> wrote:
            >
            > On 02/03/12 22:11, rolandkbs wrote:
            > > Creating a file and checking for its presence is not an atomic operation.
            >
            > Actually...
            >
            > There's a long and honoured art of using the filesystem for locking,
            > using mkdir(2) and rmdir(2), like this:

            snip

            > Of course, named semaphores (see sem_open(3) for details) are better in
            > every possible way, but there are still cases (e.g. mailboxes, or doing
            > things from scripts) where the above approach is suitable.

            Yes, I had not realized that semaphores would work between different processes, but that is clearly the proper way to do it. I see that there are also messages and even shared memory facilities, which might be useful for my problem.
          Your message has been successfully submitted and would be delivered to recipients shortly.