The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Util::LockFile::PID - An advisory file for pid lock

VERSION

This documentation describes version 0.01

SYNPOSIS

 use Util::LockFile::PID;
 
 my $file = '/lock/file/path';
 my $lock = Util::LockFile::PID->new( $file );
 my $pid = $lock->lock();

 if ( $pid )
 {
     if ( my $child = fork() )
     {
         my $handle = $lock->handle();
         syswrite $handle, $child;
         exit 0;
     }

     ## child safely does critical stuff
 }
 else
 {
     my $pid = Util::LockFile::PID->check( $file );

     die "another instance $pid already running\n" if $pid;
 }

DESCRIPTION

lock()

Attempts to acquire lock. Returns pid if successful. Returns undef otherwise.

handle()

Returns the handle of the lock file.

check( $filename )

Returns ID of process that owns the lock. Returns 0 if not locked.

SEE ALSO

Fcntl

AUTHOR

Kan Liu

COPYRIGHT and LICENSE

Copyright (c) 2010. Kan Liu

This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.