use Fcntl;
use POSIX qw(:unistd_h);
...
# $fh -- open file handle
# $lock is the structure that will be passed to fcntl().
# Note that values in the structure must be valid even
# though they are not required for F_GETLK. If you just
# zero-out everything fcntl() will return "Invalid argument"
my $lock = pack 'sslli', F_UNLCK, SEEK_SET, 0, 0, 0;
# Second parameter - 5, represents F_GETLK. I couldn't use
# F_GETLK directly because it is mapped to 8 (F_GETLK64) and
# I couldn't get the definition for flock64 (the equivalent
# of "flock" for large files). The reason F_GETLK64 is
# mapped to F_GETLK is because perl was compiled with
# USE_LARGE_FILES.
if (fcntl($fh, 5, $lock)){
my $lpid = (unpack 'sslli', $lock)[-1];
if ($lpid){
print "File is locked by PID: $lpid\n";
}else{
print "File is not locked\n";
}
}else{
print STDERR "fcntl() failed: $!\n";
}
...
####
short l_type;
short l_whence;
long long l_start;
long long l_len;
unsigned int l_pid;
####
short l_type; /* 2 bytes */
short l_whence; /* 2 bytes*/
long four_byte_foo; /* what's this? */
long long l_start; /* 8 bytes */
long long l_len; /* 8 bytes */
unsigned int l_pid; /* 4 bytes */
long four_byte_bar; /* what is this? */