[ale] Only one process

Brian Pitts brian at polibyte.com
Fri Jun 4 22:23:32 EDT 2010


On 06/04/2010 09:44 PM, JK wrote:
> I wrote:
>
>   >  You really must have a "create-file-only-if-it-doesn't-already-exist"
>   >  operation in order to make this work, and flock(1) is the first
>
> To clarify: this is perfectly possible in a C program or whatever,

In C this is open(..., O_EXCL|O_CREAT), right?

Since the OP's script is in Perl, can't he use these same modes? Replace 
the if(-f $pid_file) test with an attempt to create it.

use Fcntl;
sysopen(..., O_EXCL|O_CREAT) or die "File already exists\n";

That just worked in a simple test I ran. According to the documentation 
it's no good if NFS is involved.

O_EXCL
When used with O_CREAT, if the file already exists it is an error and 
the open will fail. In this context, a symbolic link exists, regardless 
of where its points to. O_EXCL is broken on NFS file	 systems, programs 
which rely on it for performing locking tasks will contain a race 
condition. 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.

-- 
All the best,
Brian Pitts


More information about the Ale mailing list