Re: At most 1 running copy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 26 Nov 2004 22:49:51 -0600, Ed Wilts <ewilts@xxxxxxxxxx> wrote:

> use FileHandle;
> use Fcntl qw(:DEFAULT :flock);
> my $FH = new FileHandle;
> sysopen(FH, "ftphandler.lock", O_RDWR | O_CREAT) or die "can't open ftphandler.lock: $!";
> flock(FH, LOCK_EX | LOCK_NB) or exit;
> print "running...\n";
> [rest of code here]
> close (FH);
> unlink ("ftphandler.lock");

Two things I see off the top of my head:
- O_RDWR|O_CREAT won't fail if the file already exists.  How does this
stop people from running it twice if the secon one starts up before
the first one has time to flock it?  Wouldn't you be better off using
O_EXCL|O_CREAT?
- Beware of using flock (or O_CREAT) on NFS mounted file systems. 
They either won't work at all, or they have a race condition in them. 
If you're creating lock files in the home directory, remember that in
the future your program might fail if they switch to a NAS for the
home directory.

Getting back to what I said earlier about the technique of creating a
unique file and then hard linking to the lock file, here is what "man
2 open" says:
              The solution  for
              performing  atomic file locking using a lockfile is to create a
              unique file on the same fs (e.g.,  incorporating  hostname  and
              pid),  use  link(2)  to  make a link to the lockfile. If link()
              returns 0, the lock is successful.  Otherwise, use  stat(2)  on
              the  unique file to check if its link count has increased to 2,
              in which case the lock is also successful.


-- 
 "To announce that there must be no criticism of the president, or that we
are to stand by the president, right or wrong, is not only unpatriotic and
servile, but is morally treasonable to the American public." -- Teddy Roosevelt


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux