1833756 Members
2977 Online
110063 Solutions
New Discussion

How to list file locks

 
garbarino9000k370i
New Member

How to list file locks

Hi,
Is there a way to know what processes have locks on a file, and what kind of locks?
I was looking at lslk/lsof, by Victor Abell from Purdue University, but lslk doesn't seems to be ported to HP/UX, and lsof lists only the first entry on the lock table for each given file.
I had made a little and ugly utility that reads the whole file using fcntl, looking for the locked regions, but I think something better must exists, that obtains the information directly from the kernel lock tables.

Cheers,

Mauro
"This sentence is false."
5 REPLIES 5
Edward Alfert_2
Respected Contributor

Re: How to list file locks

fuser
"Do what you love and you will never work a day in your life." - Confucius
Sridhar Bhaskarla
Honored Contributor

Re: How to list file locks

There are no default commands to check the file lock percentage as far as I know.

If you have Glance+ installed, you can get the information as below.

$echo "print tbl_file_lock_util " >> test
$glance -j 1 -i 1 -adviser_only -syntax test

This will print the nflock usage.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: How to list file locks

Oops..

Please discard my message... I mistook.

The best tool that is ever known for openfile/locks is lsof. If that doesn't work for you... then I mute myself.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
garbarino9000k370i
New Member

Re: How to list file locks

Hello,
Thanks for the answers, but:
1) fuser desn't tell me anything about locks(Just about opens)
2) I don't want to know the file lock table utilization percentage, but the list of processes that have locks(read locks and/or write locks)on sectors of a file or in all the file(in the last case, I can use lsof)

"This sentence is false."
Suhas_3
Advisor

Re: How to list file locks

I guess lockf ( c function) will be usefull for you.
If you are looking for unix command / utility instead of C function, it won't work as, by the time you find out whether file / sector of file is locked / unlocked, the information can become out of date. with lockf, you can avoid this by aquiring lock.
Following is taken from "man lockf" ..
===============================================
Application Usage
Because in the future the variable errno will be set to EAGAIN rather
than EACCES when a section of a file is already locked by another
process, portable application programs should expect and test for
either value. For example:

if (lockf(fd, F_TLOCK, siz) == -1)
if ((errno == EAGAIN) || (errno == EACCES))
/*
* section locked by another process
* check for either EAGAIN or EACCES
* due to different implementations
*/
else if ...
/*
* check for other errors
*/
==============================================
Regards,
Suhas